poj 1475 Pushing Boxes(bfs)
2015-08-24 19:57
435 查看
Pushing Boxes
Description
Imagine you are standing inside a two-dimensional maze composed of square cells which may or may not be filled with rock. You can move north, south, east or west one cell at a step. These moves are called walks.
One of the empty cells contains a box which can be moved to an adjacent free cell by standing next to the box and then moving in the direction of the box. Such a move is called a push. The box cannot be moved in any other way than by pushing, which means that
if you push it into a corner you can never get it out of the corner again.
One of the empty cells is marked as the target cell. Your job is to bring the box to the target cell by a sequence of walks and pushes. As the box is very heavy, you would like to minimize the number of pushes. Can you write a program that will work out the
best such sequence?
Input
The input contains the descriptions of several mazes. Each maze description starts with a line containing two integers r and c (both <= 20) representing the number of rows and columns of the maze.
Following this are r lines each containing c characters. Each character describes one cell of the maze. A cell full of rock is indicated by a `#' and an empty cell is represented by a `.'. Your starting position is symbolized by `S', the starting position of
the box by `B' and the target cell by `T'.
Input is terminated by two zeroes for r and c.
Output
For each maze in the input, first print the number of the maze, as shown in the sample output. Then, if it is impossible to bring the box to the target cell, print ``Impossible.''.
Otherwise, output a sequence that minimizes the number of pushes. If there is more than one such sequence, choose the one that minimizes the number of total moves (walks and pushes). If there is still more than one such sequence, any one is acceptable.
Print the sequence as a string of the characters N, S, E, W, n, s, e and w where uppercase letters stand for pushes, lowercase letters stand for walks and the different letters stand for the directions north, south, east and west.
Output a single blank line after each test case.
Sample Input
Sample Output
用优先队列每次让push数最少的出队,father数组记录上一个节点,递归输出路径。
Time Limit: 2000MS | Memory Limit: 131072K | |||
Total Submissions: 4846 | Accepted: 1671 | Special Judge |
Imagine you are standing inside a two-dimensional maze composed of square cells which may or may not be filled with rock. You can move north, south, east or west one cell at a step. These moves are called walks.
One of the empty cells contains a box which can be moved to an adjacent free cell by standing next to the box and then moving in the direction of the box. Such a move is called a push. The box cannot be moved in any other way than by pushing, which means that
if you push it into a corner you can never get it out of the corner again.
One of the empty cells is marked as the target cell. Your job is to bring the box to the target cell by a sequence of walks and pushes. As the box is very heavy, you would like to minimize the number of pushes. Can you write a program that will work out the
best such sequence?
Input
The input contains the descriptions of several mazes. Each maze description starts with a line containing two integers r and c (both <= 20) representing the number of rows and columns of the maze.
Following this are r lines each containing c characters. Each character describes one cell of the maze. A cell full of rock is indicated by a `#' and an empty cell is represented by a `.'. Your starting position is symbolized by `S', the starting position of
the box by `B' and the target cell by `T'.
Input is terminated by two zeroes for r and c.
Output
For each maze in the input, first print the number of the maze, as shown in the sample output. Then, if it is impossible to bring the box to the target cell, print ``Impossible.''.
Otherwise, output a sequence that minimizes the number of pushes. If there is more than one such sequence, choose the one that minimizes the number of total moves (walks and pushes). If there is still more than one such sequence, any one is acceptable.
Print the sequence as a string of the characters N, S, E, W, n, s, e and w where uppercase letters stand for pushes, lowercase letters stand for walks and the different letters stand for the directions north, south, east and west.
Output a single blank line after each test case.
Sample Input
1 7 SB....T 1 7 SB..#.T 7 11 ########### #T##......# #.#.#..#### #....B....# #.######..# #.....S...# ########### 8 4 .... .##. .#.. .#.. .#.B .##S .... ###T 0 0
Sample Output
Maze #1 EEEEE Maze #2 Impossible. Maze #3 eennwwWWWWeeeeeesswwwwwwwnNN Maze #4 swwwnnnnnneeesssSSS
用优先队列每次让push数最少的出队,father数组记录上一个节点,递归输出路径。
#include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <queue> #define INF 100000000 using namespace std; int n,m; char map[22][22]; bool vis[22][22][22][22]; //标记人和箱子的位置 int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}}; int father[1000*1000]; char ans[1000*1000]; struct node { int x1,y1; int x2,y2; int step; //总的步数 int pnum; //push数 int id; bool operator <(const node &a)const { if(pnum!=a.pnum) return pnum>a.pnum; return step>a.step; } }; node s,e; void dfs(int id) //递归输出路径 { if(father[id]!=-1) { dfs(father[id]); printf("%c",ans[id]); } } int bfs() { node temp; priority_queue<node>q; s.step=0; s.pnum=0; s.id=0; memset(vis, false, sizeof(vis)); q.push(s); vis[s.x1][s.y1][s.x2][s.y2]=true; father[0]=-1; int id=1; while (!q.empty()) { s=q.top(); q.pop(); if(s.x2==e.x2 && s.y2==e.y2) return s.id; for (int i=0; i<4; i++) { temp=s; temp.x1+=dir[i][0]; temp.y1+=dir[i][1]; int x=temp.x1; int y=temp.y1; if(x<0 || x>=n || y<0 || y>=m || map[x][y]=='#') continue; if(x==temp.x2 && y==temp.y2) //遇到箱子,推箱子 { int xx=x+dir[i][0]; int yy=y+dir[i][1]; if(xx<0 || xx>=n || yy<0 || yy>=m || map[xx][yy]=='#') continue; if(!vis[x][y][xx][yy]) { vis[x][y][xx][yy]=true; father[id]=temp.id; temp.id=id; temp.x2=xx; temp.y2=yy; temp.step++; //总步数++ temp.pnum++; //推箱子次数++ q.push(temp); if(i==0) ans[id]='N'; if(i==1) ans[id]='E'; if(i==2) ans[id]='S'; if(i==3) ans[id]='W'; id++; } } else //没遇到箱子 { if(!vis[x][y][temp.x2][temp.y2]) { temp.step++; father[id]=temp.id; temp.id=id; if(i==0) ans[id]='n'; if(i==1) ans[id]='e'; if(i==2) ans[id]='s'; if(i==3) ans[id]='w'; id++; vis[x][y][temp.x2][temp.y2]=true; q.push(temp); } } } } return -1; } int main() { int cnt=0; while (scanf("%d%d",&n,&m) && (n+m)) { for (int i=0; i<n; i++) { scanf("%s",map[i]); for (int j=0; j<m; j++) { if(map[i][j]=='B') { s.x2=i; s.y2=j; } else if(map[i][j]=='T') { e.x2=i; e.y2=j; } else if(map[i][j]=='S') { s.x1=i; s.y1=j; } if(map[i][j]!='#') map[i][j]='.'; } } int id=bfs(); printf("Maze #%d\n",++cnt); if (id==-1) printf("Impossible."); else dfs(id); printf("\n\n"); } return 0; }
相关文章推荐
- Cocos2d-x 创建精灵的五种方法
- [Linux运维 -- webserver]nginx的日志轮转
- 复习笔记
- Oracle学习笔记20150822分页查询sql语句分类和集合运算
- The stack size specified is too small, Specify at least 228k
- Redis常用的命令(五)------列表list
- leetcode263_264:Ugly Number & Ugly Number II
- java web之Servlet技术
- ARM处理器寄存器
- kvc和kvo的使用情况的了解
- 基数排序C++实现
- Ajax工作原理
- 深入理解PHP原理之错误抑制与内嵌HTML
- uiwebview添加到阅读列表
- 如何让IE支持自定义协议
- 哈诺塔题型问题+uva10795+URAL 2029
- useragent 全集 firefox插件 useragetn switch 使用
- xcode修改代码目录结构出现clang:error:nosuchfileordirectory解决方法
- hibernate3第三章之关联映射
- xcode修改代码目录结构出现clang:error:nosuchfileordirectory解决方法