POJ基本算法bfs1753,2965
来源:互联网 发布:linux yes命令 编辑:程序博客网 时间:2024/05/19 13:26
POJ1753
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
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;bool vis[65540];int p[65540],v[65540],ope[16],l,r;int bfs(int ans){ if(ans==0||ans==(1<<16)-1) return 0; memset(vis,0,sizeof(vis)); l=r=0; vis[ans]=1,p[r++]=ans,v[0]=0; while(l<r){ for(int i=0;i<16;i++){ int num=p[l]^ope[i]; if(vis[num]) continue; if(num==0||num==(1<<16)-1) return v[l]+1; v[r]=v[l]+1; p[r++]=num; vis[num]=1; } l++; } return -1;}int main(){char s[4][5]; for(int i=0;i<4;i++) for(int j=0;j<4;j++){ int num=4*i+j; ope[num]=1<<num; if(i-1>=0) ope[num]+=1<<(num-4); if(j-1>=0) ope[num]+=1<<(num-1); if(j+1<4) ope[num]+=1<<(num+1); if(i+1<4) ope[num]+=1<<(num+4); } scanf("%s%s%s%s",s[0],s[1],s[2],s[3]); int ans=0; for(int i=3;i>=0;i--) for(int j=3;j>=0;j--){ ans<<=1; if(s[i][j]=='b') ans++; } int a=bfs(ans); if(a==-1) printf("Impossible\n"); else printf("%d\n",a);return 0;}
POJ2965
Description
The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator.
There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location [i, j] (1 ≤ i, j ≤ 4). However, this also changes states of all handles in row i and all handles in column j.
The task is to determine the minimum number of handle switching necessary to open the refrigerator.
Input
The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “−” means “open”. At least one of the handles is initially closed.
Output
The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.
Sample Input
-+-----------+--
Sample Output
61 11 31 44 14 34 4
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;bool vis[65540];struct op{ int a[16];}h[65540];//操作过程int p[65540],v[65540],ope[16],l,r;//节点 步数 操作int bfs(int ans){ if(!ans) return 0; l=r=0; for(int i=0;i<16;i++) h[0].a[i]=0; vis[ans]=1; v[r]=0; p[r++]=ans; while(l<r){ for(int i=0;i<16;i++){ int num=ope[i]^p[l]; if(!vis[num]){ vis[num]=1; v[r]=v[l]+1; h[r]=h[l]; h[r].a[v[r]]=i; p[r++]=num; if(!num) return r-1; } } l++; } return -1;}int main(){char s[4][5];int x[4],y[4];x[0]=(1<<4)-1;y[0]=1+(1<<4)+(1<<8)+(1<<12);for(int i=1;i<4;i++) x[i]=x[i-1]<<4,y[i]=y[i-1]<<1; for(int i=0;i<4;i++) for(int j=0;j<4;j++) ope[4*i+j]=x[i]|y[j]; while(scanf("%s%s%s%s",s[0],s[1],s[2],s[3])!=EOF){ int ans=0; for(int i=3;i>=0;i--) for(int j=3;j>=0;j--){ ans<<=1; if(s[i][j]=='+') ans++; } int n=bfs(ans); printf("%d\n",v[n]); for(int i=1;i<=v[n];i++) { int mid=h[n].a[i]; printf("%d %d\n",mid/4+1,mid%4+1); } }return 0;}bfs,第i个状态得来的操作步骤存储在h[i]的op数组里。(最多不会超过16个操作步骤)
- POJ基本算法bfs1753,2965
- poj初期基本算法
- 数论中的基本算法 POJ 1845 SPOJ DIVSUM
- 初级->基本算法->模拟 poj 2632 Crashing Robots
- 初级->基本算法->模拟 poj 1573 Robot Motion
- 初级->基本算法->构造法 poj 3295 Tautology(永真式)
- 基本算法dijkstra的POJ水题推荐
- 基本算法floyd的poj水题推荐
- POJ 1273 两种基本最大流算法
- poj 1459 Power Network 初级->图算法->最大流(基本算法:增广路)
- 基本算法
- 基本算法
- 基本算法
- 基本算法
- 基本算法
- 基本算法
- 基本算法
- 基本算法
- JavaScript入门-第1章 请做好准备
- [BZOJ3670]NOI2014动物园|KMP
- 判断应用程序.exe及动态链接库.dll是32位还是64位
- Linux文件权限详解(一)
- UVa1152 - 4 Values whose Sum is 0(hash)
- POJ基本算法bfs1753,2965
- [BZOJ2875]NOI2012随机数生成器|矩阵乘法
- 对于重定位我该如何理解??
- Hadoop中使用正则匹配的效率问题
- 关于 java.util.concurrent.RejectedExecutionException (2012-07-03 22:36:38)
- 机器学习问题方法总结
- 图的拓扑排序
- web前端优化
- [BZOJ3991]SDOI2015寻宝游戏|set|虚树