POJ-1475 pushing Boxes (bfs+ priority queue)

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

The main topic: Push the box. However, the path with the least number of steps to push is required, and if the same, the path with the fewest total steps is found.
Problem Analysis: The priority queue is undoubtedly used because it involves the priority of the path. The next step is to solve the problem by defining the state and state transitions. Obviously (in fact, this "obviously" took a long time to understand), the state parameter is "You" and the box coordinates, traversed the path and has "pushed" the number of steps and total steps. For this problem, state transfer is easier to do than define state.

Because the position of the box is also changing, so the state parameter must have the box coordinates. At first did not notice this point or did not want to understand, leading to blind delay a lot of work!!!


The code is as follows:
1# include<iostream>2# include<cstdio>3# include<queue>4# include<cstring>5# include<algorithm>6 using namespacestd;7 structnode8 {9     intmx,my,bx,by,pt,t,h;Ten     strings; OneNodeint_MX,int_my,int_BX,int_by,int_pt,int_t,string_s): mx (_MX), my (_my), BX (_BX), by (_by), PT (_pt), T (_t), S (_s) {} A     BOOL operator< (ConstNode &a)Const { -         if(pt==a.pt) -             returnT>A.T; the         returnPt>a.pt; -     } - }; - intr,c,vis[ -][ -][ -][ -]; + Charmp[ -][ -]; - stringf[2]={"NESW","NESW"}; + intd[4][2]={{-1,0},{0,1},{1,0},{0,-1}}; A BOOLOkintXinty) at { -     if(x>=0&&x<r&&y>=0&&y<c) -         return true; -     return false; - } - voidBFsintMxintMyintBxintby ) in { -Priority_queue<node>Q; tomemset (Vis,0,sizeof(Vis)); +vis[mx][my][bx][by]=1; -Q.push (Node (mx,my,bx,by,0,0,"")); the      while(!q.empty ()) *     { $Node u=q.top ();Panax Notoginseng Q.pop (); -         if(mp[u.bx][u.by]=='T'){ thecout<<u.s<<Endl; +             return ; A         } the          for(intI=0;i<4;++i) { +             intnx=u.mx+d[i][0],ny=u.my+d[i][1]; -             if(OK (nx,ny) &&mp[nx][ny]!='#'){ $                 if(nx==u.bx&&ny==u.by) { $                     intnbx=u.bx+d[i][0],nby=u.by+d[i][1]; -                     if(OK (Nbx,nby) &&mp[nbx][nby]!='#'&&!Vis[nx][ny][nbx][nby]) { -vis[nx][ny][nbx][nby]=1; the                         strings=U.S.; -s+=f[1][i];WuyiQ.push (Node (nx,ny,nbx,nby,u.pt+1, u.t+1, s)); the                     } -}Else{ Wu                     if(!Vis[nx][ny][u.bx][u.by]) { -vis[nx][ny][u.bx][u.by]=1; About                         strings=U.S.; $s+=f[0][i]; -Q.push (Node (nx,ny,u.bx,u.by,u.pt,u.t+1, s)); -                     } -                 } A             } +         } the     } -printf"impossible.\n"); $ } the intMain () the { the     intcas=0, Mx,my,bx,by; the      while(SCANF ("%d%d", &r,&c), r+c) -     { in          for(intI=0; i<r;++i) { thescanf"%s", Mp[i]); the              for(intj=0; j<c;++j) { About                 if(mp[i][j]=='S') themx=i,my=J; the                 if(mp[i][j]=='B') thebx=i,by=J; +             } -         } theprintf"Maze #%d\n",++CAs);Bayi BFS (mx,my,bx,by); theprintf"\ n"); the     } -     return 0; -}

Do after sense: must in want to understand the thought and then write code, otherwise, write more useless place!!!

POJ-1475 pushing Boxes (bfs+ priority queue)

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.