Flip Game
Time Limit: MS Memory Limit: 65536 KB
64-bit integer IO format: %i64d,%i64u Java class name: Main
[Submit] [Status] [Discuss]
Descriptionflip 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. Inputthe input consists of 4 lines with 4 characters "W" or "B" from each of the denote game field position. Outputwrite to the output file a single integer number-the minimum number of rounds needed to achieve the goal of the GA Me from 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
Split Line **************************************************** ******************************************************************************
The school actually put it into the enumeration of exercises, I was stunned, but careful thinking is indeed an enumeration of problems, but I am using the idea of dynamic planning so I think it is dynamic planning,
1 //The idea of dynamic planning, every point has two cases, the complexity is 2^n, need to pay attention to the problem of scanf2#include <stdio.h>3#include <string.h>4 using namespacestd;5 BOOLchess[6][6]={false};6 intStep;7 BOOLFlag;8 intr[5]={-1,1,0,0,0};9 intc[5]={0,0,-1,1,0};//(0,0) BiaoshizijihenhaoyongaTen BOOLIsover () {//Jianchashifoujieshu One inti,j; A for(i=1;i<5; i++) - for(j=1;j<5; j + +) - if(chess[i][j]!=chess[1][1]) the return false; - return true; - } - voidFlipintRowintCol) {//Inverse dictionary corresponds to all points that can be flipped + for(intI=0;i<5; i++) -chess[row+r[i]][col+c[i]]=!chess[row+r[i]][col+C[i]]; + } A voidDfsintRowintColintDeep ) { at if(deep==Step) { - //printf ("hhh%d==%dflag==%d\n", Deep,step,flag); -flag=isover (); - return ; - } - in if(Flag| | row==5)return ; -Flip (Row,col);//Flip to if(col<4) DFS (row,col+1, deep+1); + ElseDFS (row+1,1, deep+1); - Flip (row,col); the if(col<4) DFS (row,col+1, deep); * ElseDFS (row+1,1, deep); $ return ;Panax Notoginseng } - intMain () the { + inti,j; A Chartemp; the //memset (chess,0,sizeof (chess)); + for(intI=1;i<5; i++){ - for(intj=1;j<5; j + +){ $scanf"%c", &temp);//CIN will not accept newline characters, but scanf will be read in with newline characters. $ if(temp=='b') chess[i][j]=true; - } - GetChar (); the } - /*For (i=1;i<5;i++)Wuyi For (j=1;j<5;j++) the printf ("%d", chess[i][j]);*/ - for(step=0; step<= -; step++) {//The maximum is 16 flips, because after 16 times each flip will cause and does not turn over that lattice the same situation. WuDfs1,1,0); - //printf ("step=%d\n", STEP); About if(flag) Break; $ } - if(flag) printf ("%d\n", step); - Elseprintf"impossible\n"); - A}View Code
Here is to turn several pieces for the DP Clue, and the subset generated like Ah! Each one is not turned over, and the subset generation is each to take the number of clues to find the end condition.
I temporarily understand the violence, DP, deep search (I am still a small white ah, will be less, do not blame, temporarily on the knowledge of the summary of the point is not very mature)
POJ1573 (enumeration, dynamic planning)