题目
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?
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 7 11 SB..#.T #T##......# ########### #....B....# #.#.#..#### #.######..# 8 4 #.....S...# ########### .... .##. 0 0 .#.. .#.. .#.B .##S .... ###T
Sample Output
Maze #1 EEEEE Maze #2 Impossible. Maze #3 eennwwWWWWeeeeeesswwwwwwwnNN Maze #4 swwwnnnnnneeesssSSS
此题网上有多种AC代码,不过有的代码并没有给出最优解,如下面两组数据:
本题我用的也是双重BFS,先BFS出来箱子的路径,在判断路径的时候再加入BFS人的路径,其中人是根据箱子的路径来走的,所以最优路径决定于箱子的路径。所以在写方向数组的时候把上下写在前面,左右写在后面,在判断箱子路径的时候加入一些条件,然后优先搜索上下或优先搜索左右,保证路径最短,具体条件可以再代码中自己体会。本文最后贴出本题的一些测试数据。5 10 ########## #T...#...# #...SB...# #....#...# ########## Maze #1 EEseenWWWWWWswN 11 19 ....#####.......... ....#...#.......... ....#...#.......... ..###..B##......... ..#......#......... ###.#.##.#...###### #...#.##.#####T...# #.................# ####..###.#S##....# ...#......######### ...########........ Maze #2 nwwwnnnwwnneSwswssseeennnWWnwSSSnnwwssseEEEEEEEEEEseNenW
代码
#include<stdio.h> #include<string.h> #include<cstring> #include<iostream> #include<queue> using namespace std; int dir[][2]={-1,0,1,0,0,1,0,-1}; char pushdir[][2]={'n','N','s','S','e','E','w','W'}; int r,c,ex,ey; char map[30][30]; int person[30][30],box[30][30][4]; string anser,ansp; struct node { int x,y; int px,py; string ans; }; int main() { int bbfs(int bx,int by,int px,int py); int px,py,bx,by,k=0; while(1) { k++; anser=""; scanf("%d %d",&r,&c); if(r==0&&c==0) break; for(int i=1;i<=r;i++) for(int j=0;j<=c;j++) { scanf("%c",&map[i][j]); if(map[i][j]=='S') { px=i; py=j; } if(map[i][j]=='B') { bx=i; by=j; } if(map[i][j]=='T') { ex=i; ey=j; } } printf("Maze #%d\n",k); if(bbfs(bx,by,px,py)) cout<<anser<<endl<<endl; else printf("Impossible.\n\n"); } return 0; } int ifok(int x,int y) { if(x<1||y<1||x>r||y>c||map[x][y]=='#') return 0; return 1; } int pbfs(int px,int py,int bx,int by,int bbx,int bby)//用于判断箱子能否被人从(bbx,bby)推到(bx,by),其中(px,py)是人目前位置 { memset(person,0,sizeof(person)); node now,next; queue<node> q; now.x=px; now.y=py; now.ans=""; person[px][py]=1; person[bbx][bby]=1; q.push(now); while(!q.empty()) { now=q.front(); q.pop(); if(now.x==(2*bbx-bx)&&now.y==(2*bby-by)) { ansp=now.ans; return 1; } for(int i=0;i<4;i++) { next.x=now.x+dir[i][0]; next.y=now.y+dir[i][1]; if(ifok(next.x,next.y)&&(next.x!=bbx||next.y!=bby)&&person[next.x][next.y]==0) { person[next.x][next.y]=1; next.ans=now.ans+pushdir[i][0]; q.push(next); } } } return 0; } int bbfs(int bx,int by,int px,int py) { memset(box,0,sizeof(box)); node now,next; now.x=bx; now.y=by; now.px=px; now.py=py; // box[bx][by]=1; now.ans=""; queue<node> q; q.push(now); while(!q.empty()) { now=q.front(); q.pop(); if(map[now.x][now.y]=='T') { anser=now.ans; return 1; } if(now.py==now.y&&((now.px<now.x&&now.x<ex)||(now.px>now.x&&now.x>ex))|| (now.px==now.x&&((now.py<now.y&&now.y>ey)||(now.py>now.y&&now.y<ey))))//符合条件优先搜索左右方向 { for(int i=0;i<4;i++) { next.x=now.x+dir[i][0]; next.y=now.y+dir[i][1]; if(ifok(next.x,next.y)&&box[next.x][next.y][i]==0&& ifok(2*now.x-next.x,2*now.y-next.y)) { if(pbfs(now.px,now.py,next.x,next.y,now.x,now.y)) { // box[now.x][now.y]=0; box[next.x][next.y][i]=1; next.px=now.x; next.py=now.y; next.ans=now.ans+ansp+pushdir[i][1]; q.push(next); } } } } else//优先搜索上下方向 { for(int i=3;i>=0;i--) { next.x=now.x+dir[i][0]; next.y=now.y+dir[i][1]; if(ifok(next.x,next.y)&&box[next.x][next.y][i]==0&& ifok(2*now.x-next.x,2*now.y-next.y)) { if(pbfs(now.px,now.py,next.x,next.y,now.x,now.y)) { // box[now.x][now.y]=0; box[next.x][next.y][i]=1; next.px=now.x; next.py=now.y; next.ans=now.ans+ansp+pushdir[i][1]; q.push(next); } } } } } return 0; }
很多很多测试数据 输入: 1 7 SB....T 1 7 SB..#.T 7 11 ########### #T##......# #.#.#..#### #....B....# #.######..# #.....S...# ########### 8 4 .... .##. .#.. .#.. .#.B .##S .... ###T 5 11 #T##......# #.#.#..#### #....B....# #.######..# #.....S...# 4 5 ##T.. .SB.. .#... ...## 7 8 ........ .######. .....T#. #.#####. #...BS.. #..##### ######## 10 5 ...## .#.## .#.## .#.## .#.## .#.## .#... SB... ##... ##T.. 1 3 STB 1 3 SBT 1 3 TBS 3 1 T B S 3 3 S.. .B. T.# 12 14 S.#........... .B.#.......... ....#......... #....#........ .#....#....... ..#....#...... ...#....#..... ....#....#.... .....#....#... ......#....#.. .......#....#. ........#...T# 11 19 ....#####.......... ....#...#.......... ....#...#.......... ..###..B##......... ..#......#......... ###.#.##.#...###### #...#.##.#####T...# #.................# ####..###.#S##....# ...#......######### ...########........ 20 20 S................... .##################. ..................#. .#.#..............#. .#.###########.#..#. .#.#...........#..#. .#.#..########.#..#. .#.#.........#.#..#. .#.########..#.#..#. .#.#.....B...#.#.... .#.#.######..#.#..#. .#.#.........#.#..#. .#.#..########.#..#. .#.#............#.#. .#.#............#.#. .#.#.############.#. ..................#. .#................#. .##################. ...................T 20 20 .................... .##################. ..................#. #################.#. ................#.#. .##############.#.#. .#............#.#.#. .#.##########.#.#.#. .#.#........#.#.#.#. .#.#.####...#.#.#.#. .#.#.#..SB..#.#.#.#. .#.#.#.##T..#.#.#.#. .#.#.#.######.#.#.#. .#.#.#........#.#.#. .#.#.##########.#.#. .#.#............#.#. .#.##############.#. .#................#. .##################. .................... 20 20 .................... .##################. ..................#. #################.#. ................#.#. .##############.#.#. .#............#.#.#. .#.##########.#.#.#. .#.#........#.#.#.#. .#.#.####...#.#.#.#. .#.#.#.SB...#.#.#.#. .#.#.#..#.###.#.#.#. .#.#.#.....##.#.#.#. .#.#.#.T.#....#.#.#. .#.#.##########.#.#. .#.#............#.#. .#.##############.#. .#................#. .##################. .................... 20 20 #################..# SB.................# ################.#.# .................#.# ..############.#.#.# #..............#.#.# #.#.########.#.#.#.# #.#..........#.#.#.# #.#.#.####.#.#.#.#.# #.#.#......#.#.#.#.# #.#.#.#.##.#.#.#.#.# #.#.#.#.##T#.#.#.#.# #.#.#.#........#.#.# #.#.#.#.######.#.#.# #.#.#............#.# #.#.#.##########.#.# #.#................# #.#.##############.. #................... #..################# 20 20 ..#................. .#SB.#...........##. .#.#.#.#.#####.###.. .#.....#.....#...#.. .#.###.#...#.....#.# .#...#####.#####.#.. .....#.....#.#...##. .###.#.#...#.#...... .#...#.#.###.#.####. .#.....#.....#...#.. .#.###.#...#.....#.# .#...#####.#####.#.. .....#.....#.#...##. .###.#.#...#.#...... .#...#.#.###.#.###.# .#.....#.....#...#.. .#.###.#...#.....##. .#...#####.#####.#.. ...........#.......# .#########...####..T 20 20 S................... .T.................. .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... ..................B. .................... 20 20 T................... .S.................. .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... ...................B 20 20 #################### #SB################# #################### #################### #################### #################### #################### #################### #################### #################### #################### #################### #################### #################### #################### #################### #################### #################### #################### ###################T 20 1 S . B . . . . . . . . . . . . . . . . T 20 3 S.. ##. ... .## ... ##. ... .## ... ##. ... .## ... ##. ... .## ... ##. ... .BT 1 20 TB.................S 0 0 输出: Maze #1 EEEEE Maze #2 Impossible. Maze #3 eennwwWWWWeeeeeesswwwwwwwnNN Maze #4 swwwnnnnnneeesssSSS Maze #5 eennwwWWWWeeeeeesswwwwwwwnNN Maze #6 wsseenN Maze #7 WWWswNNseeeeeennnnwwwwwwwssEEEE Maze #8 EwnnnnnnneessssssSS Maze #9 Impossible. Maze #10 E Maze #11 W Maze #12 N Maze #13 eesWnwS Maze #14 eSwsEneSwsEneSwsEneSwsEneSwsEneSwsEneSwsEneSwsEneSwsEneSwsEE Maze #15 nwwwnnnwwnneSwwsssseeennnWWnwSSSnnwwssseEEEEEEEEEEseNenW Maze #16 sseesssssssssssssseennnnnnneeeeEEwwwwwwsseeeeeeenennwSSesWWWWWWWeeeeeennwwwwwwwsSSSSSneeeeeeeeenennnnnnnnnnwwwwwwwwwwnwwsssssssssssssseEEEEEEEEEEEEEseNNNNNNNwnEEwnnnnnnwwwwwwwwwwwwwnwwwwnneeeeeeeeeeeeeeeeeeessssssssSSSSSSSSSS Maze #17 wwssseeeeeeennnnnnnwwwwwwwwwwwssssssssssseeeeeeeeeeeeeeennnnnnnnnnnnnnnwwwwwwwwwwwwwwwwwnneeeeeeeeeeeeeeeeeeessssssssssssssssssswwwwwwwwwwwwwwwwwwwnnnnnnnnnnnnnnneeeeeeeeeeeeeeessssssssssswwwwwwwwwwwnnnnnnneeeeesS Maze #18 EwsseeeseeennnnnnnwwwwwwwwwwwssssssssssseeeeeeeeeeeeeeennnnnnnnnnnnnnnwwwwwwwwwwwwwwwwwnneeeeeeeeeeeeeeeeeeessssssssssssssssssswwwwwwwwwwwwwwwwwwwnnnnnnnnnnnnnnneeeeeeeeeeeeeeessssssssssswwwwwwwwwwwnnnnnnneeeeesSSnnnwwwwwssssssseeeeeeeeeeennnnnnnnnnnwwwwwwwwwwwwwwwssssssssssssssseeeeeeeeeeeeeeeeeeennnnnnnnnnnnnnnnnnnwwwwwwwwwwwwwwwwwwwsseeeeeeeeeeeeeeeeessssssssssssssswwwwwwwwwwwwwwwnnnnnnnnnnneeeeeeeeeeessssssswwwnWWennwwsS Maze #19 EEEEEEEEEEEEEEEEEneSSSSSSSSSSSSSSSSSesWWWWWWWWWWWWWWWWWswNNNNNNNNNNNNNNNwnEEEEEEEEEEEEEEEwssssssssssseesseennnnnnnnnnnnnnnwwsSSSSSSSSSSSSSnnnnnnnnnnnnnneessssssssssssssswWWWWWWWWWWWWWennnnnnnnnwwnnwwssssssssssssseenNNNNNNNNNNNsssssssssssswwnnnnnnnnnnnnneEEEEEEEEEEEwwwwwwwwwwwwnneeeeeeeeeeeeesSSSSSSSSSnnnnnnnnnneessssssssssswWWWWWWWWWeeeeeeeeeesswwwwwwwwwwwnNNNNNNNsssssssswwnnnnnnnnneEEEEEEEwwwwwwwwnneeeeeeeeesSSSSSnnnnnneessssssswWWWWWeeeeeesswwwwwwwnNNNsssswwnnnnneEEEwwwwnneeeeesSS Maze #20 EneSSnneessswWWennwwsSSSesesswwsssswwnnnnnneEEneSSSnnnwwnneeeenneesseessswwwwssswWWnwSSSnnenennwwwwsssssseEEneSSSwwssseeseeeeeennwwnnnnwwssswWWnwSSSnnenennwwwwsssssseEEEEEEEEwwwwwnwwnneeeennneessseeeeseeeesswwwwswwNNNssseeneeeennwwwwnwWWswNNNssseesswwwwwwwnwnneeeennneEEseNNNsswwssseeeeseennneennnwwwwnwWWswNNNsseessswwwwssswwnnnnwwnneeeennneEEseNNNwwnnneeeeeessswwnwWWswNNseeeeseennnwwwwnwwwsEEEEEEneSSSnnwwwwwwsseeeeseEEneSSSnnwwnnneeneeesswsssesswwWWnwSSSnneennnwwwwnwwssswwssseeeeseEEneSSSwwssseessseenennwnnwWWnwSSSesesswwwwswwnnnneeseEEneSSnwwwwnwwsssseeneeeEEwnnwwnnneeeessesswSwsE Maze #21 essesesseeseseseeeesseeessssessessseeNNNNNNNNNNNNNNNNNenWWWWWWWWWWWWWWWWW Maze #22 Impossible. Maze #23 Impossible. Maze #24 sSSSSSSSSSSSSSSSSS Maze #25 eesswwsseesswwsseesswwsseesswwsseesswwsE Maze #26 wwwwwwwwwwwwwwwwwW
这道题好像有点问题,不过如果输出结果是对的还是可以AC,有的代码在有的数据不是最短路径也可以AC