POJ 1753 Flip Game(BFS)
来源:互联网 发布:mysql 表空间查看 编辑:程序博客网 时间:2024/04/30 15:07
Flip Game
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 34192 Accepted: 14933
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
一个4*4的棋盘,每次选一个位置,将该位置及其上下左右共5个位置的棋子翻转,问最少翻转多少次可以使16个棋子颜色一样。
由于棋盘已经固定为4*4的大小,所以可以用一个二进制数来表示当前整个棋盘的状态。
如:
bbww
bwbw
bbbb
wwww
可以用二进制数1100 1010 1111 0000表示,转换为十进制就是51952,则vis[51952]=1。然后bfs搜索最小步数即可。
//#pragma comment(linker,"/STACK: 1677721600")#include<queue>#include<cmath>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int MAXN=1e5+50;#define MAX_INT 0x3f3f3fint vis[MAXN];queue<int>q;int change(int num,int n){ num^=1<<n; if(n>=4) num^=1<<(n-4); if(n<=11) num^=1<<(n+4); if(n%4!=0) num^=1<<(n-1); if(n%4!=3) num^=1<<(n+1); return num;}int main(){#ifndef ONLINE_JUDGE freopen("in.txt","r",stdin);#endif // ONLINE_JUDGE int s=0,ans=MAX_INT,cnt=0,scnt=0,last=1,now=0; char t; memset(vis,0,sizeof(vis)); for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { scanf("%c",&t); if(t=='b') s=s*2+1; else s=s*2+0; } getchar(); } vis[s]=1; q.push(s); while(!q.empty()) { int num=q.front(); q.pop(); if(num==0||num==65535) { ans=scnt; printf("%d\n",ans); break; } for(int i=0;i<16;i++) { int after=change(num,i); if(!vis[after]) { vis[after]=1; q.push(after); now++; } } cnt++; if(cnt>=last) { last=now; now=0; cnt=0; scnt++; } } if(ans==MAX_INT) { printf("Impossible\n"); } return 0;}
1 0
- POJ 1753 Flip Game BFS
- POJ 1753 Flip Game(BFS)
- POJ 1753 Flip Game (BFS)
- POJ 1753 Flip Game BFS/DFS
- POJ 1753 Flip Game ------- BFS & 位运算
- 位运算-BFS-Flip Game-POJ 1753
- 【BFS+状态压缩】-POJ-1753-Flip Game
- POJ 1753 Flip Game(bfs)
- poj 1753 Flip Game(位运算+bfs)
- POJ 1753 Flip Game(状态压缩BFS)
- POJ 1753 Flip Game(BFS+状态压缩)
- 【BFS|位运算】POJ-1753 Flip Game
- 【POJ】1753 - Flip Game(bfs,枚举)
- poj 1753 Flip Game(枚举,bfs)(简单)
- (POJ 1753)Flip Game <状压+BFS>
- POJ:1753-Flip Game(二进制+bfs)
- [bfs] poj 1753 Flip Game# bfs + 位存储
- POJ 1753 Flip Game BFS+位压缩状态
- Gym 100712G Heavy Coins(DFS)
- “儿童”计算器
- Gym 100712I Bahosain and Digits(枚举)
- Majority Number II
- 原型模式
- POJ 1753 Flip Game(BFS)
- Scala:基于trait的多重继承构造器的执行顺序、基于trait的AOP实践
- C++中模板类使用友元模板函数
- 20_Android中apk安装器,通过WebView来load进一个页面,Android通知,程序退出自动杀死进程,通过输入包名的方式杀死进程
- linux下apache绑定多个域名
- 设计模式--命令模式
- linux /etc/init.d/ rc*.d 详解
- X命名空间
- viewpager的圆点指示器