Title Link: http://poj.org/problem?id=1753
Flip Game
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 36419 |
|
Accepted: 15866 |
Description
Flip game is played on a rectangular 4×4 field with two-sided pieces placed on each of its squares. One side of each piece are white and the other one are black and each piece is lying either it's black or white side up. Each round your 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 is chosen every round according to the following rules:
- Choose any one of the 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 is 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 was shown at the picture) and then the field would 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 is to write a program, that would 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" from each of the 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 fro M the given position. If The goal is initially achieved and then write 0. If it's impossible to achieve the goal and then write the word "impossible" (without quotes).
Sample Input
Bwwbbbwbbwwbbwww
Sample Output
4
Source
Northeastern Europe 2000 Test Instructions: Enter 4*4 characters, including b,w, each time you select a location, change the position up and down to include their own values, ask at least how many times to change. Analysis: Violent enumeration again.
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>using namespacestd;inta[6][6];Charstr[Ten][Ten];intb[5][2]= {1,0,0,1,-1,0,0,-1,0,0};intMin;intPaiintans) { intt=a[1][1]; intFlag =0; for(intI=1; i<=4; i++) { for(intj=1; j<=4; J + +) { if(a[i][j]!=t) {flag=1; I=5; Break; } } } if(Flag = =0) { if(Ans <Min) Min=ans; return 1; } return 0;}voidDFS (intXintYintans) { if(y>4) {x+=1; Y-=4; } if(x>4|| Y>4) { intf =Pai (ans); return ; } DFS (x, y+1, ans); for(intk=0; k<5; k++) {a[x+b[k][0]][y+b[k][1]]^=1; } DFS (x, y+1, ans+1); for(intk=0; k<5; k++) {a[x+b[k][0]][y+b[k][1]]^=1; }}intMain () {Charstr[Ten][Ten]; Memset (A,-1,sizeof(a)); for(intI=1; i<=4; i++) {scanf ("%s", str[i]+1); } for(intI=1; i<=4; i++) { for(intj=1; j<=4; J + +) { if(Str[i][j] = ='b') A[i][j]=1; ElseA[i][j]=0; }} Min= -; if(Pai (0)) Min=0; ElseDFS (1,1,0); if(min== -) printf ("impossible\n"); Elseprintf ("%d\n", Min); return 0;}
POJ 1753 Flip Game