Pushing Boxes
Time Limit: 2000MS | Memory Limit: 131072K | |||
Total Submissions: 7212 | Accepted: 2473 | Special 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
题目大意:相信大家都玩过推箱子这个游戏吧,现在题目要求用最少的步数去移动箱子,并且人移动的步数不计算在箱子移动的步数之内,要求实现最小步数把箱子推入终点,并且打印出来箱子和人移动的路径,如果箱子无法达到终点那么就输出impossible
这题是组内对抗赛的C题,可以看得出要用Bfs,我们的想法是先用BFS找到箱子的路径,然后根据箱子的路径来BFS人移动的路径,看看人是否可以到达箱子的上一个位置,如果不能到达就说明箱子不能够到达终点。那么该如何实现这个思路呢?
首先来写一个人对于箱子的BFS
bool BFS_Findman(int s,int e,int aa,int bb,int ss,int ee)
{
queue<man> MM;
if(s<0||s>m||e<0||e>n||mapp[s][e]=='#') return false;
mmst.x=ss;mmst.y=ee;mmst.ans="";
memset(vis,0,sizeof(vis));
vis[aa][bb]=1;
vis[ss][ee]=1;
MM.push(mmst);
while(!MM.empty())
{
mmst=MM.front();
MM.pop();
if(mmst.x==s&&mmst.y==e)
{
return true;
}
for(int i=0;i<4;i++)
{
mmed.x=mmst.x+dir[i][0];
mmed.y=mmst.y+dir[i][1];
if(mmed.x>0&&mmed.x<=m&&mmed.y>0&&mmed.y<=n&&!vis[mmed.x][mmed.y]&&mapp[mmed.x][mmed.y]!='#')
{
mmed.ans=mmst.ans+ddir[i];
vis[mmed.x][mmed.y]=1;
MM.push(mmed);
}
}
}
return false;
}
这是判断人是否可以到达箱子的周围并且推动箱子向着终点走去
然后我们在写一个箱子路径的BFS
bool BFS_Findbox()
{
queue<box> BB;
bbst.x=boxx;bbst.y=boxy;bbst.ans="";
bbst.mani=manx;bbst.manj=many;
BB.push(bbst);
while(!BB.empty())
{
bbst=BB.front();
BB.pop();
if(bbst.x==tarx&&bbst.y==tary)
{
return true;
}
for(int i=0;i<4;i++)
{
bbed.x=bbst.x+dir[i][0];
bbed.y=bbst.y+dir[i][1];
if(bbed.x>0&&bbed.y>0&&bbed.x<=m&&bbed.y<=n&&!mark[bbed.x][bbed.y][i]&&mapp[bbed.x][bbed.y]!='#')
{
if(BFS_Findman(bbed.x-2*dir[i][0],bbed.y-2*dir[i][1],bbst.x,bbst.y,bbst.mani,bbst.manj))
{
bbed.mani=bbst.x;
bbed.manj=bbst.y;
bbed.ans=bbst.ans+mmst.ans+(ddir[i]);
mark[bbed.x][bbed.y][i]=1;
BB.push(bbed);
}
}
}
}
return false;
}
找到箱子的路径,其中在箱子的结构体中有一个string类型的ans来保存箱子的路径
下面是整个的代码
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
int boxx,boxy;
int manx,many;
int tarx,tary;
int dir[][2]={-1,0,1,0,0,-1,0,1};
//这是n s w e 的顺序
int m,n;
char ddir[]={'n','s','w','e'};
bool mark[25][25][4];
bool vis[25][25];
char mapp[25][25];
struct box{
int x,y;
int mani,manj;
string ans;
}bbst,bbed;
struct man{
int x,y;
string ans;
}mmst,mmed;
char i2I(char i)
{
return (i-'a'+'A');
}
bool BFS_Findman(int s,int e,int aa,int bb,int ss,int ee)
{
queue<man> MM;
if(s<0||s>m||e<0||e>n||mapp[s][e]=='#') return false;
mmst.x=ss;mmst.y=ee;mmst.ans="";
memset(vis,0,sizeof(vis));
vis[aa][bb]=1;
vis[ss][ee]=1;
MM.push(mmst);
while(!MM.empty())
{
mmst=MM.front();
MM.pop();
if(mmst.x==s&&mmst.y==e)
{
return true;
}
for(int i=0;i<4;i++)
{
mmed.x=mmst.x+dir[i][0];
mmed.y=mmst.y+dir[i][1];
if(mmed.x>0&&mmed.x<=m&&mmed.y>0&&mmed.y<=n&&!vis[mmed.x][mmed.y]&&mapp[mmed.x][mmed.y]!='#')
{
mmed.ans=mmst.ans+ddir[i];
vis[mmed.x][mmed.y]=1;
MM.push(mmed);
}
}
}
return false;
}
bool BFS_Findbox()
{
queue<box> BB;
bbst.x=boxx;bbst.y=boxy;bbst.ans="";
bbst.mani=manx;bbst.manj=many;
BB.push(bbst);
while(!BB.empty())
{
bbst=BB.front();
BB.pop();
if(bbst.x==tarx&&bbst.y==tary)
{
return true;
}
for(int i=0;i<4;i++)
{
bbed.x=bbst.x+dir[i][0];
bbed.y=bbst.y+dir[i][1];
if(bbed.x>0&&bbed.y>0&&bbed.x<=m&&bbed.y<=n&&!mark[bbed.x][bbed.y][i]&&mapp[bbed.x][bbed.y]!='#')
{
if(BFS_Findman(bbed.x-2*dir[i][0],bbed.y-2*dir[i][1],bbst.x,bbst.y,bbst.mani,bbst.manj))
{
bbed.mani=bbst.x;
bbed.manj=bbst.y;
bbed.ans=bbst.ans+mmst.ans+(ddir[i]);
mark[bbed.x][bbed.y][i]=1;
BB.push(bbed);
}
}
}
}
return false;
}
int main()
{
int T,k=1;
while(scanf("%d%d",&m,&n)&&m+n)
{
memset(mark,0,sizeof(mark));
memset(mapp,'#',sizeof(mapp));
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
{
cin>>mapp[i][j];
if(mapp[i][j]=='S')
{
manx=i;many=j;
}
if(mapp[i][j]=='T')
{
tarx=i;tary=j;
}
if(mapp[i][j]=='B')
{
boxx=i;boxy=j;
}
}
printf("Maze #%d\n",k++);
if(BFS_Findbox()) printf("%s\n\n",bbst.ans.c_str());
else printf("Impossible.\n\n");
}
return 0;
}
这个我只测试了本地的数据,并没有交到poj上提交(样例过了就是过了!!!)