Flip Game

http://poj.org/problem?id=1753

 1 #include<cstdio>

 2 #include<algorithm>

 3 #include<string.h>

 4 using namespace std;

 5 

 6 char s[6][6];

 7 int a[6][6];

 8 int deep,step,flag;

 9 void inti(int row,int c)

10 {

11     a[row-1][c]=!a[row-1][c];

12     a[row][c+1]=!a[row][c+1];

13     a[row+1][c]=!a[row+1][c];

14     a[row][c-1]=!a[row][c-1];

15     a[row][c]=!a[row][c];

16 }

17 int check()

18 {

19     for(int i=1; i<=4; i++)

20     {

21         for(int j=1; j<=4; j++)

22         {

23             if(a[i][j]!=a[1][1]) return 0;

24         }

25     }

26     return 1;

27 }

28 void dfs(int row,int c,int deep)

29 {

30     if(deep==step)

31     {

32         flag=check();

33         return;

34     }

35     if(flag||row==5) return;

36     inti(row,c);

37     if(c<4)

38         dfs(row,c+1,deep+1);

39     else

40         dfs(row+1,1,deep+1);

41 

42     inti(row,c);

43     if(c<4)

44         dfs(row,c+1,deep);

45     else

46         dfs(row+1,1,deep);

47     return ;

48 }

49 int main()

50 {

51     memset(a,0,sizeof(a));

52     for(int i=0; i<4; i++)

53     {

54         scanf("%s",s[i]);

55     }

56     for(int i=0; i<4; i++)

57     {

58         for(int j=0; j<4; j++)

59         {

60             if(s[i][j]=='b')

61                 a[i+1][j+1]=1;

62         }

63     }

64     for(step=0; step<=16; step++)

65     {

66         dfs(1,1,0);

67         if(flag)

68         {

69             printf("%d\n",step);

70             break;

71         }

72     }

73     if(!flag) printf("Impossible\n");

74     return 0;

75 }
View Code

 

你可能感兴趣的:(game)