[poj P1475] Pushing Boxes
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[poj P1475] Pushing Boxes相关的知识,希望对你有一定的参考价值。
[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:
View Code1 %:pragma GCC optimize(2) 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 #define id(x,y) (((x)-1)*m+(y)) 7 using namespace std; 8 const int N=25; 9 const int fl[4][2]={-1,0,1,0,0,-1,0,1}; 10 const char wal[4]={‘n‘,‘s‘,‘w‘,‘e‘}; 11 const char pus[4]={‘N‘,‘S‘,‘W‘,‘E‘}; 12 int n,m,Maz,pre[N*N*N*N]; char a[N][N],fp[N*N*N*N]; bool vis[N*N][N*N]; 13 struct point {int x,y,z;}ori[4]; 14 struct state { 15 int man,box,s1,s2,dfn; 16 state() {} 17 state(int _man,int _box,int _s1,int _s2,int _dfn):man(_man),box(_box),s1(_s1),s2(_s2),dfn(_dfn) {} 18 bool operator < (const state &u) const { 19 return s1==u.s1?s2>u.s2:s1>u.s1; 20 } 21 }; 22 void put(int dfn) { 23 if (dfn==0) return; 24 put(pre[dfn]); 25 printf("%c",fp[dfn]); 26 } 27 void bfs() { 28 memset(vis,0,sizeof vis),vis[ori[1].z][ori[2].z]=1; 29 priority_queue <state> q; while (!q.empty()) q.pop(); 30 state cur,nxt; int x,y,x0,y0,d,dfn=0,tag=0; 31 q.push(state(ori[1].z,ori[2].z,0,0,0)); 32 while (!q.empty()) { 33 cur=q.top(),q.pop(); 34 if (cur.box==id(ori[3].x,ori[3].y)) {tag=1; put(cur.dfn); puts(""); break;} 35 for (int i=0; i<4; i++) { 36 x=(cur.man-1)/m+1,y=(cur.man-1)%m+1; 37 x0=x+fl[i][0],y0=y+fl[i][1]; 38 if (x0<1||x0>n||y0<1||y0>m) continue; 39 if (a[x0][y0]==‘#‘) continue; 40 if (id(x0,y0)==cur.box) { 41 x=x0+fl[i][0],y=y0+fl[i][1]; 42 if (x<1||x>n||y<1||y>m) continue; 43 if (a[x][y]==‘#‘) continue; 44 if (vis[id(x0,y0)][id(x,y)]) continue; 45 vis[id(x0,y0)][id(x,y)]=1; 46 pre[++dfn]=cur.dfn,fp[dfn]=pus[i]; 47 nxt.man=id(x0,y0),nxt.box=id(x,y); 48 nxt.s1=cur.s1+1,nxt.s2=cur.s2,nxt.dfn=dfn; 49 q.push(nxt); 50 }else { 51 if (vis[id(x0,y0)][cur.box]) continue; 52 vis[id(x0,y0)][cur.box]=1; 53 pre[++dfn]=cur.dfn,fp[dfn]=wal[i]; 54 nxt.man=id(x0,y0),nxt.box=cur.box; 55 nxt.s1=cur.s1,nxt.s2=cur.s2+1,nxt.dfn=dfn; 56 q.push(nxt); 57 } 58 } 59 } 60 if (!tag) puts("Impossible."); 61 } 62 int main() { 63 Maz=0; 64 while (scanf("%d%d",&n,&m)!=EOF,n|m) { 65 Maz++; 66 for (int i=1; i<=n; i++) { 67 scanf("%s",a[i]+1); 68 for (int j=1; j<=m; j++) { 69 if (a[i][j]==‘S‘) ori[1].x=i,ori[1].y=j; else 70 if (a[i][j]==‘B‘) ori[2].x=i,ori[2].y=j; else 71 if (a[i][j]==‘T‘) ori[3].x=i,ori[3].y=j; 72 } 73 } 74 for (int i=1; i<=3; i++) ori[i].z=id(ori[i].x,ori[i].y); 75 printf("Maze #%d\n",Maz); 76 bfs(); 77 puts(""); 78 } 79 return 0; 80 }
以上是关于[poj P1475] Pushing Boxes的主要内容,如果未能解决你的问题,请参考以下文章