POJ 1753 Flip Game

来源:互联网 发布:诺基亚 x311玩java 编辑:程序博客网 时间:2024/06/08 18:47
Flip Game
Time Limit: 1000MS
 Memory Limit: 65536KTotal Submissions: 40291 Accepted: 17497

Description

Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each round you 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 are chosen every round according to the following rules:
  1. Choose any one of the 16 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 are 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 is shown at the picture), then the field will 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 are to write a program that will 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" each that 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 from the given position. If the goal is initially achieved, then write 0. If it's impossible to achieve the goal, then write the word "Impossible" (without quotes).

Sample Input

bwwbbbwbbwwbbwww

Sample Output

4
#include<stdio.h>char map[4][4];bool judge()//判断是否全部颜色相同 {for(int i=0;i<4;i++)for(int j=0;j<4;j++)if(map[i][j]!=map[0][0])return  false;return true; }void change(int i)//翻格子后的变化 {int r=i/4;   int c=i%4;  //转换横纵坐标 map[r][c]=(map[r][c]=='b'?'w':'b'); if(r>0) map[r-1][c]=(map[r-1][c]=='b'?'w':'b');if(r<3) map[r+1][c]=(map[r+1][c]=='b'?'w':'b');if(c>0) map[r][c-1]=(map[r][c-1]=='b'?'w':'b');if(c<3) map[r][c+1]=(map[r][c+1]=='b'?'w':'b');}bool DFS(int n,int num){if(n==0){if(judge())return true ;else return false;}for(int i=num;i<16;i++){change(i);//上下左右变色 if(DFS(n-1,i+1))//判断是否找到 return true;change(i);//变色复原 }return false;}int main(){int i;bool flag=false;for(i=0;i<4;i++){for(int j=0;j<4;j++)scanf("%c",&map[i][j]);getchar();}for(i=0;i<=16;i++){if(DFS(i,0))//判断是否找到 {flag=true; break;}}if(flag)printf("%d\n",i);else printf("Impossible\n");return 0;}

Frighting!
1 0
原创粉丝点击