码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1475 Pushing Boxes

时间:2014-10-02 01:17:21      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   ar   java   

Pushing Boxes

Time Limit: 2000ms
Memory Limit: 131072KB
This problem will be judged on PKU. Original ID: 1475
64-bit integer IO format: %lld      Java class name: Main
Special Judge
 
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? 
bubuko.com,布布扣
 

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

 
解题:二维bfs?。。。。。推箱子,主要方向是箱子移动方向,先确定箱子移动方向,再确定人要到达箱子的哪一侧,箱子移动,需要一个bfs,人到箱子的一侧需要一个bfs。。。故需要两个bfs。
 
bubuko.com,布布扣
  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 }
View Code

 

HDU 1475 Pushing Boxes

标签:des   style   blog   http   color   io   os   ar   java   

原文地址:http://www.cnblogs.com/crackpotisback/p/4003663.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!