poj 1753 Flip Game (dfs)
2014-04-26 11:09
609 查看
Flip Game
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:
Choose any one of the 16 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 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
Sample Output
Source
Northeastern Europe 2000
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 28805 | Accepted: 12461 |
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:
Choose any one of the 16 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 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
bwwb bbwb bwwb bwww
Sample Output
4
Source
Northeastern Europe 2000
//164K 125MS C++ 1211B 2014-04-26 11:02:12
/*
题意:
问最少翻几步可以使棋盘棋子一样,不可能就输出Impossible
搜索枚举:
枚举全部状态,每个位置的棋子有翻或不翻两种状态,枚举全部状态。
注意一个棋子翻两次则和没翻一样,所以一种有2^16种情况,用dfs枚举全部状态。
*/
#include<stdio.h>
#include<string.h>
int g[10][10];
int flag;
int judge(int tg[][10]) //判断
{
for(int i=1;i<=4;i++)
for(int j=1;j<=4;j++)
if(g[i][j]!=g[1][1]) return 0;
return 1;
}
void flip(int i,int j) //翻棋
{
g[i][j]^=1;
g[i-1][j]^=1;
g[i+1][j]^=1;
g[i][j-1]^=1;
g[i][j+1]^=1;
}
void dfs(int x,int y,int cnt,int n)
{
if(cnt==n){
flag=judge(g);
return;
}
if(flag || y>4) return;
flip(x,y);
if(x<4) dfs(x+1,y,cnt+1,n);
else dfs(1,y+1,cnt+1,n);
flip(x,y);
if(x<4) dfs(x+1,y,cnt,n);
else dfs(1,y+1,cnt,n);
}
int main(void)
{
char c[10];
while(scanf("%s",c)!=EOF)
{
memset(g,0,sizeof(g));
for(int i=0;i<4;i++) g[1][i+1]=c[i]=='b'?1:0;
for(int i=1;i<4;i++){
scanf("%s",c);
for(int j=0;j<4;j++)
g[i+1][j+1]=c[j]=='b'?1:0;
}
flag=0;
int cnt=-1;
for(int i=0;i<16;i++){
dfs(1,1,0,i);
if(flag){
cnt=i;break;
}
}
if(cnt==-1) puts("Impossible");
else printf("%d\n",cnt);
}
return 0;
}
/*
bwwb bbwb bwwb bwww
bwbw
bwww
wwwb
wwwb
bwww
wwww
wwww
wwww
*/
相关文章推荐
- poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)
- POJ 1753 Flip Game (DFS)
- 【dfs】poj1753——Flip Game
- POJ 1753 Flip Game (DFS 太暴力了!)
- poj 1753 Flip Game (dfs + 枚举)
- poj 1753 Flip Game(dfs)_但是
- poj 1753 Flip Game【dfs】
- POJ 1753 Flip Game(类似与N皇后问题的DFS)
- POJ--1753--Flip Game【DFS】
- poj_1753 Flip Game(dfs+枚举)/(bfs+位运算)
- POJ 1753 Flip Game (DFS + 枚举)
- POJ1753 Flip Game【DFS】
- POJ 1753 Flip Game(dfs+枚举)
- poj 1753 Flip Game(dfs)
- POJ 1753 Flip Game (DFS)
- poj--1753--Flip Game(dfs好题)
- 解题报告:POJ 1753 Flip Game 简单深搜dfs
- POJ 1753 Flip Game(回溯查找,dfs)
- poj--1753--Flip Game(dfs好题)
- poj 1753 Flip Game(枚举,dfs)