POJ 1753 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

The main topic: is to give you a 4*4 chess board, by flipping pieces to make the whole board into full black or all white, ask a few steps to complete the operation.
Note: When flipping one of its pieces, the upper and lower sides are to do the corresponding flip, that is, black to white, white to black.
1#include <iostream>2#include <stdio.h>3 using namespacestd;4 BOOLchess[6][6]= {{false}};5 intr[6]= {0,0,0,-1,1};6 intc[6]= {0,1,-1,0,0};7 intStep;8 BOOLFlag;9 BOOLReach_all ()//determine if it is a colorTen { One      for(intI=1;i<5; i++) A     { -          for(intj=1;j<5; j + +) -         { the             if(chess[i][j]!=chess[1][1]) -                 return false; -         } -     } +     return true; - } + voidTurnintRowintCol//Turn Chess A { at      for(intI=0;i<5; i++) -chess[row+r[i]][col+c[i]]=!chess[row+r[i]][col+C[i]]; -     return ; - } - voidDfsintRowintColintDeep//deep search for iterative backtracking is important - { in     if(deep==Step) -     { toflag=Reach_all (); +         return ; -     } the     if(Flag| | row==5) *         return ; $Turn (Row,col);//Turn ChessPanax Notoginseng     if(col<4) -DFS (row,col+1, deep+1); the     Else +DFS (row+1,1, deep+1); ATurn (Row,col);//if it doesn't fit, turn it back . the     if(col<4) +DFS (row,col+1, deep); -     Else $DFS (row+1,1, deep); $     return ; - } - intMain () the { -     Charch;Wuyi      for(intI=1; i<5; i++) the     { -          for(intj=1; j<5; J + +) Wu         { -scanf"%c",&ch); About             if(ch=='b') $chess[i][j]=true; -         } -GetChar ();//forgot to write before, always output impossible -     } A      for(step=0; step<= -; step++)//Enumerate each of these cases +     { theDfs1,1,0); -         if(flag) $              Break; the     } the     if(flag) theprintf"%d\n", step); the     Else -printf"impossible\n"); in     return 0; the}
View Code

POJ 1753 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.