Poj 1753 Flip Game 状态压缩 + DFS

暴力枚举

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <cstdlib>
 6 #include <cmath>
 7 
 8 using namespace std;
 9 
10 char c;
11 
12 int m,t;
13 
14 int sum = 999999;
15 
16 void search(int site,int sta,int ans)
17 {
18     if(sta == 0 || sta == 65535)
19     {
20         if(ans < sum)
21         {
22             sum = ans;
23         }
24         return ;
25     }
26     else if(sta == m && ans != 0)
27     {
28         return;
29     }
30 
31     if(site >= 17)
32         return;
33 
34     search(site+1,sta,ans);//当前棋子不改变
35 
36     sta ^= (1 << (site-1));//center
37 
38     if(site%4 != 0)
39         sta ^= (1 << (site)); //right
40 
41     if(site%4 != 1)
42         sta ^= (1 << (site-2)); // left
43 
44     if(site >= 5)
45         sta ^= (1 << (site-5)); //up
46 
47     if(site <= 12)
48         sta ^= (1 << (site+3)); //down
49 
50     search(site+1,sta,ans+1);//改变当前棋子后继续枚举
51 }
52 
53 int main()
54 {
55     int i,j;
56 
57     char s[10];
58 
59     sum = 999999;
60     
61     for(m = 0,t = 1,i = 0;i < 4; i++)
62     {
63         scanf("%s",s);
64         for(j = 0;j < 4; j++)
65         {
66             if(s[j] == 'b')
67                 m += t;
68             t *= 2;
69         }
70 
71     }
72 
73     search(1,m,0);
74 
75     if(sum != 999999)
76         cout<<sum<<endl;
77     else cout<<"Impossible"<<endl;
78 
79     return 0;
80 }

 

你可能感兴趣的:(Poj 1753 Flip Game 状态压缩 + DFS)