TOJ 3248 Flip Game

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:

Choose any one of the 16 pieces.

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.

TOJ 3248 Flip Game

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

bwwb

bbwb

bwwb

bwww

Sample Output

4

Source

Northeastern Europe 2000

 

因为地图是4*4,每个格子只有2种情况,所以最多的情况只有2^16种。

可以把地图看成:

 0   1   2   3

 4   5   6   7

 8   9  10 11

12 13 14 15

从0开始搜索,下面的结点要么翻,要么不翻。记录每次翻完后的黑白棋的数量。如果(黑棋=16 或者 黑棋=0)表示已经达到目的。

 

 1 #include <stdio.h>

 2 #define inf 0x3f3f3f3f

 3 

 4 char g[4][4];

 5 int f[4][4];

 6 int dir[4][2]={

 7     {0,1},{0,-1},{1,0},{-1,0}

 8 };

 9 int ans;

10 int sum;

11 

12 void change(int x , int y){

13     f[x][y]=!f[x][y];

14     for(int i=0; i<4; i++){

15         int tx=x+dir[i][0];

16         int ty=y+dir[i][1];

17         if( 0<=tx && tx<4 && 0<=ty && ty<4){

18             f[tx][ty]=!f[tx][ty];

19         }

20     }

21 }

22 

23 void dfs(int h , int step){

24     if(sum==0 || sum==16){

25         if(step<ans)

26             ans=step;

27     }

28     if(h>=16)return;

29     //不翻 

30     dfs(h+1,step);

31     //

32     int x=h%4;

33     int y=h/4;

34     int sum1=0;

35     if(f[x][y]){

36         sum1--;    

37     }else{

38         sum1++;

39     }

40     for(int i=0; i<4; i++){

41         int tx=x+dir[i][0];

42         int ty=y+dir[i][1];

43         if( 0<=tx && tx<4 && 0<=ty && ty<4){

44             if(f[tx][ty]){

45                 sum1--;

46             }else{

47                 sum1++;

48             }    

49         }

50     }

51     change(x,y);

52     sum+=sum1;

53     dfs(h+1,step+1);

54     sum-=sum1;

55     change(x,y);

56 }

57 

58 int main()

59 {

60     sum=0;

61     for(int i=0; i<4; i++){

62         scanf("%s",g[i]);

63     }

64     for(int i=0; i<4; i++){

65         for(int j=0; j<4; j++){

66             if(g[i][j]=='b'){

67                 f[i][j]=1;

68                 sum++;                

69             }

70             else{

71                 f[i][j]=0;

72             }

73         }

74     }

75     ans=inf;

76     dfs(0,0);

77     if(ans==inf){

78         printf("Impossible\n");

79     }else{

80         printf("%d\n",ans);

81     }

82     return 0;

83 }

 

你可能感兴趣的:(game)