POJ-1475-Pushing Boxes(BFS)

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-Pushing Boxes(BFS)

Input

The input contains the descriptions of several mazes. Each maze description starts with a line containing two integers r and c (both

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 .”.

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

思路:用一个四维数组分别记录箱子和人的位置。son数组记录上一个节点,用优先队列让推的次数最小的先出队。递归输出路径。

#include
#include
using namespace std;

struct S{
int x,y,bx,by,id,pcnt,acnt;
friend bool operatorb.pcnt;
    else return a.acnt>b.acnt;
}

priority_queueque;

char mp[20][21];
bool vis[20][20][20][20];
int nxt[4][2]={{0,1},{1,0},{0,-1},{-1,0}},val[1000000],son[1000000],idx;

void dfs(int x)
{
    if(son[x]!=-1)
    {
        dfs(son[x]);
        printf("%c",val[x]);
    }
}

int main()
{
    int n,m,i,j,p,q,ex,ey,casenum=1;

    while(~scanf("%d%d",&n,&m) && n)
    {
        printf("Maze #%d\n",casenum++);

        for(i=0;i=0 && t.x=0 && t.y=0 && t.bx=0 && t.by

Original: https://www.cnblogs.com/hrhguanli/p/5178288.html
Author: hrhguanli
Title: POJ-1475-Pushing Boxes(BFS)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/546489/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球