描述

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;
}

POJ1475 Pushing Boxes(BFS套BFS)的更多相关文章

  1. POJ1475 Pushing Boxes(双搜索)

    POJ1475 Pushing Boxes  推箱子,#表示墙,B表示箱子的起点,T表示箱子的目标位置,S表示人的起点 本题没有 Special Judge,多解时,先最小化箱子被推动的次数,再最小化 ...

  2. POJ-1475 Pushing Boxes (BFS+优先队列)

    Description Imagine you are standing inside a two-dimensional maze composed of square cells which ma ...

  3. POJ1475 Pushing Boxes 华丽丽的双重BFS

    woc累死了写了两个半小时...就是BFS?我太菜了... 刚开始以为让人预先跑一遍BFS,然后一会儿取两节加起来就好了,结果发现求出来的最短路(就是这个意思)会因箱子的移动而变化....我死了QWQ ...

  4. poj1475 Pushing Boxes[双重BFS(毒瘤搜索题)]

    地址. 很重要的搜索题.★★★ 吐槽:算是写过的一道码量比较大的搜索题了,细节多,还比较毒瘤.虽然是一遍AC的,其实我提前偷了大数据,但是思路还是想了好长时间,照理说想了半小时出不来,我就会翻题解,但 ...

  5. poj1475 Pushing Boxes(BFS)

    题目链接 http://poj.org/problem?id=1475 题意 推箱子游戏.输入迷宫.箱子的位置.人的位置.目标位置,求人是否能把箱子推到目标位置,若能则输出推的最少的路径,如果有多条步 ...

  6. poj1475 -- Pushing Boxes

    这道题其实挺有趣 的,这让我想起小时候诺基亚手机上的推箱子游戏(虽然一点也不好玩) (英文不好-->)  题意翻译: 初始人(S),箱子(B),目的地(T)用人把箱子推到 T最小步数及其路径(满 ...

  7. POJ1475(Pushing Boxes)--bbffss

    题目在这里 题目一看完就忙着回忆童年了.推箱子的游戏. 假设只有一个箱子.游戏在一个R行C列的由单位格子组成的区域中进行,每一步, 你可以移动到相邻的四个格子中的一个,前提是那个格子是空的:或者,如果 ...

  8. 『Pushing Boxes 双重bfs』

    Pushing Boxes Description Imagine you are standing inside a two-dimensional maze composed of square ...

  9. POJ 1475 Pushing Boxes 搜索- 两重BFS

    题目地址: http://poj.org/problem?id=1475 两重BFS就行了,第一重是搜索箱子,第二重搜索人能不能到达推箱子的地方. AC代码: #include <iostrea ...

随机推荐

  1. c# 大白话告诉你Thread的Sleep和Join的区别

    我们的程序默认会有两个线程,一个是主线程,一个是负责垃圾回收的线程.如果代码不使用多线程,就只有主线程这一条干道.1.在主线程中调用Thread.Sleep(1000),表示主线程阻塞自己1秒.2.在 ...

  2. lhgDialog弹窗提示窗口组件

    原文地址:http://www.lhgdialog.com/api/ 在页面head引入lhgdialog(如果项目采用jQuery作为框架,则引用jQuery的库). <script type ...

  3. centos8 安装 docker

    centos 安装docker  官方参考地址:https://docs.docker.com/install/linux/docker-ce/centos/ 里面包含包下载地址: https://d ...

  4. ndk-build官方使用说明

    ndk-build 脚本可用于编译采用 NDK 基于 Make 的编译系统的项目.此外,我们还针对 ndk-build 使用的 Android.mk和 Application.mk 配置提供了更具体的 ...

  5. kubernetes 1.15 有哪些让人眼前一亮的新特性?

    原文链接:kubernetes 1.15 有哪些让人眼前一亮的新特性? 2019 年 6 月 20 日,Kubernetes 重磅发布了 1.15 版本,不过笔者忙到现在才有空认真来看一下到底更新了哪 ...

  6. Queue介绍

    美人如斯! 前言 队列是一种先进先出(FIFO)的数据结构,与生活中的排队类似,即先来先被服务,这样的特点决定了其具有一定的优先级含义,可以被用于任务调度等场景.队列模型如图: 图1.队列模型 jav ...

  7. c#读取数据库bool值

    数据库里bit这个布尔类型的字段,非常实用,但是在c#里读取时,许多人喜欢犯一些错误,导致运行报错. 实际中,有效的正确读取方法只有以下两种: int xxx= Convet.ToInt16(read ...

  8. javascript原型深入解析2--Object和Function,先有鸡先有蛋

    1.提出两个问题: Js 的prototype和__proto__ 是咋回事? 先有function 还是先有object? 2.引用<JavaScript权威指南>的一段描述: 每个JS ...

  9. tqdm()与set_description()的用法

    pbar=tqdm(range(55156))for i in pbar: # print(i) a=464443161*845113131 pbar.set_description("tr ...

  10. 2019 百合佳缘java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.百合佳缘等公司offer,岗位是Java后端开发,因为发展原因最终选择去了百合佳缘,入职一年时间了,也成为了面 ...