POJ1475 Pushing Boxes(BFS+BFS)
来源:互联网 发布:单片机与接口技术 编辑:程序博客网 时间:2024/05/17 23:03
题目就是推箱子的小游戏,叫你输出最优方案。推箱子用大写字母,人走用小写。
自己的想法:BFS乱搜。题解:双重BFS。涨姿势了,第一次见到。
外层BFS表示箱子要被推到的方向,内层BFS判断人是否能走到需要的那个位置;比如要往左推箱子,则要判断人是否能走到箱子的右边。
外层BFS还要开个数组判断是否在这个方向上经过这个点,不过我没理解透,估计再叫我写一遍我也写不出来。
#include<cstdio>#include<cstring>#include<queue>#include<string>#include<iostream>using namespace std;struct Node{ int x,y,px,py;//在外层BFS中分别表示人的位置,箱子位置 string S; Node(){} Node(int a,int b,int c,int d) {x = a; y = b; px = c; py = d;}};Node st;int n,m;int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};char mp[25][25],P[4] = {'N','S','W','E'},M[4] = {'n','s','w','e'};bool inarea(int i,int j){ if(i < 1||i > n||j < 1||j > m) return false; else return true;}void init(){ memset(mp,0,sizeof mp);}string tmp;bool vis[25][25];bool bfsto(Node human,Node box)//内层bfs把人走到箱子的位置{ tmp = ""; memset(vis,0,sizeof vis); queue<Node> q; Node sst; sst.x = human.px,sst.y = human.py;//人在的位置 q.push(sst); while(!q.empty()) { Node u = q.front(),v; q.pop(); if(u.x == human.x&&u.y == human.y) //走到目标 { tmp = u.S; return true; } for(int i = 0; i < 4; i++) { v.x = u.x+dir[i][0],v.y = u.y+dir[i][1],v.S = u.S; if(!inarea(v.x,v.y)||vis[v.x][v.y]) continue; if(mp[v.x][v.y] == '#') continue; if(v.x == box.x&&v.y == box.y) continue;//箱子的位置 v.S += M[i],vis[v.x][v.y] = 1; q.push(v); } } return false;}bool passed[25][25][4];void bfs(){ memset(passed,0,sizeof passed); queue<Node> Q; Q.push(st); while(!Q.empty()) { Node u = Q.front(),next,human; Q.pop(); if(mp[u.x][u.y] == 'T') { cout<<u.S<<endl<<endl; return; } for(int i = 0; i< 4; i++) { next = u,human = u; next.x = u.x+dir[i][0],next.y = u.y+dir[i][1]; if(!inarea(next.x,next.y)) continue; if(passed[next.x][next.y][i]) continue;//在方向i上箱子是否经过这个位置 if(mp[next.x][next.y] == '#') continue; if(i == 0) human.x = u.x+1;//向上推箱子,人要走到箱子的下边 else if (i == 1) human.x = u.x-1; else if(i == 2) human.y = u.y+1; else human.y = u.y-1; if(!bfsto(human,u)) continue; passed[next.x][next.y][i] = 1; next.S = u.S+tmp; next.S += P[i]; next.px = u.x,next.py = u.y; Q.push(next); } } printf("Impossible.\n\n");}int main(){ int cas = 1; while(scanf("%d%d",&n,&m) != EOF&&n+m) { init(); st.S = ""; for(int i = 1; i <= n; i++) { scanf("%s",mp[i]+1); for(int j = 1; j <= m; j++) { if(mp[i][j] == 'S') {st.px = i,st.py = j,mp[i][j] = '.';} if(mp[i][j] == 'B') {st.x = i,st.y = j,mp[i][j] = '.';} } } printf("Maze #%d\n",cas++); bfs(); }}
0 0
- POJ1475 Pushing Boxes(BFS+BFS)
- 【POJ1475】Pushing Boxes-A*(或BFS?)
- poj1475 Pushing Boxes
- poj1475 Pushing Boxes
- POJ-1475-Pushing Boxes(BFS)
- 【POJ1475】Pushing Boxes 推箱子
- POJ 1475 Pushing Boxes 搜索- 两重BFS
- uva 589 - Pushing Boxes(双重bfs)
- Pushing Boxes (poj 1475 嵌套bfs)
- poj 1475 Pushing Boxes(bfs)
- POJ 1475 Pushing Boxes(BFS)
- POJ 1475 Pushing Boxes(BFS)
- POJ Pushing Boxes 优先队列BFS
- POJ 1475 Pushing Boxes(嵌套BFS)
- POJ 1475 Pushing Boxes 嵌套BFS -
- poj 1475 Pushing Boxes(推箱子游戏,bfs+bfs)
- POJ 1475 Pushing Boxes 【A*bfs套bfs】
- #POJ1475#Pushing Boxes(A*_搜索)
- 常见jar包说明
- Java两个线程交替打印(实现Runnable接口)
- 十个值得一试的开源深度学习框架
- Android中handle用法总结
- uestc1131男神的礼物【dp】
- POJ1475 Pushing Boxes(BFS+BFS)
- [收藏]redis速查
- vim的几种模式和按键映射(初学者的科普文)
- linux实现cp(复制)功能的小函数
- 从C10K到C10M高性能网络的探索与实践
- 抽象类与接口
- qt5.5程序打包发布以及依赖
- java int与integer的区别
- Mac安装ReactNative