POJ-2965-The Pilots Brothers' refrigerator
2013-03-09 19:46
375 查看
跟POJ的1753很像,只是题目一些条件改了, 首先应该将所有都变成+号,而且改变当前位置的状态,所在行和列都要相反转化。
同样BFS可以求解。
代码:
同样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 2965 The Pilots Brothers' refrigerator
- poj 2965 The Pilots Brothers' refrigerator
- poj 2965 The Pilots Brothers' refrigerator (迭代加深dfs)
- poj 2965 The Pilots Brothers' refrigerator(dfs 枚举 +打印路径)
- POJ 2965 The Pilots Brothers' refrigerator(dfs+记录路径)
- POJ 2965 The Pilots Brothers' refrigerator
- ACM--枚举+DFS/巧妙思想--POJ--2965--The Pilots Brothers' refrigerator--初期算法
- POJ 2965 The Pilots Brothers' refrigerator
- 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(dfs+枚举 || 规律)
- 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
- POJ2965,The Pilots Brothers' refrigerator,枚举,DFS