http://poj.org/problem?id=1475

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=249

Pushing Boxes
Time Limit: 2000MS   Memory Limit: 131072K
Total Submissions: 4662   Accepted: 1608   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? 
poj 1475 || zoj 249 Pushing Boxes

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

分析:
第一次看到题目的时候,以为可以两次bfs,先拿箱子bfs目标,记录路径,得到箱子的开始状态(即人的最终状态),然后再次拿人BFS箱子的初试状态,记录路径,把两个路径加起来即可(之前竟然不知道这个叫嵌套BFS)。
有几个细节需要注意:
    1,箱子移动时,箱子可以移动到人当前所在的位置。
    2,人移动时,人不能移动到箱子未改变状态时所在的位置。
后来第三组数据不对,想到了箱子是不能像人一样拐弯的,Orz

WA代码:

  1 #include <stdio.h>
  2 #include <algorithm>
  3 #include <iostream>
  4 #include <string.h>
  5 #include <string>
  6 #include <math.h>
  7 #include <stdlib.h>
  8 #include <queue>
  9 #include <stack>
 10 #include <set>
 11 #include <map>
 12 #include <list>
 13 #include <iomanip>
 14 #include <vector>
 15 #pragma comment(linker, "/STACK:1024000000,1024000000")
 16 #pragma warning(disable:4786)
 17 
 18 using namespace std;
 19 
 20 const int INF = 0x3f3f3f3f;
 21 const int MAX = 20 + 2;
 22 const double eps = 1e-8;
 23 const double PI = acos(-1.0);
 24 
 25 char ma[MAX][MAX];
 26 int vis[MAX][MAX];
 27 int n , m;
 28 int si , sj , bi , bj , ti , tj , ei , ej;
 29 int dir[4][2] = {1 , 0 , -1 , 0 , 0 , 1 , 0 , -1};
 30 char path[4] = {'S' , 'N' , 'E' , 'W'};
 31 char path1[4] = {'s' , 'n' , 'e' , 'w'};
 32 string str , str1 , ans;
 33 
 34 struct T
 35 {
 36     int x , y ;
 37     string sss;
 38 }temp , in , out;
 39 
 40 queue<T>qq , qq1;
 41 
 42 void bfs(int i , int j)
 43 {
 44     temp.x = i;
 45     temp.y = j;
 46     temp.sss = "";
 47     qq.push(temp);
 48     vis[i][j] = 1;
 49     while(!qq.empty())
 50     {
 51         out = qq.front();
 52         qq.pop();
 53         if(out.x == ti && out.y == tj)
 54         {
 55             str = out.sss;
 56             break;
 57         }
 58         for(int k = 0;k < 4;k ++)
 59         {
 60             int ix = out.x + dir[k][0];
 61             int iy = out.y + dir[k][1];
 62             if(ma[ix][iy] == '#' || vis[ix][iy] || ix < 1 || iy < 1 || ix > n || iy > m)
 63                 continue;
 64             in.x = ix;
 65             in.y = iy;
 66             in.sss = out.sss + path[k];
 67             vis[ix][iy] = 1;
 68             qq.push(in);
 69         }
 70     }
 71 }
 72 
 73 void bfs1(int i , int j)
 74 {
 75     temp.x = i;
 76     temp.y = j;
 77     temp.sss = "";
 78     qq.push(temp);
 79     vis[i][j] = 1;
 80     while(!qq.empty())
 81     {
 82         out = qq.front();
 83         qq.pop();
 84         if(out.x == ei && out.y == ej)
 85         {
 86             str1 = out.sss;
 87             break;
 88         }
 89         for(int k = 0;k < 4;k ++)
 90         {
 91             int ix = out.x + dir[k][0];
 92             int iy = out.y + dir[k][1];
 93             if(ma[ix][iy] == '#' || ma[ix][iy] == 'B' || vis[ix][iy] || ix < 1 || iy < 1 || ix > n || iy > m)
 94                 continue;
 95             in.x = ix;
 96             in.y = iy;
 97             in.sss = out.sss + path1[k];
 98             vis[ix][iy] = 1;
 99             qq.push(in);
100         }
101     }
102 }
103 
104 int main()
105 {
106     int first = 1;
107     while(scanf("%d %d",&n , &m) , n + m)
108     {
109         int i , j;
110         memset(vis , 0 , sizeof(vis));
111         for(i = 1;i <= n;i ++)
112         {
113             for(j = 1;j <= m;j ++)
114             {
115                 scanf("%c",&ma[i][j]);
116                 if(ma[i][j] == 'S')
117                 {
118                     si = i;
119                     sj = j;
120                 }
121                 else if(ma[i][j] == 'B')
122                 {
123                     bi = i;
124                     bj = j;
125                 }
126                 else if(ma[i][j] == 'T')
127                 {
128                     ti = i;
129                     tj = j;
130                 }
131             }
132             getchar();
133         }
134         str = "";
135         while(!qq.empty())
136             qq.pop();
137         bfs(bi , bj);
138         if(str[0] == 'S')
139         {
140             ei = bi - 1;
141             ej = bj;
142         }
143         else if(str[0] == 'N')
144         {
145             ei = bi + 1;
146             ej = bj;
147         }
148         else if(str[0] == 'E')
149         {
150             ei = bi;
151             ej = bj - 1;
152         }
153         else if(str[0] == 'W')
154         {
155             ei = bi;
156             ej = bj + 1;
157         }
158 
159         memset(vis , 0 , sizeof(vis));
160         str1 = "";
161         while(!qq.empty())
162             qq.pop();
163         bfs1(si , sj);
164         ans = "";
165         ans = str1 + str;
166 
167         printf("Maze #%d\n",first ++);
168         if(ans != "")
169             cout << ans << "\n" << endl;
170         else
171             cout << "Impossible\n" << endl;
172     }
173     return 0;
174 }
View Code

相关文章:

  • 2021-12-05
  • 2022-01-19
  • 2021-12-19
  • 2021-07-05
  • 2022-12-23
  • 2021-06-23
  • 2020-03-15
  • 2021-09-11
猜你喜欢
  • 2021-06-15
  • 2022-12-23
  • 2021-11-28
  • 2022-12-23
  • 2022-12-23
  • 2022-01-14
  • 2021-12-27
相关资源
相似解决方案