位运算-BFS-Flip Game-POJ 1753
来源:互联网 发布:淘宝客推广的效果 编辑:程序博客网 时间:2024/05/17 08:20
Description
- 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
Output
Sample Input
bwwbbbwbbwwbbwww
Sample Output
4
给十六个格子标上序号
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
便可以采用二进制记录不同的状态
0 1 0 0 0 1 1 0 0 0 1 1 0 1 1 1
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
此时便记录下了
14 10 9 5 4 2 1 0
最后采用十进制保存此时的状态 1表示‘b’,0表示‘w’,再做相应的翻转,既0 -> 1, 1 -> 0
#include<iostream>#include<cstring>using namespace std;char ma[5][5];//用于输入int states[65535]={0};//(核心部分)记录状态因为最大为2^16-1=65535所以数组大小为65535int step[65535]={0};//记录步数int rear=0,front=0;//记录头与尾bool vis[65535];//用于记录是否搜过//输入,并记录初始状态,放入队列首位置void shuru(){ int i,state=0; for(i=0;i<4;i++){ cin>>ma[i]; int j; for(j=0;j<4;j++) if(ma[i][j]=='b') state|=1<<(i*4+j); } states[rear++]=state;//记录初始状态放入队列首 vis[state]=true;//记录已走过}//反转一个,并产生影响int fanzhuan(int stat,int i){ int state=0; state|=1<<i; if(i%4!=0)state|=1<<(i-1); if((i+1)%4!=0)state|=1<<(i+1); if(i-4>=0)state|=1<<(i-4); if(i+4<16)state|=1<<(i+4); return (state^stat);}//BFS宽度遍历,搜索每种状态,不断放入队列中,找到后立即返回真(true)bool bfs(){ while(front<rear) { int state=states[front++];//取出队列头,记录此时状态 if(0==state||65535==state)//一旦找到,立即返回 { cout<<step[state]<<"\n"; return true; } int i; for(i=0;i<16;i++)//遍历每种状态 { int st; st=fanzhuan(state,i); if(!vis[st])//防止重复遍历 { states[rear++]=st;//不可以的加到队列尾部 vis[st]=true; step[st]=step[states[front-1]]+1;//步数加1 } } } return false;}int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); shuru(); if(!bfs())cout<<"Impossible\n"; return 0;}
- POJ 1753 Flip Game ------- BFS & 位运算
- 位运算-BFS-Flip Game-POJ 1753
- poj 1753 Flip Game(位运算+bfs)
- 【BFS|位运算】POJ-1753 Flip Game
- POJ Flip Game(BFS + 位运算)
- poj 1753 Flip Game (bfs+bit位运算 ---好题!)
- [ACM] POJ 1753 Flip Game (枚举,BFS,位运算)
- POj 1753--Flip Game(位运算+BFS)
- POJ 1753 - Flip Game (位运算 + BFS)
- [ACM] POJ 1753 Flip Game (枚举,BFS,位运算)
- POJ 1753 Flip Game(bfs+状态压缩+位运算)
- POJ 1753 Flip Game ------- DFS & 位运算 .
- [bfs] poj 1753 Flip Game# bfs + 位存储
- poj1753 Flip Game BFS+位运算
- #POJ1753#Flip Game(位运算+BFS)
- POJ 1753 Flip Game BFS+位压缩状态
- POJ-1753 Flip Game【枚举+BFS+位存储】
- POJ 1753 Flip Game BFS
- Android系统的改进(一)-- Lock屏幕功能
- 结束也意味着新的开始
- 好书推荐
- 关于html和javascript在浏览器中的加载顺序问题的讨论(zz)
- 线性空间引论(第2版)
- 位运算-BFS-Flip Game-POJ 1753
- iOS开发中的单元测试(一)& 二
- 内存对齐与ANSI C中struct型数据的内存布局
- DateTime计算年龄
- 代理模式
- Discuz!X/数据库操作方法
- 自定义BroadcastReceiver接收器
- nyoj 93 汉诺塔(三)
- svn里checkout depth各选项的具体含义是什么