Poj 1475 pushing boxes (dual BFS/push box games)

Source: Internet
Author: User

Pushing boxes

Time limit:2000 ms   Memory limit:131072 K
Total submissions:3975   Accepted:1403   Special Judge

Description

Imagine you are standing inside a two-dimen=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 wowould like to minimize the number of pushes. can you write a program that will work out
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 '. '. your starting position is symbolized by's ', the starting position
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 ctions 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 #1 eeeeemaze # 2impossible. Maze #3 eennwwwwwweeeeeesswwwwwnnnmaze #4 swwwnnnnnneeessssss

 

Question Analysis:

1. Ensure that the front and back positions of the box are not obstacles. If the front position is an obstacle, the person cannot reach it. The latter position is an obstacle and the box cannot be pushed.

2. The purpose of the question is: the box arrives at the destination, and the person arrives before the box.

3. Note CIN> you can eat the line feed. If you use scanf () to read the line feed, use gets () to eat the line feed.

 

Thoughts:

The first time I used queue to write BFS (), I felt pretty good ~

 

Code :

# Include <cstdio> # include <iostream> # include <queue> # include <cstring> # include <string> # define N 30 using namespace STD; int DX [4] = {-,}; // corresponding to n \ s \ W \ Eint dy [4] = }; struct node {int PR, PC; // The person's position int Br, BC; // The Box's position string ans; // The current operation }; char pushes [4] = {'n', 's', 'w', 'E '}; // The direction and coordinate changes must correspond to Char walks [4] = {'n', 's', 'w', 'E'}; string path; int R, c; int visp [N] [N]; int visb [N] [N]; char maps [N] [N]; bool isinmaps (Int x, int y) {If (x> = 1 & x <= R & Y> = 1 & Y <= c) Return true; else return false;} // determines whether a person can reach the first grid in the box. // (Sr, SC): person location // (ER, EC ): location before the box // (BBR, BBC): Location of the box // ans: path record int BFS (INT Sr, int SC, int er, int EC, int BBR, int BBC, string & ans1) {memset (visp, 0, sizeof (visp); node cur, temp; queue <node> q; // The while (! Q. empty () // clear Q before the queue is used. pop (); cur. PR = Sr; // queue initialization cur. PC = SC; cur. ans = ""; q. push (cur); visp [BBR] [BBC] = 1; // The guarantor does not walk through the box while (! Q. empty () // If the queue is not empty, search {cur = Q. front (); // element Q. pop (); // indicates that the stack has been checked for its surrounding position if (cur. PR = ER & cur. PC = EC) // returns {ans1 = cur. ans; return 1;} If (visp [cur. PR] [cur. PC]) continue; visp [cur. PR] [cur. PC] = 1; for (INT I = 0; I <4; I ++) // search for {temp in four directions. PR = cur. PR + dx [I]; temp. PC = cur. PC + dy [I]; If (isinmaps (temp. PR, temp. PC )&&! Visp [temp. PR] [temp. PC] & maps [temp. PR] [temp. PC]! = '#') {Temp. ans = cur. ans + walks [I]; q. push (temp); // if the current position can be taken, enter the queue }}return 0;} // the box to the terminal // (Sr, SC ): person location // (BR, BC): The Box location int bfs1 (INT Sr, int SC, int BBR, int BBC) {memset (visb, 0, sizeof (visb); node cur1, temp1; queue <node> q1; while (! Q1.empty () q1.pop (); cur1.pr = Sr; cur1.pc = SC; cur1.br = BBR; cur1.bc = BBC; cur1.ans = ""; q1.push (cur1); While (! Q1.empty () {cur1 = q1.front (); q1.pop (); If (visb [cur1.br] [cur1.bc]) continue; visb [cur1.br] [cur1.bc] = 1; if (maps [cur1.br] [cur1.bc] = 'T') {Path = cur1.ans; return 1 ;}for (INT I = 0; I <4; I ++) {// int nextr = cur1.br + dx [I]; int nextc = cur1.bc + dy [I]; // next position of the box int backr = cur1.br-DX [I]; int backc = cur1.bc-dy [I]; string ans = ""; if (isinmaps (nextr, nextc) & isinmaps (backr, backc) & maps [nextr] [nextc]! = '#' & Maps [backr] [backc]! = '#'&&! Visb [nextr] [nextc]) {If (BFS (cur1.pr, cur1.pc, backr, backc, cur1.br, cur1.bc, ANS) {temp1.pr = cur1.br; // update the location of the box and person: temp1.pc = cur1.bc; temp1.br = nextr; temp1.bc = nextc; temp1.ans = cur1.ans + ans + pushes [I]; // The path is the last step from + person to box + person to push box q1.push (temp1) ;}}}return 0 ;}int main () {int Sr, SC; // start point int Br, BC; // The Position of the box INT cases = 0; while (~ Scanf ("% d", & R, & C) {If (r = 0 & C = 0) break; int I, J; for (I = 1; I <= r; I ++) for (j = 1; j <= C; j ++) {CIN> maps [I] [J]; If (maps [I] [J] = 's') {sr = I; SC = J ;} if (maps [I] [J] = 'B') {BR = I; BC = J;} Path = ""; if (bfs1 (Sr, SC, BR, BC) cout <"maze #" <++ cases <Endl <path <Endl; else cout <"maze #" <++ cases <Endl <"impossible. "<Endl;} return 0 ;}

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.