POJ:1077 Eight(双向BFS+判重)
来源:互联网 发布:九州风神知乎 编辑:程序博客网 时间:2024/05/13 13:24
HDU也有这道题但是数据比较强,我的代码过不了(内存超限了。。)。只能过POJ的。。
这是某大神的。。
http://www.cnblogs.com/goodness/archive/2010/05/04/1727141.html
我的代码。。
用了stl这是速度慢的一个重要原因。。
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<map>#include<queue>#include<algorithm>using namespace std;struct State{ int grid[5][5]; int x,y; string path;};int Convers(State t){ int n=0; for(int i=0; i<3; ++i) for(int j=0; j<3; ++j) n=n*10+t.grid[i][j]; return n;}int M[5][3]= {{-1,0},{1,0},{0,-1},{0,1}};map<int,bool> mp1,mp2;map<int,string> ph1,ph2;int main(){ char str[100]; while(gets(str)) { State st; for(int i=0,j=0; str[i]; ++i) { if(isdigit(str[i])) { st.grid[j/3][j%3]=str[i]-'0'; j++; } else if(str[i]=='x') { st.grid[j/3][j%3]=0; st.x=j/3; st.y=j%3; j++; } } mp1.clear(); mp2.clear(); ph1.clear(); ph2.clear(); mp1[Convers(st)]=true; bool ok=false; string ans; if(Convers(st)!=123456780) { queue<State> q1,q2; q1.push(st); State ed; for(int i=0; i<8; ++i) ed.grid[i/3][i%3]=i+1; ed.grid[2][2]=0; ed.x=2; ed.y=2; q2.push(ed); mp2[Convers(ed)]=true; bool a1=false,a2=false; while(!q1.empty()||!q2.empty()) { if(ok) break; State t1,t2; if(!q1.empty()) { t1=q1.front(); q1.pop(); } else a1=true; if(!q2.empty()) { t2=q2.front(); q2.pop(); } else a2=true; for(int i=0; i<4&&!ok&&!a1; ++i) { State t=t1; t.x+=M[i][0]; t.y+=M[i][1]; if(t.x<0||t.x>3||t.y<0||t.y>3) continue; swap(t.grid[t.x][t.y],t.grid[t1.x][t1.y]); int flag=Convers(t); if(mp1[flag]) continue; else { if(i==0) t.path+='u'; else if(i==1) t.path+='d'; else if(i==2) t.path+='l'; else if(i==3) t.path+='r'; if(mp2[flag]) { ans=t.path+ph2[flag]; ok=true; } else { mp1[flag]=true; ph1[flag]=t.path; q1.push(t); } } } for(int i=0; i<4&&!ok&&!a2; ++i) { State t=t2; t.x+=M[i][0]; t.y+=M[i][1]; if(t.x<0||t.x>3||t.y<0||t.y>3) continue; swap(t.grid[t.x][t.y],t.grid[t2.x][t2.y]); int flag=Convers(t); if(mp2[flag]) continue; else { if(i==0) t.path='d'+t.path; else if(i==1) t.path='u'+t.path; else if(i==2) t.path='r'+t.path; else if(i==3) t.path='l'+t.path; if(mp1[flag]) { ans=ph1[flag]+t.path; ok=true; } else { mp2[flag]=true; ph2[flag]=t.path; q2.push(t); } } } } } else ok=true; if(ok) cout<<ans<<endl; else cout<<"unsolvable"<<endl; } return 0;}
- POJ:1077 Eight(双向BFS+判重)
- poj 1007 双向bfs+set判重
- POJ 1077 Eight(康拓展开 BFS 双向BFS)
- HDU 1043 && POJ 1077 Eight bfs || 双向bfs || A*搜索
- poj 3131 Cubic Eight-Puzzle 双向广搜 Hash判重
- poj 1077 Eight (BFS)
- POJ 1077 Eight BFS -
- UVA 704 Colour Hash (双向bfs + 判重)
- HDU 1043 Eight ((八数码问题)逆向BFS + 康托定理判重)
- poj 3897 Maze Stretching (二分+bfs+二维判重)
- POJ 3414 Pots(bfs + 判重 + 输出路径)
- poj 1077 Eight(BFS + 康托判重)
- POJ 1077 Eight(BFS Hash)
- HDU 1430 魔板 (双向BFS)/(BFS预处理)+map判重
- poj 3131 双向搜索+hash判重
- POJ Holedox Moving BFS hash判重
- POJ 1465 Multiple BFS + 余数判重
- POJ 2046 Gap(BFS+hash判重)
- 自己给自己定的学习安排
- dhtmlxGrid初始化加载,jquery请求加载数据
- 求1/3-3/5+5/7-7/9…+19/21的值
- kmp
- UIPageControl属性
- POJ:1077 Eight(双向BFS+判重)
- 设计模式1:单例模式(考虑多线程的情况)
- 数学之美番外篇:平凡而又神奇的贝叶斯方法
- 最小距离之和-中位数
- 服务器远程管理简介(ILO,BMC,RSA)
- FZU 2125 简单的等式 枚举解方程式
- linux 下文件夹的用处
- http
- Could not resolve archetype org.apache.maven.archetypes:maven-archetype-quickstart