Flip Game
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 32384 |
|
Accepted: 14142 |
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: BwbwWwww BBWB bwwb here ' B ' denotes pieces lying their black side up and ' w ' denotes pieces lying their white s IDE 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 Blac K 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 20001#include <stdio.h>2#include <iostream>3 using namespacestd;4 BOOLa[6][6] = {false} ;5 intR[] = {-1,1,0,0,0} ;6 intC[] = {0,0, -1,1,0} ;7 BOOLFlag;8 intStep;9 Ten BOOLJudge_all () One { A for(inti =1; I <5; i++) { - for(intj =1; J <5; J + +) { - if(A[i][j]! = a[1][1]) the return false ; - } - } - return true ; + } - + voidFlip (intRow,intCol) A { at for(inti =0; I <5; i++) { -A[row + R[i]][col + c[i]] =!a[row + R[i]][col +C[i]]; - } - } - - voidDFS (intRow,intCol,intDeep ) in { - if(Deep = =Step) { toFlag =Judge_all (); + return ; - } the * if(Flag | | row = =5) $ return ;Panax Notoginseng - Flip (row, col); the if(Col <4) +DFS (Row, col +1, Deep +1) ; A Else theDFS (row +1,1, Deep +1) ; + //printf ("row =%d, col =%d, deep =%d\n", row, col, deep); - Flip (row, col); $ if(Col <4) $DFS (Row, col +1, deep); - Else -DFS (row +1,1, deep); the - return ;Wuyi } the - intMain () Wu { - //freopen ("A.txt", "R", stdin); About Charx; $ for(inti =1; I <5; i++) { - for(intj =1; J <5; J + +) { -CIN >>x; - if(x = ='b') AA[I][J] =1 ; + } the } - $ for(step =0; Step <= -; step++) { theDFS (1,1,0) ; the if(flag) the Break ; the } - /*step = 2; in DFS (1, 1, 0);*/ the the if(flag) Aboutprintf ("%d\n", step); the Else theprintf ("impossible\n") ; the + return 0 ; -} View CodeI have to say it's a trick to write with recursion, and it feels like the problem is to put the violence in a recursive way. (although DFS is called) CIN originally does not accept "line break". Ideas: At least 0 steps, up to 16 steps. First check 0 steps, then 1 steps, 2 steps ..., etc. Reprint: http://www.cnblogs.com/lyy289065406/archive/2011/07/29/2120501.html |