POJ-2965-The Pilots Brothers' refrigerator
来源:互联网 发布:单片机脉冲信号发生器 编辑:程序博客网 时间:2024/06/05 11:04
跟POJ的1753很像,只是题目一些条件改了, 首先应该将所有都变成+号,而且改变当前位置的状态,所在行和列都要相反转化。
同样BFS可以求解。
代码:
#include<cstdio>#include<cstring>#include<iostream>#include<queue>#include<string>const int MAX=(1<<16)+1;const int res=(1<<16)-1;using namespace std;bool vis[MAX];int movex[MAX],movey[MAX];struct node{ int num; int ans; int x,y; int pre;}q[MAX];int main(){ char str[4][4]; while(scanf("%s",&str[0])!=EOF) {for(int i=1;i<4;i++) scanf("%s",str[i]);memset(vis,0,sizeof(vis));node ita;ita.num=0;ita.ans=0;for(int i=0;i<4;i++) for(int j=0;j<4;j++)if(str[i][j]=='-') ita.num+=(1<<(i*4+j));if(ita.num==res){ printf("0\n"); continue;}int last=1,pre=0;q[0]=ita;vis[ita.num]=1;int ans=-1;while(pre<last){ ita=q[pre++]; for(int i=0;i<16;i++) {node itb=ita;itb.num=(itb.num^(1<<i));for(int j=i%4;j<16;j+=4) itb.num=(itb.num^(1<<j));int itc=i/4*4;for(int j=itc;j<itc+4;j++) itb.num=(itb.num^(1<<j));if(vis[itb.num]) continue;itb.ans=ita.ans+1;itb.x=i/4+1;itb.y=i%4+1;itb.pre=pre-1;if(itb.num==res){ int cou=0,p=itb.pre; printf("%d\n",ita.ans+1); movex[cou]=itb.x; movey[cou++]=itb.y; for(int j=0;j<ita.ans;j++) {movex[cou]=q[p].x;movey[cou++]=q[p].y;p=q[p].pre; } for(int j=cou-1;j>=0;j--)printf("%d %d\n",movex[j],movey[j]); ans=1; break;}itb.ans=ita.ans+1;itb.x=i/4+1;itb.y=i%4+1;itb.pre=pre-1;q[last++]=itb;vis[itb.num]=1; } if(ans!=-1)break;} } return 0;}
- poj 2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator
- poj 2965The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator 枚举
- POJ-2965-The Pilots Brothers' refrigerator
- poj 2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator
- poj 2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator
- POJ-2965-The Pilots Brothers' refrigerator
- poj 2965 The Pilots Brothers' refrigerator
- POJ 2965:The Pilots Brothers' refrigerator
- Poj 2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator(枚举)
- poj 2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator 枚举
- POJ-2965-The Pilots Brothers' refrigerator
- 无net,纯API添加管理员用户--VC
- [Python入门及进阶笔记]Python-基础-字典小结
- 做事必须搞清10个顺序
- 35岁前要做的7件与工作相关的事
- UILabel中的文字顶端对齐
- POJ-2965-The Pilots Brothers' refrigerator
- ASP.NET与JS交互
- linux 下 自动登陆 SFTP 服务器 下载文件
- 《python源码剖析》之实现small python
- 周赛字符串总结
- asp.net web服务程序输出xml格式文档
- Hoj 2385 Cube Stacking
- 编程之旅 - - - 记录自己的小白历程001
- 字符串问题