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 byS', the starting position of the
B’ and the target cell by `T’.
box byInput 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.
暴搜。
按箱子的走法bfs,每一步的时候bfs人的走法,可以保证箱子步数最少,相同时人的步数最少。
具体实现有一些细节。比如:
保存状态只用保存当前位置和上一步是怎么走来的【当然还有路径】,那么人的位置就确定了。
开始的时候枚举人走到箱子的四个边上,但是注意,在之后搜索的时候限制这一步不能再变方向。
第一个搜到的解不一定是最优的,要把所有相同步数的解找见以后找出来人走的最少的。
箱子判重不能只记位置,还要记来的方向。但是bfs人的时候可以只记位置。
用string记录路径比较方便,“+”即可。
#include<cstdio>
#include<cstring>
#include<queue>
#include<string>
#include<iostream>
using namespace std;
struct con
{
int x,y,f;
string pa;
}c1,c2,c3;
string path,ans[2000];
queue<con> que;
int map[25][25],m,n,t_x,t_y,xx[4]={-1,1,0,0},yy[4]={0,0,1,-1},len[2000];
char s[25],bl[4]={'N','S','E','W'},sl[4]={'n','s','e','w'};
bool vis[25][25][4],m_vis[25][25];
bool find(int n_x,int n_y,int t_x,int t_y,int b_x,int b_y)
{
if (t_x<1||t_x>n||t_y<1||t_y>m||map[t_x][t_y]) return 0;
if (n_x==t_x&&n_y==t_y)
{
path="";
return 1;
}
memset(m_vis,0,sizeof(m_vis));
queue<con> q1;
int i,j,k,x,y,z;
con ca,cb,cc;
m_vis[n_x][n_y]=1;
q1.push((con){n_x,n_y,0,""});
while (!q1.empty())
{
ca=q1.front();
q1.pop();
for (i=0;i<4;i++)
{
x=ca.x+xx[i];
y=ca.y+yy[i];
if (x<1||x>n||y<1||y>m||map[x][y]||m_vis[x][y]||(x==b_x&&y==b_y))
continue;
m_vis[x][y]=1;
cb=(con){x,y,0,ca.pa+sl[i]};
if (x==t_x&&y==t_y)
{
path=cb.pa;
return 1;
}
q1.push(cb);
}
}
return 0;
}
int main()
{
int i,j,k,p,q,x,y,z,m_x,m_y,cnt,K=0,tim;
while (scanf("%d%d",&n,&m)&&m&&n)
{
K++;
memset(vis,0,sizeof(vis));
while (!que.empty()) que.pop();
cnt=0;
for (i=1;i<=n;i++)
{
scanf("%s",s+1);
for (j=1;j<=m;j++)
if (s[j]=='#')
map[i][j]=1;
else
{
map[i][j]=0;
if (s[j]=='B')
{
c1.x=i;
c1.y=j;
}
if (s[j]=='S')
{
m_x=i;
m_y=j;
}
if (s[j]=='T')
{
t_x=i;
t_y=j;
}
}
}
tim=1;
for (i=0;i<4;i++)
if (find(m_x,m_y,c1.x-xx[i],c1.y-yy[i],c1.x,c1.y))
{
c1.pa=path;
c1.f=i;
tim++;
que.push(c1);
}
while (!que.empty())
{
c1=que.front();
que.pop();
if (tim) tim--;
for (i=0;i<4;i++)
{
if (tim&&i!=c1.f) continue;
x=c1.x+xx[i];
y=c1.y+yy[i];
if (x<1||x>n||y<1||y>m||vis[x][y][i]||map[x][y]) continue;
if (!find(c1.x-xx[c1.f],c1.y-yy[c1.f],c1.x-xx[i],c1.y-yy[i],c1.x,c1.y)) continue;
vis[x][y][i]=1;
c2=(con){x,y,i,c1.pa+path+bl[i]};
if (x==t_x&&y==t_y) ans[++cnt]=c2.pa;
if (!cnt) que.push(c2);
}
}
printf("Maze #%d\n",K);
if (!cnt)
printf("Impossible.");
else
{
for (i=1;i<=cnt;i++)
len[i]=ans[i].length();
p=1;
for (i=2;i<=cnt;i++)
if (len[i]<len[p]) p=i;
cout<<ans[p];
}
printf("\n\n");
}
}