[poj P1475] Pushing Boxes

Time Limit: 2000MS   Memory Limit: 131072K   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

Source

又是一道变态的搜索题。。。

真心烦。。。

好吧,这其实也算蛮经典的吧,只是非常容易写挂。

要么WA,要么TLE。。

主要由于这题的特殊性,即人只能“推”箱子,而不是箱子自己走。

这就涉及到两个对象。处理起来稍稍复杂。

但是可不要用一些看似很优秀的算法(排除真的很优秀的可能),因为这题可能会有很多数据分分钟叉掉一个又一个idea。

比如下面的数据:

4 5
...T#
.#.B.
.#.#.
..S..

这是discuss里面的一组好数据。

还有比如:

10 12
############
#.......####
#...###.#T.#
#.#..##.#..#
#.#...#.#.##
#.#.#.B.....
#######.#.#.
#S####..#...
#......#####
############

特别是第二组,人必须先绕道箱子左边,然后推过头一点,再绕到箱子右边,推到T的正下方,再绕到箱子下面,把箱子推进去。

那么,我就用vis[人][箱子]记录某种状态是否可达,然后用个优先队列维护,以保证最优。

然后题目还说,先保证推箱子次数最小,再在这个限制下使得走路次数最小。

然后。。还有诸多的细节,需要自己注意一下。。

code:

 %:pragma GCC optimize()
 #include<cstdio>
 #include<cstring>
 #include<algorithm>
 #include<queue>
 #define id(x,y) (((x)-1)*m+(y))
 using namespace std;
 ;
 ][]={-,,,,,-,,};
 ]={'n','s','w','e'};
 ]={'N','S','W','E'};
 int n,m,Maz,pre[N*N*N*N]; char a[N][N],fp[N*N*N*N]; bool vis[N*N][N*N];
 ];
 struct state {
     int man,box,s1,s2,dfn;
     state() {}
     state(int _man,int _box,int _s1,int _s2,int _dfn):man(_man),box(_box),s1(_s1),s2(_s2),dfn(_dfn) {}
     bool operator < (const state &u) const {
         return s1==u.s1?s2>u.s2:s1>u.s1;
     }
 };
 void put(int dfn) {
     ) return;
     put(pre[dfn]);
     printf("%c",fp[dfn]);
 }
 void bfs() {
     memset(vis,,].z][ori[].z]=;
     priority_queue <state> q; while (!q.empty()) q.pop();
     state cur,nxt; ,tag=;
     q.push(state(ori[].z,ori[].z,,,));
     while (!q.empty()) {
         cur=q.top(),q.pop();
         ].x,ori[].y)) {tag=; put(cur.dfn); puts(""); break;}
         ; i<; i++) {
             x=(cur.man-)/m+,y=(cur.man-)%m+;
             x0=x+fl[i][],y0=y+fl[i][];
             ||x0>n||y0<||y0>m) continue;
             if (a[x0][y0]=='#') continue;
             if (id(x0,y0)==cur.box) {
                 x=x0+fl[i][],y=y0+fl[i][];
                 ||x>n||y<||y>m) continue;
                 if (a[x][y]=='#') continue;
                 if (vis[id(x0,y0)][id(x,y)]) continue;
                 vis[id(x0,y0)][id(x,y)]=;
                 pre[++dfn]=cur.dfn,fp[dfn]=pus[i];
                 nxt.man=id(x0,y0),nxt.box=id(x,y);
                 nxt.s1=cur.s1+,nxt.s2=cur.s2,nxt.dfn=dfn;
                 q.push(nxt);
             }else {
                 if (vis[id(x0,y0)][cur.box]) continue;
                 vis[id(x0,y0)][cur.box]=;
                 pre[++dfn]=cur.dfn,fp[dfn]=wal[i];
                 nxt.man=id(x0,y0),nxt.box=cur.box;
                 nxt.s1=cur.s1,nxt.s2=cur.s2+,nxt.dfn=dfn;
                 q.push(nxt);
             }
         }
     }
     if (!tag) puts("Impossible.");
 }
 int main() {
     Maz=;
     while (scanf("%d%d",&n,&m)!=EOF,n|m) {
         Maz++;
         ; i<=n; i++) {
             scanf();
             ; j<=m; j++) {
                 ].x=i,ori[].y=j; else
                 ].x=i,ori[].y=j; else
                 ].x=i,ori[].y=j;
             }
         }
         ; i<=; i++) ori[i].z=id(ori[i].x,ori[i].y);
         printf("Maze #%d\n",Maz);
         bfs();
         puts("");
     }
     ;
 }

[poj P1475] Pushing Boxes的更多相关文章

  1. (poj 1475) Pushing Boxes

    Imagine you are standing inside a two-dimensional maze composed of square cells which may or may not ...

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

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

  3. poj 1475 Pushing Boxes 推箱子(双bfs)

    题目链接:http://poj.org/problem?id=1475 一组测试数据: 7 3 ### .T. .S. #B# ... ... ... 结果: //解题思路:先判断盒子的四周是不是有空 ...

  4. poj 1475 || zoj 249 Pushing Boxes

    http://poj.org/problem?id=1475 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=249 Pushin ...

  5. Pushing Boxes(广度优先搜索)

    题目传送门 首先说明我这个代码和lyd的有点不同:可能更加复杂 既然要求以箱子步数为第一关键字,人的步数为第二关键字,那么我们可以想先找到箱子的最短路径.但单单找到箱子的最短路肯定不行啊,因为有时候不 ...

  6. 『Pushing Boxes 双重bfs』

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

  7. POJ1475 Pushing Boxes(双搜索)

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

  8. HDU 1475 Pushing Boxes

    Pushing Boxes Time Limit: 2000ms Memory Limit: 131072KB This problem will be judged on PKU. Original ...

  9. Pushing Boxes POJ - 1475 (嵌套bfs)

    Imagine you are standing inside a two-dimensional maze composed of square cells which may or may not ...

随机推荐

  1. redis示例 - 限速器,计时器

    INCR INCR key 将 key 中储存的数字值增一. 如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCR 操作. 如果值包含错误的类型,或字符串类型的值不能表示 ...

  2. 去掉vim的BELL提示音

    title: date: 2017-11-09 15:07:08 tags: vim categories: 开发工具 --- 在vi/vim中使用 :set noeb 意思是noerrorbells ...

  3. 【软件工程1916|W(福州大学)_助教博客】团队第一次作业成绩公示

    题目 第一次作业 评分准则: 队名(最好能够体现项目内容,要求有亮点与个性):(1分) 拟作的团队项目描述:一句话(中英文不限):(1分) 队员风采:介绍每一名队员,包括成员性格.擅长的技术.编程的兴 ...

  4. Shell脚本:while read line无法读取最后一行的问题

    [1]Shell脚本:while read line无法读取最后一行的问题 刚刚利用shell脚本处理日志文件时,发现了一个问题:while read line无法读取到最后一行 通过编辑器可以看到待 ...

  5. jmeter如何玩之badboy + jmeter并发性能测试

    今天下班时公司安排了一个同事来对项目做集群性能测试,怀着对性能测试的好奇心,下班后没有着急离开,而是等待 那位同事的到来,然后在旁边学习了下如何使用Badboy和jmeter做性能测试. 1. 软件介 ...

  6. C++ 打印机设置

    我在网上已不断看到一些网友关于自定义纸张打印的问题,基本上还没有较完美的解决方案,我在这里提供一个WindowsNT/2000/XP下的解决办法,供广大同仁参考.Windows9x/Me下也有解决办法 ...

  7. Docker Kubernetes YAML文件常用指令

    YAML文件常用指令 配置文件说明: 定义配置时,指定最新稳定版API(当前为v1). 配置文件应该存储在集群之外的版本控制仓库中.如果需要,可以快速回滚配置.重新创建和恢复. 应该使用YAML格式编 ...

  8. linux基础之awk

    gawk - pattern scanning and processing language 基本用法: gawk [options] 'program' FILE... program: PATT ...

  9. centos6.5下安装Redis

    已有redis-3.2.1.tar.gz文件 拖到centos系统的桌面 现在在桌面目录下 tar -zxv -f redis-3.2.1.tar.gz以解压压缩包 cd redis-3.2.1以切换 ...

  10. (转)A curated list of Artificial Intelligence (AI) courses, books, video lectures and papers

    A curated list of Artificial Intelligence (AI) courses, books, video lectures and papers. Updated 20 ...