poj 2965 The Pilots Brothers' refrigerator bfs+状态压缩+路径回溯
来源:互联网 发布:xp优化工具2016 编辑:程序博客网 时间:2024/06/08 11:45
此题易超时,因为16个开关,每个开关有两种状态,可以将’-‘看成0,’+’看成1,从第一排第一个到第四排最后一个,每一种状态都可以用一个数字来代替,所以一共是(1<<16)-1种状态,即65535种,bfs还是可以的。在广搜过程中,因为每一个开关扳动的过程其对应的一行及一列都会发生变化,可以将其状态对应的值分别和这些状态对应的数字进行^处理,为了防止TLE,可以提前打个表,这样异或一下就可以了,不用异或7次。
路径回溯可以再开一个结构体,对应每一次搜索都给定一个次序,在结构体里面存两个值,一个对应改变的行列,一个对应状态改变之前的那个次序,这样在广搜搜到最终状态的时候就可以了一步一步回溯到最终的结果。
题目:
The Pilots Brothers’ refrigerator
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 23926 Accepted: 9221 Special Judge
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
6
1 1
1 3
1 4
4 1
4 3
4 4
代码:
#include<stdio.h>#include<string.h>#include<queue>using namespace std;char a[8][8];bool c[100000];int multi[16]= {4383, 8751, 17487,34959, 4593, 8946,17652, 35064, 7953,12066,20292,36744, 61713,61986, 62532,63624 },k=0; //手动打表struct stu{ int x,step,num;} s;struct st{ int x,pre;} d[100001];void present(int num){ if(num) { present(d[num].pre); printf("%d %d\n",(d[num].x)/4+1,(d[num].x)%4+1); }}bool mach(int i,stu t){ s.x=t.x; s.x=s.x^multi[i]; if(!c[s.x]) { s.num=++k; d[k].x=i; d[k].pre=t.num; s.step=t.step+1; c[s.x]=1; return 1; } return 0;}void bfs(){ queue<stu>q; q.push(s); stu t; int i,j; while(!q.empty()) { t=q.front(); q.pop(); if(!t.x) { printf("%d\n",t.step); present(t.num); }//路径回溯 else { for(i=0; i<16; i++) { if(mach(i,t)) q.push(s); } } }}int main(){ int i,j; while(scanf("%s",a[1]+1)!=EOF) { memset(c,0,sizeof(c)); s.x=0,s.step=0,k=0,s.num=0; for(i=2; i<=4; i++) scanf("%s",a[i]+1); for(i=1; i<=4; i++) for(j=1; j<=4; j++) if(a[i][j]=='+') s.x^=(1<<(j-1+(i-1)*4)); //状态压缩 if(s.x) c[s.x]=1; bfs(); }}
- poj 2965 The Pilots Brothers' refrigerator bfs+状态压缩+路径回溯
- BFS + 状态压缩 POJ 2965 The Pilots Brothers' refrigerator
- POJ 2965-The Pilots Brothers' refrigerator(位运算+BFS+回溯路径)
- The Pilots Brothers' refrigerator(POJ 2965)(dfs枚举+状态压缩)
- POJ2965 The Pilots Brothers' refrigerator 「BFS+状态压缩」
- Poj_2965 The Pilots Brothers' refrigerator(状态压缩,bfs)
- POJ 2965 The Pilots Brothers' refrigerator(回溯查找,dfs)
- POJ 2965 The Pilots Brothers' refrigerator BFS+位运算
- POJ 2965 The Pilots Brothers' refrigerator(BFS+二进制判重)
- POJ 2965The Pilots Brothers' refrigerator (bfs)
- POJ-2965-The Pilots Brothers' refrigerator-bfs枚举-位运算
- POJ 2965 The Pilots Brothers' refrigerator【BFS+状压 Or 脑洞】
- poj 2965 The Pilots Brothers' refrigerator (状压+bfs)
- 【BFS|位运算+输出路径】POJ-2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator(dfs+记录路径)
- poj 2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator
- poj 2965The Pilots Brothers' refrigerator
- Xutils
- SSM框架(Spring+SpringMVC+MyBatis)—环境eclipse+maven+jetty
- 软件架构为何重要?---不可取代的专业能力系列(三)
- HDU-5763-Another Meaning(KMP+DP)
- 在命令行中查看python帮助文档 --dir、help、__doc__
- poj 2965 The Pilots Brothers' refrigerator bfs+状态压缩+路径回溯
- PHP 程序员的技术成长规划
- Android 异步加载图片,使用LruCache和SD卡或手机缓存,效果非常的流畅
- leetcode131—Palindrome Partitioning
- 点input框,弹出一个多选框checkbox,进行选择,每个以逗号隔开
- hihocoder #1043 : 完全背包 (DP)
- 根据动态id获取val或者text值
- Android照片墙应用实现,再多的图片也不怕崩溃
- 微信公众号开发教程[019]-新版客服