POJ 1475 Pushing Boxes(推箱子游戏 内嵌BFS)
来源:互联网 发布:测试网络稳定性工具 编辑:程序博客网 时间:2024/06/14 06:27
题意:就特么是推箱子。
思路:我们的目标是让箱子到达目标地点,所以我们就主要是对箱子进行BFS,然后我们在这之后判断一下人可不可以推动这个箱子。然后记录下人挪动的步骤。当然我们用3维数组对箱子的移动进行判重,第三维度记录箱子是从哪个方向移动过来的。
#include <cstdio>#include <iostream>#include <queue>#include <cstring>#include <algorithm>#include <vector>using namespace std;struct node{ int sx,sy; //人的坐标 int bx,by; //box 坐标 vector <char> ans; //记录路径};int dx[]={-1,1,0,0};int dy[]={0,0,-1,1};char op[]={'n','s','w','e'};int n,m;int map[25][25];int vb[25][25][4];int vs[25][25];//void debug(node t) //调试函数{ for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(i==t.bx && j==t.by) {printf("B");continue;} else if(i==t.sx && j==t.sy) {printf("S");continue;} if(map[i][j]==9)printf("X"); else if(map[i][j]==0)printf("#"); else if(map[i][j]==1)printf("."); } putchar(10); } putchar(10);}bool bfss(node &t,int mx,int my) //对人进行BFS 判断是否可以推动 这里传了引用,为了改变人的位置{ if(t.sx==mx && t.sy==my) { t.sx=t.bx; t.sy=t.by; return true; } node w,e; memset(vs,0,sizeof(vs)); queue<node>QQ; w=t; QQ.push(w); vs[w.sx][w.sy]=1; while(!QQ.empty()) { w=QQ.front(); QQ.pop(); for(int k=0;k<4;k++) { e=w; int x=e.sx+dx[k]; int y=e.sy+dy[k]; if(map[x][y]==0)continue; if(x==mx && y==my) { e.sx=e.bx; e.sy=e.by; e.ans.push_back(op[k]); t=e; return true; } if(map[x][y]!=0 && vs[x][y]!=1 && (x!=e.bx || y!=e.by)) { e.sx=x; e.sy=y; e.ans.push_back(op[k]); vs[x][y]=1; QQ.push(e); } } } return false;}void bfsb(node t){ memset(vb,0,sizeof(vb)); queue<node>Q; node w,e; Q.push(t); while(!Q.empty()) { w=Q.front(); Q.pop(); //debug(w); for(int k=0;k<4;k++) { e=w; int x=e.bx+dx[k]; int y=e.by+dy[k]; int mx=e.bx-dx[k]; int my=e.by-dy[k]; if(map[mx][my]==0)continue; if(map[x][y]!=0 && vb[x][y][k]!=1) { if(bfss(e,mx,my)) { e.ans.push_back(op[k]-32); e.bx=x; e.by=y; vb[x][y][k]=1; Q.push(e); // debug(e); } else continue; //因为这个语句所以没有过下面所给的第二个样例 因为在人无法到达理想位置的时候 就不可以挪动箱子 即使箱子就在目标地点的旁边 if(map[x][y]==9) { for(int s=0;s<e.ans.size();s++) printf("%c",e.ans[s]); //printf("%c",op[k]-32); putchar(10); return ; } } } } printf("Impossible.\n");}int main(){ node w; int CASE=1; while(scanf("%d%d",&n,&m)!=EOF && n && m) { getchar(); memset(map,0,sizeof(map)); char ch; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { scanf("%c",&ch); if(ch=='#')map[i][j]=0; else if(ch=='.')map[i][j]=1; else if(ch=='T')map[i][j]=9; else if(ch=='S'){w.sx=i;w.sy=j;map[i][j]=1;} else if(ch=='B'){w.bx=i;w.by=j;map[i][j]=1;} } getchar(); } printf("Maze #%d\n",CASE++); bfsb(w); putchar(10); } return 0;}/*6 7ST.........#.............B.....#.#....#...11 19....#####..............#...#..............#...#............###..B##...........#......#.........###.#.##.#...#######...#.##.#####T...##.................#####..###.#S##....#...#......#########...########........Maze #1nwwwnnnwwnneSwswssseeennnWWnwSSSnnwwssseEEEEEEEEEEseNenW4 4#T###.BS#.#.#...*/
- POJ 1475 Pushing Boxes(推箱子游戏 内嵌BFS)
- poj 1475 Pushing Boxes(推箱子游戏,bfs+bfs)
- POJ 1475 Pushing Boxes (双重BFS/推箱子游戏)
- POJ 1475 Pushing Boxes 推箱子游戏
- hdu 1254 推箱子/poj 1475 Pushing Boxes(推箱子经典问题,BFS嵌套BFS)
- poj 1475 Pushing Boxes (推箱子游戏 三维数组判重)
- POJ-1475-Pushing Boxes(BFS)
- 【POJ1475】Pushing Boxes 推箱子
- POJ 1475 Pushing Boxes 搜索- 两重BFS
- 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 【A*bfs套bfs】
- poj 1475 Pushing Boxes
- (poj 1475) Pushing Boxes
- Minimum Inversion Number HDU
- 敏捷开发之工程师素质
- MySQL-Transfer2.2发布
- 使用SecureCRT登录本机cygwin
- STL中容器(vector)的简单实现
- POJ 1475 Pushing Boxes(推箱子游戏 内嵌BFS)
- CC2530芯片在TI协议栈中自定义外部中断
- Tesseract-OCR引擎 入门
- NLP工具集
- MATLAB将视频保存为MAT类型
- ABAP "FOR ALL ENTRIES IN" 使用指南
- asp.net对XML的操作
- PKU 1207 The 3n + 1 problem
- v4l2 编程接口 — driver