1 Source Code
2
3 Problem: 1753 User: eth1
4 Memory: 716K Time: 79MS
5 Language: G++ Result: Accepted
6
7 Source Code
8 #include <iostream>
9 #include <cstring>
10 #include <queue>
11 #include <vector>
12 using namespace std;
13 int n=17,p[16];
14 int black=0;
15 int white=(1<<16)-1;
16 void solve(int cur,int d,int b)
17 {
18 if(cur==black || cur==white)
19 {
20 if(d < n) n=d;
21 return;
22 }
23 if(d >= n) return;
24 for(int i=b;i<16;i++)
25 {
26 int tem=cur;
27 int x=i/4,y=i%4;
28 int t=p[x*4+y];
29 if((cur & t)==0) tem+=t; else tem-=t;
30 if(x-1>=0){ t=p[(x-1)*4+y];
31 if((cur & t)==0) tem+=t; else tem-=t;}
32 if(x+1<4) {t=p[(x+1)*4+y];
33 if((cur & t)==0) tem+=t; else tem-=t;}
34 if(y-1>=0) {t=p[x*4+y-1];
35 if((cur & t)==0) tem+=t; else tem-=t;}
36 if(y+1<4) {t=p[x*4+y+1];
37 if((cur & t)==0) tem+=t; else tem-=t;}
38 solve(tem,d+1,i+1);
39
40 }
41 }
42 int main()
43 {
44 char s[5];
45 for(int i=0;i<16;i++) p[i]=(1<<i);
46 int cur=0;
47 for(int i=0;i<4;i++)
48 {
49 cin>>s;
50 for(int j=0;j<4;j++)
51 {
52 if(s[j]=='b') cur+=p[i*4+j];
53 }
54 }
55 solve(cur,0,0);
56 if(n!=17) cout<<n<<endl;
57 else cout<<"Impossible"<<endl;
58 return 0;
59 }
 posted on 2011-05-14 13:01  eth0  阅读(158)  评论(0编辑  收藏  举报