# [POJ 1475] Pushing Boxes【A*】

• 2018-01-03
• 0
• 0

## Problem:

 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

Southwestern European Regional Contest 1997

## Solution:

• push: h() = Manhattan(box, destination)。
• walk: h() = Manhattan(man, box) - 1，减 1 的原因是 man 只要走到 box 四周的某一格即可。

## Code: O(玄学) [2324K, 79MS]

```#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int dx = {0, 1, 0,-1};
const int dy = {1, 0,-1, 0};
const char DIR = {'E', 'S', 'W', 'N'};

int cas = 0, R, C;
char mp;

struct Point{
int x, y;

inline bool operator == (const Point &p2) const {return x == p2.x && y == p2.y;}

inline bool isLegal() {return x > 0 && x <= R && y > 0 && y <= C && mp[x][y] != '#';}
} T;

struct State{
Point man, box;
int pf, pg, ph, wf, wg, wh;
int id;

inline bool operator < (const State &st2) const {
if(pf != st2.pf) return pf > st2.pf;
return wf > st2.wf;
}
} S;

priority_queue<State> openList;
int closeList;

inline void clearPQ(priority_queue<State> &pq) {priority_queue<State> tmp; swap(tmp, pq);}

inline int Manhattan(const Point &p1, const Point &p2) {return abs(p1.x - p2.x) + abs(p1.y - p2.y);}

inline bool isBetter(const State &st){
int *buf = closeList[st.man.x][st.man.y][st.box.x][st.box.y];
if(buf != st.pf) return st.pf < buf;
return st.wf < buf;
}

inline void updateClose(const State &st){
int *buf = closeList[st.man.x][st.man.y][st.box.x][st.box.y];
buf = st.pf, buf = st.wf;
}

int fromId, topfrom;
char fromDir;

inline int Astar(){
S.pg = 0, S.ph = Manhattan(S.box, T), S.pf = S.pg + S.ph;
S.wg = 0, S.wh = Manhattan(S.man, S.box) - 1, S.wf = S.wg + S.wh;
S.id = 0;
openList.push(S);
while(!openList.empty()){
State u = openList.top(), v;
openList.pop();
for(register int i = 0; i < 4; i++){
v = u, v.man.x += dx[i], v.man.y += dy[i];
if(!v.man.isLegal()) continue;
if(v.man == v.box){  // Push
v.box.x += dx[i], v.box.y += dy[i];
if(!v.box.isLegal()) continue;
v.pg++, v.ph = Manhattan(v.box, T), v.pf = v.pg + v.ph;
if(!isBetter(v)) continue;
fromId[v.id = ++topfrom] = u.id, fromDir[v.id] = DIR[i];
openList.push(v);
updateClose(v);
if(v.box == T) return v.id;
}
else{  // Walk
v.wg++, v.wh = Manhattan(v.man, v.box) - 1, v.wf = v.wg + v.wh;
if(!isBetter(v)) continue;
fromId[v.id = ++topfrom] = u.id, fromDir[v.id] = DIR[i] - 'A' + 'a';
openList.push(v);
updateClose(v);
}
}
}
return -1;
}

inline void traceBack(int cur){
if(fromId[cur]) traceBack(fromId[cur]);
putchar(fromDir[cur]);
}

int main(){
while(scanf("%d%d", &R, &C) != EOF && R && C){
for(register int i = 1; i <= R; i++){
scanf("%s", mp[i] + 1);
for(register int j = 1; j <= C; j++){
if(mp[i][j] == 'T') T.x = i, T.y = j;
if(mp[i][j] == 'S') S.man.x = i, S.man.y = j;
if(mp[i][j] == 'B') S.box.x = i, S.box.y = j;
}
}
clearPQ(openList), memset(closeList, 0x3f, sizeof(closeList));
topfrom = 0;
int endId = Astar();
printf("Maze #%d\n", ++cas);
if(endId == -1) puts("Impossible.\n");
else traceBack(endId), puts("\n");
}
return 0;
}
```

#### 评论

49750

https://github.com/Darkleafin

OPEN AT 2017.12.10

Please refresh the page if the code cannot be displayed normally.

https://visualgo.net/en

- Theme by Qzhai