poj1184 聪明的打字员(BFS剪枝)
来源:互联网 发布:java构造方法例子 编辑:程序博客网 时间:2024/05/16 07:56
http://poj.org/problem?id=1184
用字符串s存下数字,并把光标位置做一个字符加到s末尾,用map做标记状态是否出现过,然后bfs即可。
不剪枝是过不了的,考虑的两种交换操作都不涉及到2,3,4,5位置,所以这几个位置只能通过up,down来改变。
如果这几个位置是和目标状态是不一样的,那肯定是用up,down操作是最优的,而不会执行left,right操作。
所以这几个位置只有在和目标状态一样时做left,right操作。
#include <iostream>#include <map>#include <string> #include <queue> using namespace std;struct point{int step;string s;};string e;map<string,int> my;queue<point> q;int BFS(point st){point t,tt; string ss; int i;while(!q.empty()) q.pop();q.push(st); my[st.s]=1;while(!q.empty()){t=q.front(); q.pop();for(ss=t.s,i=0;i<6;i++)if(ss[i]!=e[i]) break;if(i==6) return t.step;ss=t.s; swap(ss[0],ss[ss[6]-'0']);//Swap0:if(!my.count(ss)){tt.s=ss; tt.step=t.step+1;q.push(tt); my[ss]=1;}ss=t.s; swap(ss[5],ss[ss[6]-'0']); //Swap1if(!my.count(ss)){tt.s=ss; tt.step=t.step+1;q.push(tt); my[ss]=1;}ss=t.s; if(ss[ss[6]-'0']!='9'&&ss[ss[6]-'0']!=e[ss[6]-'0']) ss[ss[6]-'0']+=1; //Up:if(!my.count(ss)){tt.s=ss; tt.step=t.step+1;q.push(tt); my[ss]=1;}ss=t.s; if(ss[ss[6]-'0']!='0'&&ss[ss[6]-'0']!=e[ss[6]-'0']) ss[ss[6]-'0']-=1;//Down: if(!my.count(ss)){tt.s=ss; tt.step=t.step+1;q.push(tt); my[ss]=1;}ss=t.s; if(ss[6]-'0'!=0)//left{if(ss[6]!='5') //1 2 3 4位置相同才能移动光标 {if(ss[ss[6]-'0']==e[ss[6]-'0']) ss[6]-=1; }else ss[6]-=1;}if(!my.count(ss)){tt.s=ss; tt.step=t.step+1;q.push(tt); my[ss]=1;}ss=t.s; if(ss[6]-'0'!=5)//Right{if(ss[6]!='0') //1 2 3 4位置相同才能移动光标 {if(ss[ss[6]-'0']==e[ss[6]-'0']) ss[6]+=1; }else ss[6]+=1;}if(!my.count(ss)){tt.s=ss; tt.step=t.step+1;q.push(tt); my[ss]=1;}}}int main(int argc, char *argv[]){point st;while(cin>>st.s>>e){my.clear();st.s+='0'; st.step=0;cout<<BFS(st)<<endl;}return 0;}
- poj1184 聪明的打字员(BFS剪枝)
- poj1184 聪明的打字员 BFS+剪枝
- poj1184 聪明的打字员 (bfs)
- poj1184聪明的打字员(操作分离+bfs)
- poj1184 聪明的打字员
- poj1184 聪明的打字员
- poj1184 聪明的打字员
- 聪明的打字员POJ1184
- poj1184 聪明的打字员(双向搜索)
- POJ 1184 聪明的打字员 (BFS + 剪枝)
- poj 1184 聪明的打字员 (操作分离缩小状态+bfs)
- [BFS 排列Hash] POJ 1184 聪明的打字员
- 聪明的打字员
- POJ 聪明的打字员
- 聪明的打字员
- 【NOI2001】聪明的打字员
- poj 1184 聪明的打字员
- POJ 1184 聪明的打字员
- 两分钟让你明白cocos2dx的屏幕适配策略
- 未约定经济补偿的竞业禁止条款是否无效
- AIX 下的 core dump 分析入门
- 九、android的ListView实现数据列表展示
- HTML基础之<head>篇
- poj1184 聪明的打字员(BFS剪枝)
- android无线adb开发调试
- 五大常用算法之分治法
- HTML基础之<head>篇
- toj3479 Selfish Grazing
- 驱动中如何给ring3层应用程序提权
- 一道绝妙的查找题
- EclipseRCP开发2-建立菜单
- HTML基础之<head>篇