Pushing Boxes
64-bit integer IO format: %lld Java class name: Main
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
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
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
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 30; 18 struct stats{ 19 int px,py,bx,by; 20 string path; 21 }; 22 struct node{ 23 int x,y; 24 string path; 25 }; 26 char mp[maxn][maxn]; 27 int r,c,box_x,box_y,pson_x,pson_y; 28 string ans; 29 const int dir[4][2] = {0,-1,0,1,-1,0,1,0}; 30 const char dc[4] = {'W','E','N','S'}; 31 const char dc2[4] = {'w','e','n','s'}; 32 bool check(int x,int y){ 33 return mp[x][y] != '#'; 34 } 35 bool bfs2(int nx,int ny,int tx,int ty,int kx,int ky,string &pans){ 36 queue<node>q; 37 bool vis[maxn][maxn] = {false}; 38 vis[nx][ny] = vis[kx][ky] = true; 39 node now,tmp; 40 now.x = nx; 41 now.y = ny; 42 now.path = ""; 43 q.push(now); 44 while(!q.empty()){ 45 now = q.front(); 46 q.pop(); 47 if(now.x == tx && now.y == ty){ 48 pans = now.path; 49 return true; 50 } 51 for(int i = 0; i < 4; i++){ 52 int zx = now.x + dir[i][0]; 53 int zy = now.y + dir[i][1]; 54 if(check(zx,zy)&&!vis[zx][zy]){ 55 vis[zx][zy] = true; 56 tmp.x = zx; 57 tmp.y = zy; 58 tmp.path = now.path + dc2[i]; 59 q.push(tmp); 60 } 61 } 62 } 63 return false; 64 } 65 bool bfs(){ 66 queue<stats>q; 67 bool vis[maxn][maxn] = {false}; 68 vis[box_x][box_y] = true; 69 stats tmp,now; 70 now.px = pson_x; 71 now.py = pson_y; 72 now.bx = box_x; 73 now.by = box_y; 74 now.path = ""; 75 q.push(now); 76 while(!q.empty()){ 77 now = q.front(); 78 q.pop(); 79 for(int i = 0; i < 4; i++){ 80 int nx = now.bx + dir[i][0]; 81 int ny = now.by + dir[i][1]; 82 int tx = now.bx - dir[i][0]; 83 int ty = now.by - dir[i][1]; 84 string pans = ""; 85 if(check(nx,ny)&&check(tx,ty)&&!vis[nx][ny]){ 86 if(bfs2(now.px,now.py,tx,ty,now.bx,now.by,pans)){ 87 vis[nx][ny] = true; 88 tmp.px = now.bx; 89 tmp.py = now.by; 90 tmp.bx = nx; 91 tmp.by = ny; 92 tmp.path = now.path + pans + dc[i]; 93 if(mp[nx][ny] == 'T'){ 94 ans = tmp.path; 95 return true; 96 } 97 q.push(tmp); 98 } 99 } 100 } 101 } 102 return false; 103 } 104 int main(){ 105 int cs = 1; 106 while(~scanf("%d %d",&r,&c) && r + c){ 107 memset(mp,'#',sizeof(mp)); 108 getchar(); 109 for(int i = 1; i <= r; i++){ 110 for(int j = 1; j <= c; j++){ 111 mp[i][j] = getchar(); 112 if(mp[i][j] == 'B'){ 113 box_x = i; 114 box_y = j; 115 } 116 if(mp[i][j] == 'S'){ 117 pson_x = i; 118 pson_y = j; 119 } 120 } 121 getchar(); 122 } 123 printf("Maze #%d\n", cs++); 124 if(bfs()) cout<<ans<<endl; 125 else puts("Impossible."); 126 puts(""); 127 } 128 return 0; 129 }