POJ 1753 Flip Game (DFS)
2015-01-21 20:27
495 查看
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).
View Code
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 32104 | Accepted: 13988 |
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).
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<stdlib.h> #include<queue> #include<stack> #include<algorithm> #define LL __int64 using namespace std; const int MAXN=5+5; const int INF=0x3f3f3f3f; const double EPS=1e-9; int dir4[][2]={{0,1},{1,0},{0,-1},{-1,0}}; int dir8[][2]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}}; int dir_8[][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}}; int map[6][6],step; bool flag; char temp; 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 turn(int x,int y) { map[x][y]=!map[x][y]; for(int i=0;i<4;i++) { int xx=x+dir4[i][0]; int yy=y+dir4[i][1]; map[xx][yy]=!map[xx][yy]; } } void DFS(int x,int y,int cur) { if(cur==step) { flag=judge(); return ; } if(x==4||flag) return ; turn(x,y); if(y==3) DFS(x+1,0,cur+1); else DFS(x,y+1,cur+1); turn(x,y); if(y==3) DFS(x+1,0,cur); else DFS(x,y+1,cur); return ; } int main() { //freopen("in.txt","r",stdin); for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { scanf("%c",&temp); if(temp=='b') map[i][j]=1; } getchar(); } flag=false; for(step=0;step<=16;step++) { DFS(0,0,0); if(flag) break; } if(flag) cout<<step<<endl; else cout<<"Impossible"<<endl; }
View Code
相关文章推荐
- POJ 1753 Flip Game(DFS)
- poj 1753 Flip Game【dfs】
- POJ 1753 Flip Game ------- DFS & 位运算 .
- POJ 1753 Flip Game(类似与N皇后问题的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 + 枚举)
- 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】
- 【dfs】poj1753——Flip Game
- poj_1753 Flip Game(dfs+枚举)/(bfs+位运算)