poj1753 Flip Game DFS,枚举
来源:互联网 发布:我国历年gdp数据 编辑:程序博客网 时间:2024/05/17 23:31
Flip Game
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 34437 Accepted: 15058
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:
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.
- 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
bwwbbbwbbwwbbwww
Sample Output
4
#include<iostream>#include<cstdio>#define INF 0x3f3f3f3fusing namespace std;string s[4];int num=INF;int a[10][10];int panduan(){for(int i=0;i<4;i++)for(int j=0;j<4;j++)if(a[i][j]!=a[0][0])return 0;return 1;}int fanzhuan(int x,int y){a[x][y]=!a[x][y];if(x<3)a[x+1][y]=!a[x+1][y];if(x>0)a[x-1][y]=!a[x-1][y];if(y>0)a[x][y-1]=!a[x][y-1];if(y<3)a[x][y+1]=!a[x][y+1];}int DFS(int x,int y,int ans){if(panduan()){if(num>ans)num=ans;return 0;}if(x>3||y>3)return 0;int fx,fy;fx=(x+1)%4; //按列来翻转fy=(x+1)/4+y; //每个位置都有2种情况 全部跑一遍fanzhuan(x,y); //翻成另一面DFS(fx,fy,ans+1);fanzhuan(x,y); //还原DFS(fx,fy,ans);}int main(){while(cin>>s[0]){for(int i=1;i<4;i++)cin>>s[i];for(int i=0;i<4;i++)for(int j=0;j<4;j++)if(s[i][j]=='w')a[i][j]=1;elsea[i][j]=0;DFS(0,0,0);if(num!=INF)cout<<num;elsecout<<"Impossible";cout<<endl;}}<span id="transmark">DFS+枚举,时间相对少些</span>#include<iostream>#include<cstring>using namespace std;int num=0x3f3f3f3f;int a[13][15],flag;int panduan (){for(int i=0;i<4;i++)for(int j=0;j<4;j++)if(a[i][j]!=a[0][0])return 0;return 1;}int fanzhuan(int x,int y){a[x][y]=!a[x][y];if(x>0)a[x-1][y]=!a[x-1][y];if(x<3)a[x+1][y]=!a[x+1][y];if(y<3)a[x][y+1]=!a[x][y+1];if(y>0)a[x][y-1]=!a[x][y-1];}int DFS(int x,int y,int ans){if(num==ans){flag=panduan();return 0;}if(x>3||y>3||flag)return 0;int fx=(x+1)%4;int fy=(x+1)/4+y; fanzhuan(x,y); //此处顺序不能变DFS(fx,fy,ans+1);fanzhuan(x,y);DFS(fx,fy,ans);return 0;}int main(){ string s[4];for(int i=0;i<4;i++)cin>>s[i];for(int i=0;i<4;i++)for(int j=0;j<4;j++)if(s[i][j]=='w')a[i][j]=1;elsea[i][j]=0;flag=0; for(int i=0;i<=16;i++) //枚举,..最多16次{num=i;DFS(0,0,0);if(flag)break;}if(flag)cout<<num;elsecout<<"Impossible";cout<<endl;}
0 0
- POJ1753 Flip Game DFS+枚举
- poj1753 Flip Game DFS,枚举
- poj1753 Flip Game(枚举Enum+dfs)
- POJ1753--Flip Game(枚举)
- POJ1753 Flip Game【DFS】
- POJ1753 Flip Game (DFS)
- poj1753 Flip Game dfs 枚举 待补完 位运算
- poj1753——Flip Game(枚举+dfs)
- poj1753 Flip Game 枚举+BFS
- POJ1753,Flip Game,枚举,bfs都可以
- poj1753 Flip Game 枚举 暴力 递归
- (枚举初级+回溯)poj1753 Flip Game
- 【dfs】poj1753——Flip Game
- poj Flip game【DFS 枚举】
- Flip Game(DFS枚举)
- poj1753 Flip Game
- poj1753 Flip Game
- poj1753 Flip Game
- 暑假集训第四周阶段二DP A - 最长公共子序列
- JNDI配置c3p0连接池
- hdu-3008魔兽争霸
- [译]Front-end-Developer-Interview-Questions
- 《Objective-C基础教程》第四章 继承
- poj1753 Flip Game DFS,枚举
- Best Time to Buy and Sell Stock II
- [Learning Linux For Fun] unit 1 1.2~1.3
- leetcode_Median of Two Sorted Arrays
- LeetCode(116) Populating Next Right Pointers in Each Node
- 智慧城市
- UE4 移动平台游戏开发
- iOS 9之分屏多任务(Split View)
- POJ 1511 SPFA 模板