poj 1475 Pushing Boxes(推箱子游戏,bfs+bfs)
来源:互联网 发布:学编程app 编辑:程序博客网 时间:2024/06/05 15:49
算法思想:
对箱子进行bfs,每移动一次就 对人bfs一次,看能否走到箱子的后面,能走到就加入队列。
个人认为有点繁琐的地方就是路径的保存。
还有,每个case后有一个\n,不然会WA……
#include<cstring>#include<algorithm>#include<queue>#include<cstdio>#include<stack>using namespace std;struct node{ int x,y; int px,py; int d;};int tx,ty;int n,m;int dx[]={-1,1,0,0};int dy[]={0,0,-1,1};int ax[]={1,-1,0,0};int ay[]={0,0,1,-1};char word[]={'n','s','w','e'};int cas;char visb[25][25][4];char visp[25][25];stack<char> ans[10000];int map[25][25];int fa[10000];int head,tail;int pre[10000];char path[10000];bool okmap(int x,int y){ return x>=0&&x<n&&y>=0&&y<m&&map[x][y];}bool okgo(int x,int y,int tarx,int tary,int dir){ if(x==tarx&&y==tary) { ans[tail].push(word[dir]-32); return true; } queue<node> q; node front; front.px=x; front.py=y; q.push(front); node rear; pre[0]=-1; int f=0,e=1; while(!q.empty()) { front=q.front();q.pop(); for(int d=0;d<4;d++) { rear.px=front.px+dx[d]; rear.py=front.py+dy[d]; if(okmap(rear.px,rear.py)&&!visp[rear.px][rear.py]) { visp[rear.px][rear.py]=1; pre[e]=f; path[e]=word[d]; if(rear.px==tarx&&rear.py==tary) { ans[tail].push(word[dir]-32); while(e) { ans[tail].push(path[e]); e=pre[e]; } return true; } e++; q.push(rear); } } f++; } return false;}void print(int k){ if(k==0) return; print(fa[k]); while(!ans[k].empty()) { putchar(ans[k].top()); ans[k].pop(); }}void bfs(int x,int y,int px,int py){ node front; front.x=x;front.px=px; front.y=y;front.py=py; queue<node> q; q.push(front); node rear; head=0,tail=1; while(!q.empty()) { front=q.front();q.pop(); // printf("箱x=%d,y=%d人:x=%d,y=%d\n",front.x,front.y,front.px,front.py); for(int d=0;d<4;d++) { rear.x=front.x+dx[d]; rear.y=front.y+dy[d]; int lx=front.x+ax[d]; //反方向 int ly=front.y+ay[d]; if(okmap(rear.x,rear.y)&&okmap(lx,ly)&&!visb[rear.x][rear.y][d]) { memset(visp,0,sizeof(visp)); visp[front.x][front.y]=1; visp[front.px][front.py]=1; fa[tail]=head; if(okgo(front.px,front.py,lx,ly,d)) {visb[rear.x][rear.y][d]=1; rear.d=d; rear.px=front.x; rear.py=front.y; if(rear.x==tx&&rear.y==ty) { print(tail); return; } tail++; q.push(rear); } } } head++; } printf("Impossible.");}int main(){ int bx,by,px,py; char a[30]; cas=1; while(scanf("%d%d",&n,&m)!=EOF) { if(!n&&!m) break; memset(visb,0,sizeof(visb)); memset(map,0,sizeof(map)); for(int i=0;i<10000;i++) while(!ans[i].empty()) ans[i].pop(); for(int i=0;i<n;i++) { scanf("%s",a); for(int j=0;j<m;j++) { if(a[j]=='#') map[i][j]=0; else map[i][j]=1; if(a[j]=='T') { tx=i;ty=j; } else if(a[j]=='B') { bx=i;by=j; } else if(a[j]=='S') { px=i;py=j; } } } printf("Maze #%d\n",cas++); bfs(bx,by,px,py); putchar(10); putchar(10); } return 0;}
- poj 1475 Pushing Boxes(推箱子游戏,bfs+bfs)
- POJ 1475 Pushing Boxes (双重BFS/推箱子游戏)
- POJ 1475 Pushing Boxes(推箱子游戏 内嵌BFS)
- hdu 1254 推箱子/poj 1475 Pushing Boxes(推箱子经典问题,BFS嵌套BFS)
- POJ 1475 Pushing Boxes 推箱子游戏
- Pushing Boxes (poj 1475 嵌套bfs)
- poj 1475 Pushing Boxes(bfs)
- POJ 1475 Pushing Boxes(BFS)
- POJ 1475 Pushing Boxes(BFS)
- POJ 1475 Pushing Boxes(嵌套BFS)
- POJ-1475-Pushing Boxes(BFS)
- poj 1475 Pushing Boxes (推箱子游戏 三维数组判重)
- POJ 1475 Pushing Boxes 搜索- 两重BFS
- POJ 1475 Pushing Boxes 嵌套BFS -
- POJ 1475 Pushing Boxes 【A*bfs套bfs】
- POJ-1475:Pushing Boxes(嵌套bfs或bfs+A*算法)
- 双重 BFS —— POJ 1475 Pushing Boxes
- POJ Pushing Boxes 优先队列BFS
- 贝叶斯公式
- C#QRCode二维码生成的使用笔记
- Store Kit Guide(In App Purchase)翻译
- YUV视频格式到RGB32格式转换的速度优化 中篇
- 进程间的8种通信方式
- poj 1475 Pushing Boxes(推箱子游戏,bfs+bfs)
- 二维数组定义及动态分配空间
- ZTree的使用
- YUV视频格式到RGB32格式转换的速度优化 下篇
- 多态的内幕--(C++, C)语言两个版本
- 关于PHP的漏洞以及如何防止PHP漏洞?
- JQuery 上下文选择器
- MotionEvent事件在onInterceptTouchEvent()、onTouchEvent()中的传递顺序
- iOS开发的2D绘制--CoreGraphics的简单使用二(画图形)