poj 1753 Flip Game

来源:互联网 发布:电脑软件制作 编辑:程序博客网 时间:2024/06/06 04:37
Flip Game
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 34683 Accepted: 15184

Description

Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules: 
  1. Choose any one of the 16 pieces. 
  2. Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).

Consider the following position as an example: 

bwbw 
wwww 
bbwb 
bwwb 
Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become: 

bwbw 
bwww 
wwwb 
wwwb 
The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal. 

Input

The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.

Output

Write to the output file a single integer number - the minimum number of rounds needed to achieve the goal of the game from the given position. If the goal is initially achieved, then write 0. If it's impossible to achieve the goal, then write the word "Impossible" (without quotes).

Sample Input

bwwbbbwbbwwbbwww

Sample Output

4

Source

Northeastern Europe 2000


注意到没有说多组测试数据,(实际肯定是多组的),证明不用写while( ....  !=EOF)之类的,我就是因为这超时了

[cpp] view plaincopy
  1. #include<cstdio>  
  2. #include<string>  
  3. #include<cstring>  
  4. #include<iostream>  
  5. #include<cmath>  
  6. #include<algorithm>  
  7. #include<climits>  
  8. #include<queue>  
  9. #include<vector>  
  10. #include<map>  
  11. #include<sstream>  
  12. #include<set>  
  13. #include<stack>  
  14. #include<utility>  
  15. #pragma comment(linker, "/STACK:102400000,102400000")  
  16. #define PI 3.1415926535897932384626  
  17. #define eps 1e-10  
  18. #define sqr(x) ((x)*(x))  
  19. #define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)  
  20. #define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)  
  21. #define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)  
  22. #define  lson   num<<1,le,mid  
  23. #define rson    num<<1|1,mid+1,ri  
  24. #define MID   int mid=(le+ri)>>1  
  25. #define zero(x)((x>0? x:-x)<1e-15)  
  26. #define mp    make_pair  
  27. #define _f     first  
  28. #define _s     second  
  29.   
  30. using namespace std;  
  31. const int INF =0x3f3f3f3f;  
  32. //const int maxn=    ;  
  33. //const int maxm=    ;  
  34. //const int INF=    ;  
  35. typedef long long ll;  
  36. const ll inf =(ll)1000000000000000;//1e15;  
  37. //ifstream fin("input.txt");  
  38. //ofstream fout("output.txt");  
  39. //fin.close();  
  40. //fout.close();  
  41. //freopen("a.in","r",stdin);  
  42. //freopen("a.out","w",stdout);  
  43. //by yskysker123  
我的代码:朴素的dfs
[cpp] view plaincopy
  1. int a[6][6];  
  2. const int n=4,m=4;  
  3. int ans;  
  4. int dir[4][2]={ {-1,0},{+1,0},{0,-1},{0,+1} };  
  5. bool in(int &y, int &x)  
  6. {  
  7.     return 1<=y&&y<=n&&1<=x&&x<=m;  
  8. }  
  9. void dfs(int id,int step)  
  10. {  
  11.     if(id==17)  
  12.     {  
  13.        int sum=0;  
  14.     for(int i=1;i<=n;i++)  
  15.     {  
  16.         for(int j=1;j<=m;j++)  
  17.         {  
  18.             sum+=a[i][j];  
  19.         }  
  20.     }  
  21.     if(sum==0||sum==16)  ans=min(ans,step);  
  22.         return ;  
  23.   
  24.     }  
  25.     int y=(id-1)/4 +1;  
  26.     int x= id%4 ==0?4: id%4 ;  
  27. //    cout<<y<<" "<<x<<endl;  
  28.   
  29.     dfs(id+1,step);  
  30.   
  31.     a[y][x]^=1;  
  32.     for(int i=0;i<4;i++ )  
  33.     {  
  34.         int ty=y+dir[i][0];  
  35.         int tx=x+dir[i][1];  
  36.         if(!in(ty,tx)) continue;  
  37.         a[ty][tx]^=1;  
  38.     }  
  39.   
  40.     dfs(id+1,step+1);  
  41.        a[y][x]^=1;  
  42.         for(int i=0;i<4;i++ )  
  43.     {  
  44.         int ty=y+dir[i][0];  
  45.         int tx=x+dir[i][1];  
  46.         if(!in(ty,tx)) continue;  
  47.         a[ty][tx]^=1;  
  48.     }  
  49.   
  50. }  
  51. int main()  
  52. {  
  53.     char ch;  
  54.     while(1)  
  55.     {  
  56.         ans=INF;  
  57.         FOR1(i,n)  
  58.        {  
  59.           FOR1(j,m)  
  60.           {  
  61.             if(scanf(" %c",&ch)==EOF  )  
  62.                 break;  
  63.             a[i][j]=ch=='w'?0:1;  
  64. //            cout<<a[i][j]<<endl;  
  65.          }  
  66.        }  
  67.        dfs( 1,0);  
  68.        if(ans==INF)  {puts("Impossible") ;continue;}  
  69.          printf("%d\n",ans );  
  70.     }  
  71.   
  72.   
  73.   
  74.     return 0;  
  75. }  


看了别人的代码,发现可以对深度进行限制,然后逐步递增,这样就既可以保证最小,又可以免去朴素dfs找到真正结果后不必要的后续验证(是否是最小)。

但是实际上这种方法比朴素dfs慢了一倍,200ms左右,我认为是数据不大的缘故,不必要的验证并未花费太多时间。(层数还太少)


另外就是可以从全部是白或全部是黑为起点进行搜索,把所有可能的状态存下来,我并没有验证这种方法是否可行。


[cpp] view plaincopy
  1. int a[6][6];  
  2. const int n=4,m=4;  
  3. int ans,deep;  
  4. bool flag;  
  5. int dir[4][2]={ {-1,0},{+1,0},{0,-1},{0,+1} };  
  6. bool in(int &y, int &x)  
  7. {  
  8.     return 1<=y&&y<=n&&1<=x&&x<=m;  
  9. }  
  10. void dfs(int id,int step)  
  11. {  
  12.     if(step==deep)  
  13.     {  
  14.        int sum=0;  
  15.     for(int i=1;i<=n;i++)  
  16.     {  
  17.         for(int j=1;j<=m;j++)  
  18.         {  
  19.             sum+=a[i][j];  
  20.         }  
  21.     }  
  22.     if(sum==0||sum==16)  flag=1;  
  23.         return ;  
  24.   
  25.     }  
  26.     if(flag||id==17)  return ;  
  27.     int y=(id-1)/4 +1;  
  28.     int x= id%4 ==0?4: id%4 ;  
  29. //    cout<<y<<" "<<x<<endl;  
  30.   
  31.     dfs(id+1,step);  
  32.   
  33.     a[y][x]^=1;  
  34.     for(int i=0;i<4;i++ )  
  35.     {  
  36.         int ty=y+dir[i][0];  
  37.         int tx=x+dir[i][1];  
  38.         if(!in(ty,tx)) continue;  
  39.         a[ty][tx]^=1;  
  40.     }  
  41.   
  42.     dfs(id+1,step+1);  
  43.        a[y][x]^=1;  
  44.         for(int i=0;i<4;i++ )  
  45.     {  
  46.         int ty=y+dir[i][0];  
  47.         int tx=x+dir[i][1];  
  48.         if(!in(ty,tx)) continue;  
  49.         a[ty][tx]^=1;  
  50.     }  
  51.   
  52. }  
  53. int main()  
  54. {  
  55.     char ch;  
  56.   
  57.   
  58.         ans=INF;  
  59.         flag=0;  
  60.         FOR1(i,n)  
  61.        {  
  62.           FOR1(j,m)  
  63.           {  
  64.             if(scanf(" %c",&ch)==EOF  )  
  65.                 break;  
  66.             a[i][j]=ch=='w'?0:1;  
  67.          }  
  68.        }  
  69.        for(int i=0;i<=16;i++)  
  70.        {  
  71.              deep=i;  
  72.              dfs( 1,0);  
  73.              if(flag)  {ans=i;break;}  
  74.        }  
  75.   
  76.        if(ans==INF)  puts("Impossible") ;  
  77.         else printf("%d\n",ans );  
  78.   
  79.   
  80.   
  81.   
  82.     return 0;  
  83. }  


0 0