描述

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?

输入

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.

输出

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). 本题没有 Special Judge,多解时,先最小化箱子被推动的次数,再最小化人移动的步数。若仍有多条路线,则按照N、S、W、E的顺序优先选择箱子的移动方向(即先上下推,再左右推)。在此前提下,再按照n、s、w、e的顺序优先选择人的移动方向(即先上下动,再左右动)。

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.

样例输入

1 7
SB....T
1 7
SB..#.T
7 11
###########
#T##......#
#.#.#..####
#....B....#
#.######..#
#.....S...#
###########
8 4
....
.##.
.#..
.#..
.#.B
.##S
....
###T
0 0

样例输出

Maze #1
EEEEE Maze #2
Impossible. Maze #3
eennwwWWWWeeeeeesswwwwwwwnNN Maze #4
swwwnnnnnneeesssSSS

来源

Southwestern European Regional Contest 1997

本题数据为加强版,在原比赛或POJ AC的程序有可能只得50分

题解:

什么是bfs套bfs呢?在这题里,把人和物分开来想,用物体位置+人的方向来存储人与物之间的关系,那么人从左推物体就相当于人先走到物体的左边,然后把物体退了一下。

对人的走的计算也通过bfs来实现,所以是bfs套bfs。

代码实现容易出错,一些我想了比较久的代码都加了注释,希望对大家有所帮助。

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,m,t;
int dx[]={-1,1,0,0},dy[]={0,0,-1,1};
char ch[23][23],A[]={'N','S','W','E'},a[]={'n','s','w','e'};
bool vis[23][23];
string rec;
struct node {
int x,y,px,py;
string ans;
};
queue <node> q;
bool ok(int x,int y) {
return x>0 && x<=n && y>0 && y<=m && ch[x][y]!='#';
}
bool bfs2(node st,node ed) {//东西从pre推到now
rec="";
node fir;
fir.x=st.px,fir.y=st.py;
fir.ans="";
while(!q.empty()) q.pop();
q.push(fir);
memset(vis,0,sizeof vis);
while(!q.empty()) {
node now=q.front(),nxt;
q.pop();
if(now.x==st.x && now.y==st.y) {//人在物的位置了(说明物体已经被推过去了)
rec=now.ans;
return 1;
}
for(int i=0;i<4;i++) {
nxt=now;
nxt.x+=dx[i],nxt.y+=dy[i];
if(!ok(nxt.x,nxt.y) || (nxt.x==ed.x && nxt.y==ed.y) || vis[nxt.x][nxt.y]) continue;//人到物体该去的地方了,显然不行
vis[nxt.x][nxt.y]=1;
nxt.ans=now.ans+a[i];
q.push(nxt);
}
}
return 0;
}
string solve() {
node fir;
fir.ans="";
fir.x=fir.y=fir.px=fir.py=-1;
for(int i=1;i<=n && (fir.x==-1 || fir.px==-1);i++)
for(int j=1;j<=m && (fir.x==-1 || fir.py==-1);j++)
if(ch[i][j]=='B') fir.x=i,fir.y=j,ch[i][j]='.';
else if(ch[i][j]=='S') fir.px=i,fir.py=j,ch[i][j]='.';
queue <node> qq;
qq.push(fir);
bool vv[23][23][5];
memset(vv,0,sizeof vv);
string ans="Impossible.";
int cntans=0x3f3f3f3f,cnt=0x3f3f3f3f;
while(!qq.empty()) {
node now=qq.front(),nxt,pre;
qq.pop();
if(ch[now.x][now.y]=='T') {//更新答案
int cntnow=0;
for(int i=0;i<now.ans.length();i++) if(now.ans[i]>='A' && now.ans[i]<='Z') cntnow++;
if(cntnow<cntans || (cntnow==cntans && now.ans.length()<cnt)) {//题目中的排序方法
ans=now.ans;
cntans=cntnow;
cnt=now.ans.length();
}
continue;
}
for(int i=0;i<4;i++) {
nxt=now;
nxt.x+=dx[i];
nxt.y+=dy[i];
if(!ok(nxt.x,nxt.y) || vv[nxt.x][nxt.y][i]) continue;
pre=now;
if(i==0) pre.x++;
else if(i==1) pre.x--;
else if(i==2) pre.y++;
else if(i==3) pre.y--;//找到上一步状态
if(!bfs2(pre,now)) continue;
vv[nxt.x][nxt.y][i]=1;
nxt.ans=now.ans+rec;//人动
nxt.ans+=A[i];//箱子动
nxt.px=now.x;
nxt.py=now.y;
qq.push(nxt);
}
}
return ans;
}
signed main() {
while(scanf("%lld%lld",&n,&m)!=EOF && n && m) {
for(int i=1;i<=n;i++) scanf("%s",ch[i]+1);
cout << "Maze #" << ++t << '\n' << solve() << '\n' << '\n';
}
return 0;
}

最新文章

  1. jQuery2.x源码解析(缓存篇)
  2. EasyPR--一个开源的中文车牌识别系统
  3. JavaScript API 设计原则
  4. 用xshell操作linux系统的常用命令
  5. swift-06-字符串,字符以及元组类型
  6. 【转】Flask快速入门
  7. C++进阶阅读
  8. service structure flowchart with full stack functionality in a brife map
  9. STL—vector空间的动态增长
  10. 中国(北方)大学生程序设计训练赛(第一周) (D E)
  11. THE SCHOOLS WHERE APPLE, GOOGLE, AND FACEBOOK GET THEIR RECRUITS
  12. Android NFC技术(三)——初次开发Android NFC你须知道NdefMessage和NdefRecord
  13. 【js】Number与数组
  14. BZOJ2567 : 篱笆
  15. idea相关
  16. 安装,配置,启动FTP,SSH,NFS服务
  17. 第13月第25天 ios11 uitableview reloaddata contentsize
  18. eclipse启动时弹出Failed to create the Java Virtual Machine
  19. Openstack_通用模块_Oslo_vmware 创建 vCenter 虚拟机快照
  20. Java学习---JBPM[工作流]学习

热门文章

  1. sonarqube使用maven进行代码分析
  2. SharePoint - Another Way to Delete Site Collection
  3. Python【每日一问】20
  4. ssh密码登录+ Google Authenticator 实现双向认证
  5. Source Insight添加新的文件类型
  6. 详解redis持久化
  7. 转 Hystrix超时实现机制
  8. Java 并发-Unsafe 相关整理
  9. SpringBoot 基础(一)
  10. 给定一个长度为N的数组,找出出现次数大于n/2,n/3的数,要求时间复杂度O(n),空间复杂度O(1)