POJ训练计划1753_Flip Game(枚举+BFS)
来源:互联网 发布:日本语言翻译软件 编辑:程序博客网 时间:2024/06/06 09:21
Flip Game
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
解体报告
POJ计划第一题。。。
#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <algorithm>#include <cmath>#define inf 65536using namespace std;int num;struct node{ int n,step;};int maxx=inf;int vis[65555];int bfs(){ int i; queue<node >Q; memset(vis,0,sizeof(vis)); node now,next; now.n=num; now.step=0; vis[num]=1; Q.push(now); while(!Q.empty()) { now=Q.front(); Q.pop(); next.n=now.n; for(i=15; i>=0; i--) { next.n=now.n; if(i==15) { next.n^=(int)pow(2,15); next.n^=(int)pow(2,14); next.n^=(int)pow(2,11); } else if(i==12) { next.n^=(int)pow(2,12); next.n^=(int)pow(2,13); next.n^=(int)pow(2,8); } else if(i==0) { next.n^=(int)pow(2,0); next.n^=(int)pow(2,1); next.n^=(int)pow(2,4); } else if(i==3) { next.n^=(int)pow(2,3); next.n^=(int)pow(2,2); next.n^=(int)pow(2,7); } else if(i==1||i==2) { next.n^=(int)pow(2,i+4); next.n^=(int)pow(2,i); next.n^=(int)pow(2,i-1); next.n^=(int)pow(2,i+1); } else if(i==13||i==14) { next.n^=(int)pow(2,i-4); next.n^=(int)pow(2,i); next.n^=(int)pow(2,i-1); next.n^=(int)pow(2,i+1); } else if(i==4||i==8) { next.n^=(int)pow(2,i+1); next.n^=(int)pow(2,i); next.n^=(int)pow(2,i-4); next.n^=(int)pow(2,i+4); } else if(i==7||i==11) { next.n^=(int)pow(2,i-1); next.n^=(int)pow(2,i); next.n^=(int)pow(2,i-4); next.n^=(int)pow(2,i+4); } else { next.n^=(int)pow(2,i+1); next.n^=(int)pow(2,i); next.n^=(int)pow(2,i-1); next.n^=(int)pow(2,i-4); next.n^=(int)pow(2,i+4); } if(next.n==0||next.n==65535) { if(now.step+1<maxx) maxx=now.step+1; } if(!vis[next.n]) { next.step=now.step+1; vis[next.n]=1; Q.push(next); } } } if(maxx==inf) cout<<"Impossible"<<endl; else cout<<maxx;}int main(){ int i,j,k=15; char ch[4]; for(i=0; i<4; i++) { cin>>ch; for(j=0; j<4; j++) { if(ch[j]=='w') num+=(int)pow(2,k--); else k--; } } if(num==0||num==65535) { cout<<"0"<<endl; return 0; } bfs(); return 0;}
0 0
- POJ训练计划1753_Flip Game(枚举+BFS)
- 【POJ】1753 - Flip Game(bfs,枚举)
- poj 1753 Flip Game(枚举,bfs)(简单)
- POJ-1753 Flip Game【枚举+BFS+位存储】
- POJ 1753 Flip Game(bfs枚举+递推)
- [ACM] POJ 1753 Flip Game (枚举,BFS,位运算)
- [ACM] POJ 1753 Flip Game (枚举,BFS,位运算)
- POJ训练计划3414_Pots(BFS)
- POJ 1753 Flip Game BFS
- POJ 1753 Flip Game(BFS)
- POJ 1753 Flip Game (BFS)
- POJ 1753 枚举二进制+BFS
- POJ 1753 Flip Game 枚举
- POJ 1753 Flip Game (枚举)
- POJ 1753 Flip Game 枚举
- poj 1753Flip Game---枚举
- POJ 1753 Flip Game【枚举】
- POJ 1753 Flip Game (枚举)
- python 调用linux系统命令
- 大数据之“用户行为分析”
- jQuery超酷字体显示效果
- shell中(字符串截取)
- Handler详解
- POJ训练计划1753_Flip Game(枚举+BFS)
- C/C++基本数据类型(带测试程序)
- C09_C语言-typedef类型自定义
- 触屏中的js事件
- nutch with hbase
- Maven基础2
- JAVA CAS原理深度分析
- JS定时器例子讲解
- 完成端口与AcceptEx