Poj-1475-pushing Boxes (BFS)

Source: Internet
Author: User

Description

Imagine you is standing inside a two-dimensional maze composed of square cells which may or may is filled with rock. You can move north, south, east or west one cell at a step. These moves is 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 MOV ing in the direction of the box. Such a move is called a push. The box cannot be moved on any other-than by pushing, which means so if you push it to 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 would work out the best such sequence?

Input

The input contains the descriptions of several mazes. Each maze description starts with a line containing both integers r and C (both <=) representing the number of rows a nd columns of the maze.

Following this is r lines each containing C characters. Each character describes one cell of the maze. A cell full of rock was indicated by a ' # ' and an empty cell was 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-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 was 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, lower Case 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 7SB .... T1 7SB.. #. T7 11########### #T ##......##.#.#. #####.... b....##.######. ##..... s...########### #8 4.....##. #...#...#. b.# #S .... # #T0 0

Sample Output

Maze #1EEEEEMaze #2Impossible. Maze #3eennwwWWWWeeeeeesswwwwwwwnNNMaze #4swwwnnnnnneeesssSSS


Idea: Use a four-dimensional array to record boxes and people's positions separately. The son Array records the previous node, with the priority queue giving the least number of pushes to the first-out team. The recursive output path.


#include <cstdio> #include <queue>using namespace std;struct s{int x,y,bx,by,id,pcnt,acnt;friend bool operator< (struct s a,struct s B);}    T;bool operator< (struct s a,struct s B) {if (a.pcnt!=b.pcnt) return a.pcnt>b.pcnt; else return a.acnt>b.acnt;} Priority_queue<struct S>que;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<n;i++) scanf ("%s", Mp[i]);        for (i=0;i<n;i++) for (j=0;j<m;j++) for (p=0;p<n;p++) for (q=0;q<m;q++) vis[i][j][p][q]=0; for (i=0;i<n;i++) {for (j=0;j<m;j++) {if (mp[i][j]== ' S ') mp[i][j]= '. ', t.x                =i,t.y=j; if (mp[i][j]== ' B ') mp[i][j]= '. ', t.bx=I,t.by=j;            if (mp[i][j]== ' T ') mp[i][j]= '. ', ex=i,ey=j;        }} vis[t.x][t.y][t.bx][t.by]=1;        Idx=1;        T.id=0;        Son[0]=-1;        t.pcnt=0;        t.acnt=0;        while (!que.empty ()) Que.pop ();        Que.push (t);            while (!que.empty ()) {t=que.top ();                if (T.bx==ex && t.by==ey) {dfs (t.id);                printf ("\ n");            Break                } for (i=0;i<4;i++) {t.x+=nxt[i][0];                T.Y+=NXT[I][1];                if (t.x>=0 && t.x<n && t.y>=0 && t.y<m && mp[t.x][t.y]== '. ')                        {if (t.x==t.bx && t.y==t.by)//Push box {t.bx+=nxt[i][0];                        T.BY+=NXT[I][1]; if (t.bx>=0 && t.bx<n && t.by>=0 && t.by<m && mp[t.bx][t.by]=='.') {if (!vis[t.x][t.y][t.bx][t.by]) {VI                                S[t.x][t.y][t.bx][t.by]=1;                                int oldid=t.id;                                Son[idx]=t.id;                                T.id=idx;                                t.pcnt++;                                t.acnt++;                                if (i==0) val[idx]= ' E ';                                if (i==1) val[idx]= ' S ';                                if (i==2) val[idx]= ' W ';                                if (i==3) val[idx]= ' N ';                                Que.push (t);                                t.pcnt--;                                t.acnt--;                                idx++;                            T.id=oldid;                        }} T.bx-=nxt[i][0];                    T.BY-=NXT[I][1]; } else//does not push the boxSub {if (!vis[t.x][t.y][t.bx][t.by]) {                            Vis[t.x][t.y][t.bx][t.by]=1;                            int oldid=t.id;                            Son[idx]=t.id;                            T.id=idx;                            t.acnt++;                            if (i==0) val[idx]= ' e ';                            if (i==1) val[idx]= ' s ';                            if (i==2) val[idx]= ' W ';                            if (i==3) val[idx]= ' n ';                            Que.push (t);                            t.acnt--;                            idx++;                        T.id=oldid;                }}} T.x-=nxt[i][0];            T.Y-=NXT[I][1];        } que.pop ();        } if (Que.empty ()) printf ("impossible.\n");    printf ("\ n"); }}


Poj-1475-pushing Boxes (BFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.