枚举(黑白棋)
来源:互联网 发布:android基站定位源码 编辑:程序博客网 时间:2024/04/29 02:30
/*代码一:DFS+Enum*///Memory Time //240K 344MS //本题只要求输出翻转的次数,因此BFS或DFS都适用#include<iostream>using namespace std;bool chess[6][6]={false};//利用的只有中心的4x4bool flag;int step;int r[]={-1,1,0,0,0};//便于翻棋操作int c[]={0,0,-1,1,0};bool judge_all(void)//判断“清一色”{ int i,j; for(i=1;i<5;i++) for(j=1;j<5;j++) if(chess[i][j]!=chess[1][1]) return false; return true;}void flip(int row,int col)//翻棋{ int i; for(i=0;i<5;i++) chess[row+r[i]][col+c[i]]=!chess[row+r[i]][col+c[i]]; return;}void dfs(int row,int col,int deep) //深搜的迭代回溯是重点,很容易混乱{ if(deep==step) { flag=judge_all(); return; } if(flag||row==5)return; flip(row,col); //翻棋 if(col<4) dfs(row,col+1,deep+1); else dfs(row+1,1,deep+1); flip(row,col); //不符合则翻回来 if(col<4) dfs(row,col+1,deep); else dfs(row+1,1,deep); return;}int main(void){ char temp; int i,j; for(i=1;i<5;i++) for(j=1;j<5;j++) { cin>>temp; if(temp=='b') chess[i][j]=true; } for(step=0;step<=16;step++) //对每一步产生的可能性进行枚举 { //至于为什么是16,考虑到4x4=16格,而每一格只有黑白两种情况,则全部的可能性为2^16 dfs(1,1,0); if(flag)break; } if(flag) cout<<step<<endl; else cout<<"Impossible"<<endl; return 0;}
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).
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
0 0
- 枚举(黑白棋)
- 黑白棋(game)
- 黑白棋(博弈论)
- 黑白棋(落子)
- 黑白棋
- 黑白棋
- 黑白棋
- 黑白棋
- 黑白棋
- 黑白棋
- 黑白棋
- 黑白棋
- 黑白棋
- 黑白棋
- 黑白棋
- 黑白棋
- 黑白棋
- 黑白棋
- poj-1022(四维模仿)(转)
- poj-1024(唯一最短路径)(转)
- poj-1069(三角形和六边形)(转)
- Spark技术实战之1--KafkaWordCount
- hdu-(1298手机相关 dfs+字典树)
- 枚举(黑白棋)
- POJ3253-Fence Repair-贪心-哈夫曼树
- 自制FFmpeg SDK并使用output_example测试
- Android:如何设计一个完全不可见的Activity?
- 枚举(+-)
- Java二分查找算法
- 贪心
- 贪心(盈利问题)
- 构造发