您的位置:首页 > 其它

poj 1475 Pushing Boxes(bfs)

2015-08-24 19:57 435 查看
Pushing Boxes

Time Limit: 2000MSMemory Limit: 131072K
Total Submissions: 4846Accepted: 1671Special Judge
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
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: