Question:http://poj.org/problem?id=1753
问题点:穷举。
1 #include <iostream>
2 using namespace std;
3 bool a[4][4];//记录输入
4 bool b[4][4];//记录操作
5 bool c[4][4];//中间变量
6 bool judge()
7 {
8 int sum=0;
9 for(int i=0;i<4;i++)
10 {
11 for(int j=0;j<4;j++)
12 {
13 sum+=c[i][j];
14 }
15 }
16 if(sum==0||sum==16) return true;
17 else return false;
18 }
19 int inttobools(int k)
20 {
21 int cur=0;
22 int count=0;
23 for(int i=0;i<4;i++)
24 {
25 for(int j=0;j<4;j++)
26 {
27 b[i][j]=k&1<<cur++?true:false;
28 if(b[i][j]) count++;
29 }
30 }
31 return count;
32 }
33 void boolscpy()
34 {
35 for(int i=0;i<4;i++)
36 {
37 for(int j=0;j<4;j++)
38 {
39 c[i][j]=a[i][j];
40 }
41 }
42 }
43 int main()
44 {
45 memset(a,0,16*sizeof(char));
46 memset(b,0,16*sizeof(char));
47 memset(c,0,16*sizeof(char));
48 char ch;
49 int min=20;
50 for(int i=0;i<4;i++)
51 {
52 for(int j=0;j<4;j++)
53 {
54 ch=getchar();
55 if(ch=='w')
56 a[i][j]=1;
57 else
58 a[i][j]=0;
59 }
60 getchar();
61 }
62 int s=0;
63 for(int l=0;l<65536;l++)
64 {
65 int count=inttobools(l);//转换b[][],并返回其中数字1的数目
66 if(count>=min) continue;
67 boolscpy();//赋值给c[][]
68 for(int i=0;i<4;i++)
69 {
70 for(int j=0;j<4;j++)
71 {
72 if(b[i][j])
73 {
74 c[i][j]=!c[i][j];
75 if(i>0) c[i-1][j]=!c[i-1][j];
76 if(j>0) c[i][j-1]=!c[i][j-1];
77 if(i<3) c[i+1][j]=!c[i+1][j];
78 if(j<3) c[i][j+1]=!c[i][j+1];
79 }
80 }
81 }
82 if(judge()) {min=count;s=l;}
83 }
84
85 if(min==20) cout<<"Impossible"<<endl;
86 else cout<<min<<endl;
87 return 0;
88 }