poj1753 Flip Game

Source: Internet
Author: User
Tags rounds

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:
    1. Choose any one of the pieces.
    2. 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
This question looked at other people's ideas, wrote them out. First of all to be clear, if you can succeed, as long as 16 times, that is, each point is doubled, because for each point, two times and not turn over the point and the surrounding 4 points of the situation is the same (can draw), so just consider 16 steps, from 1 to 16 to see if you can make each point in the diagram is ' or ' W ', the graph has a total of 2^16 different forms. This problem learns that when a two-dimensional array is a function parameter, it cannot be written as void F (s[][]), the second dimension must have a size, such as void F (s[][10]), or it is directly written as void F (s[6][10]).
#include <stdio.h> #include <string.h>char s[6][10];int flag,step;int tab[10][2]={{0,0},{0,1},{-1,0},{0, -1},{1,0}};int Panduan (char s[6][10]) {int i,j;Char c=s[0][0];for (i=0;i<4;i++) {for (j=0;j<4;j++) {if (s[i][j]!=c)return 0;}}return 1;} void Flip (int row,int col) {int i,j,xx,yy;if (s[row][col]== ' W ') s[row][col]= ' B ';Else s[row][col]= ' W ';for (i=1;i<=4;i++) {XX=ROW+TAB[I][0];YY=COL+TAB[I][1];if (xx>=0 && xx<=3 && yy>=0 && yy<=3) {if (s[xx][yy]== ' W ') s[xx][yy]= ' B ';Else s[xx][yy]= ' W ';}}}void dfs (int row,int col,int dep) {int i,j;if (dep==step) {Flag=panduan (s);Return}if (flag | | row==4) return;Flip (Row,col);if (col<3) {DFS (ROW,COL+1,DEP+1); There's a line over here.}else Dfs (row+1,0,dep+1);//If the current number of columns is already the largest, then the row is flipped and the number of columns is changed back to 0if (flag) return;Flip (Row,col); If this point is not successful, turn back to this point, but notice that it is only this point, not all the things that have been formed behind this point, I have been thinking about it for a long time.if (col<3) {DFS (ROW,COL+1,DEP);//Because this point (Row,col) does not flip, so is the current number of steps or DEP}else Dfs (ROW+1,0,DEP);return;} int main () {int n,m,i,j;while (scanf ("%s", S[0])!=eof){for (i=1;i<4;i++) {scanf ("%s", S[i]);}if (Panduan (s)) {printf ("0\n"); continue;}for (step=1;step<=16;step++) {flag=0;DFS (0,0,0);if (flag) break;}if (flag) {printf ("%d\n", step);}else printf ("impossible\n");}return 0;}

poj1753 Flip Game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.