poj 1753 Flip Game——DFS(分类是枚举)
2014-06-15 21:35
666 查看
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
第一次用c++写程序...
#include <iostream>
#include <cstdlib>
#include <string>
#include <climits>
using namespace std;
char mp1[4][4];
int mp2[4][4];
int num=INT_MAX;
int pd()
{
int i,j,js=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(mp2[i][j]==1)
{
js++;
}
}
}
if(js==16||js==0)
{
return 1;
}
return 0;
}//此函数判断是否成为全白色或是全黑色
void gz(int a,int b)
{
if(mp2[a][b]==1)
{
mp2[a][b]=0;
}
else
{
mp2[a][b]=1;
}
}
void fan(int x,int y)
{
gz(x,y);
if(x+1>=0&&x+1<4&&y>=0&&y<4)
{
gz(x+1,y);
}
if(x-1>=0&&x-1<4&&y>=0&&y<4)
{
gz(x-1,y);
}
if(x>=0&&x<4&&y+1>=0&&y+1<4)
{
gz(x,y+1);
}
if(x>=0&&x<4&&y-1>=0&&y-1<4)
{
gz(x,y-1);
}
}//fan()函数和gz()函数是来翻棋子的
int dfs(int x,int y,int t)
{
int bx,by;
if(pd())
{
if(num>t)
num=t;
return 0;
}
if(x>=4||y>=4)
{
return 0;
}
bx=(x+1)%4;
by=y+(x+1)/4;
dfs(bx,by,t);
fan(x,y);
dfs(bx,by,t+1);
fan(x,y);//第一个dfs()走到最末端,然后递归回去,第二个根据这个区间
//从后往前递归
return 0;
}
int main()
{
int i,j;
for(i=0;i<4;i++)
{
cin>>mp1[i];
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(mp1[i][j]=='b')
{
mp2[i][j]=1;
}
else
{
mp2[i][j]=0;
}
}
}
dfs(0,0,0);
if(num==INT_MAX)
{
cout<<"Impossible\n";
}
else
{
cout<<num<<endl;
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 29494 | Accepted: 12762 |
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
第一次用c++写程序...
#include <iostream>
#include <cstdlib>
#include <string>
#include <climits>
using namespace std;
char mp1[4][4];
int mp2[4][4];
int num=INT_MAX;
int pd()
{
int i,j,js=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(mp2[i][j]==1)
{
js++;
}
}
}
if(js==16||js==0)
{
return 1;
}
return 0;
}//此函数判断是否成为全白色或是全黑色
void gz(int a,int b)
{
if(mp2[a][b]==1)
{
mp2[a][b]=0;
}
else
{
mp2[a][b]=1;
}
}
void fan(int x,int y)
{
gz(x,y);
if(x+1>=0&&x+1<4&&y>=0&&y<4)
{
gz(x+1,y);
}
if(x-1>=0&&x-1<4&&y>=0&&y<4)
{
gz(x-1,y);
}
if(x>=0&&x<4&&y+1>=0&&y+1<4)
{
gz(x,y+1);
}
if(x>=0&&x<4&&y-1>=0&&y-1<4)
{
gz(x,y-1);
}
}//fan()函数和gz()函数是来翻棋子的
int dfs(int x,int y,int t)
{
int bx,by;
if(pd())
{
if(num>t)
num=t;
return 0;
}
if(x>=4||y>=4)
{
return 0;
}
bx=(x+1)%4;
by=y+(x+1)/4;
dfs(bx,by,t);
fan(x,y);
dfs(bx,by,t+1);
fan(x,y);//第一个dfs()走到最末端,然后递归回去,第二个根据这个区间
//从后往前递归
return 0;
}
int main()
{
int i,j;
for(i=0;i<4;i++)
{
cin>>mp1[i];
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(mp1[i][j]=='b')
{
mp2[i][j]=1;
}
else
{
mp2[i][j]=0;
}
}
}
dfs(0,0,0);
if(num==INT_MAX)
{
cout<<"Impossible\n";
}
else
{
cout<<num<<endl;
}
return 0;
}
相关文章推荐
- poj 1753 : Flip Game (枚举+dfs)
- 1753 poj Flip Game 据说是枚举,我这种DFS算是枚举不?
- 文章标题 POJ 1753 : Flip Game(枚举+dfs)
- POJ 1753 FLIP GAME——枚举深度的dfs
- poj 1753 : Flip Game (枚举+dfs)
- POJ 1753 Flip Game 暴力搜索(dfs加枚举)
- Flip Game【POJ--1753】【枚举】【高斯消元】
- poj Flip Game(DFS)(枚举)
- poj Flip Game(DFS)(枚举)
- 【状态压缩枚举】poj 1753 Flip Game
- poj Flip Game(DFS)(枚举)
- 枚举 POJ 1753 Flip Game
- poj Flip Game(DFS)(枚举)
- poj Flip Game(DFS)(枚举)
- poj Flip Game(DFS)(枚举)
- poj Flip Game(DFS)(枚举)
- poj Flip Game(DFS)(枚举)
- POJ1753 Flip Game(搜索,枚举,位运算压缩)
- DFS.枚舉::poj1753 flip game && poj2965 the Pilots Brothers'refrigerator
- POJ 1753 Flip Game(暴力+DFS)