pku暑期训练3filp game
2016-07-25 19:35
253 查看
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.
输入
The input consists of 4 lines with 4 characters “w” or “b” each that denote game field position.
输出
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).
样例输入
bwwb
bbwb
bwwb
bwww
样例输出
4
http://bailian.openjudge.cn/practice/1753/
枚举所有状态,直至找到目标状态,而且由于只需要输出该种状态所在树的深度
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.
输入
The input consists of 4 lines with 4 characters “w” or “b” each that denote game field position.
输出
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).
样例输入
bwwb
bbwb
bwwb
bwww
样例输出
4
http://bailian.openjudge.cn/practice/1753/
枚举所有状态,直至找到目标状态,而且由于只需要输出该种状态所在树的深度
#include<iostream> #include<cstdio> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<string> #include<cmath> #include<set> #include<map> #include<vector> using namespace std; typedef long long LL; const int INF = 1E9; const int maxn = 5 + 5; const int dx[] = {1,0,-1,0,0}; const int dy[] = {0,-1,0,1,0}; int ditu[maxn][maxn]; int step; bool flag; struct node { void init() { memset(ditu,0,sizeof(ditu)); for(int i =0;i<4;i++) { for(int j=0;j<4;j++) { char temp; cin>>temp; if(temp=='b') { ditu[i][j] = 1; } } } } bool panduan() { for(int i =0;i<4;i++) { for(int j =0;j<4;j++) { if(ditu[i][j]!=ditu[0][0]) { return false; } } } return true; } void fz(int row,int col) { for(int i =0;i<5;i++) { int xx = dx[i] + row; int yy = dy[i] + col; if(xx>=0&&xx<4&&yy>=0&&yy<4) { ditu[xx][yy] = !ditu[xx][yy]; } } } void dfs(int row, int col ,int deep) { if(deep == step) { flag = panduan(); return ; } if(row==4||flag) return; fz(row,col); if(col<3) { dfs(row,col+1,deep+1); } else { dfs(row+1,0,deep+1); } fz(row,col); if(col<3) { dfs(row,col+1,deep); } else { dfs(row+1,0,deep); } } void solve() { init(); for(step = 0;step<=16;step++) { dfs(0,0,0); if(flag) break; } if(flag) cout<<step<<endl; else cout<<"Impossible"<<endl; } }; node jiejue; int main() { jiejue.solve(); return 0 ; }
相关文章推荐
- java的序列化和反序列化
- 使用MYSQL命令直接导入导出SQL文件
- 特效1: 鼠标移动到该区域则显示小图标
- java中位运算问题
- JAVA之线程
- [编写高质量iOS代码的52个有效方法](四)接口与API设计(上)
- Lua闭包的详解
- Servlet
- 使用XCode6.0.1将Cocos2d-x3.1工程打包ipa(支持iOS8)
- Android开源框架Universal-Image-Loader详解
- poj 2411 Mondriaan's Dream
- 网卡配置文件生效
- java的this关键字
- HDU 4431 Mahjong(天津赛区亚洲区) 模拟题,方法很重要
- poj 1996多项式计算
- Hibernate 对象的三种状态 持久状态 临时状态 游离状态
- python3.5安装lxml库
- 选择排序
- HDOJ 1896 Stones(优先队列)
- ubuntu系统下更新jdk版本