POJ 1753 Flip Game
来源:互联网 发布:淘宝答题红包有限制 编辑:程序博客网 时间:2024/06/07 01:28
Flip Game
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 26252 Accepted: 11333
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
Source
Northeastern Europe 2000
广搜(记忆化搜索)+状态压缩
#include<cstdio>#include<cstring>#include<queue>#include<iostream>using namespace std;char ch[100];bool hash[1<<17];int d[1<<17];queue<int> Que;void BFS(){ int tmp,x; memset(d,-1,sizeof(d)); memset(hash,0,sizeof(hash)); while(!Que.empty()) Que.pop(); hash[0]=1; d[0]=0; Que.push(0); while(!Que.empty()) { x=Que.front(); Que.pop(); for(int i=0;i<4;i++) for(int j=0;j<4;j++) { tmp=x^(1<<i*4+j); if(i>0) tmp^=(1<<(i-1)*4+j); if(i<3) tmp^=(1<<(i+1)*4+j); if(j>0) tmp^=(1<<i*4+j-1); if(j<3) tmp^=(1<<i*4+j+1); if(!hash[tmp]) { hash[tmp]=1; Que.push(tmp); d[tmp]=d[x]+1; } } }}int main(){ int x,y,i,j; BFS(); scanf("%s",ch); x=0; for(i=0;i<4;i++) if(ch[i]=='b') x|=(1<<i); for(i=1;i<4;i++) { scanf("%s",ch); for(j=0;j<4;j++) if(ch[j]=='b') x|=(1<<i*4+j); } y=x^((1<<16)-1); if(d[x]==-1) d[x]=1<<25; if(d[y]==-1) d[y]=1<<25; int ans=min(d[x],d[y]); if(ans==1<<25) printf("Impossible\n"); else printf("%d\n",ans); return 0;}
- poj 1753 Flip Game
- poj 1753 Flip Game
- poj 1753 Flip Game
- poj 1753 Flip Game
- POJ 1753 Flip Game
- poj 1753 Flip Game
- poj 1753 Flip Game
- POJ-1753-Flip Game
- POJ 1753: Flip Game
- POJ 1753 Flip Game
- POJ 1753 Flip Game
- poj 1753 Flip Game
- poj 1753 Flip Game
- POJ 1753 Flip Game
- POJ-1753-Flip Game
- Poj 1753 Flip Game
- POJ 1753 Flip Game
- POJ-1753-Flip Game
- maven 与 eclipse 结合运用 创建web项目
- 常见面试题
- java笔试题---杂记
- 数码管的十位静态显示和动态显示
- cxGrid入门级教程
- POJ 1753 Flip Game
- Linux 下MySQL的基本命令
- 7.0
- 博客要搬家了
- hadoop细节---Mapreduce过程详解
- .模拟状态为active的日志损坏的数据恢复实验(不完全恢复)
- [light oj 1032]Fast Bit Calculations[数位DP]
- jpa之hibernate4.2.4之双向关联之一对一、一对多、多对多
- 【HZOI】 排队