双重 BFS —— POJ 1475 Pushing Boxes

来源:互联网 发布:华为网络技术培训 编辑:程序博客网 时间:2024/05/22 08:10

对应 POJ 题目:点击打开链接

Pushing Boxes
Time Limit: 2000MS Memory Limit: 131072KTotal Submissions: 5005 Accepted: 1733 Special Judge

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? 

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 7SB....T1 7SB..#.T7 11############T##......##.#.#..#####....B....##.######..##.....S...############8 4.....##..#...#...#.B.##S....###T0 0

Sample Output

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

题意:

        一人一箱一目标,问人是否可以把箱子推到目标点,如果可以,则把最小推动次数 min_push 的路径打印出来,如果 min_push 相同的有多种解法,则把移动次数 min_move 最小的路径打印出来,如果还有多种解,则打印任意一种即可。


思路:

        对箱子进行 BFS 的过程中,箱子每往一个方向走,就需要判断人是否可以从当前位置走到箱子的反方向;该题也是可重复式搜索,比如这样的图:

        

        人虽然不能直接走到箱子上面,但可以先把它推上去,然后再从上面推下来,可以推到目标点。所以可以用一个 3 维数组 vis[4][size][size] 标记, vis[i][x][y] = 1 表示坐标 (x, y) 已经从其他坐标往 i 方向推得到过。箱子和人都要求移动次数最少,所以都要使用扩散式搜索,这样找到的第一个解才是最优的。

        Discuss 说数据有问题~ 就是像上图的情况输出 Impossible. 也能 AC。。。

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <string>#include <queue>#include <iostream>#define QUESIZE 100000#define MAPSIZE 30char Map[MAPSIZE][MAPSIZE];char dir_box[] = {'N', 'S', 'W', 'E'};char dir_man[] = {'n', 's', 'w', 'e'};int gor[] = {-1, 1, 0, 0};int goc[] = {0, 0, -1, 1};int map_r, map_c;using namespace std;typedef struct{string way;int r, c; // 箱子的位置int mr, mc; // 人的位置}Point;int judge_edge(int r, int c){if(r >= 0 && r < map_r && c >= 0 && c < map_c && Map[r][c] != '#')return 1;return 0;}string man_can_go(Point beg, Point end){queue<Point>que;int vis[MAPSIZE][MAPSIZE];memset(vis, 0, sizeof(vis));beg.way = "";que.push(beg);vis[beg.mr][beg.mc] = 1;while(!que.empty()){Point cur = que.front();que.pop();if(cur.mr == end.mr && cur.mc == end.mc)return cur.way;for(int i = 0; i < 4; i++){Point next;next.mr = cur.mr + gor[i];next.mc = cur.mc + goc[i];next.way = cur.way;if(!judge_edge(next.mr, next.mc) || vis[next.mr][next.mc] || (next.mr == beg.r && next.mc == beg.c))continue;next.way.append(1, dir_man[i]);que.push(next);vis[next.mr][next.mc] = 1;}}return "Impossible.";}string box_bfs(Point box, Point tar){queue<Point>que;int vis[4][MAPSIZE][MAPSIZE];memset(vis, 0, sizeof(vis));que.push(box);while(!que.empty()){Point cur = que.front();que.pop();if(cur.r == tar.r && cur.c == tar.c)return cur.way;for(int i = 0; i < 4; i++){Point next;// 箱子的下一个坐标next.r = cur.r + gor[i];next.c = cur.c + goc[i];// 人的下一个坐标next.mr = cur.r - gor[i];next.mc = cur.c - goc[i];next.way = cur.way;if(vis[i][next.r][next.c]) // 该点已从其他点往 i 方向得到过continue;if(!judge_edge(next.r, next.c))continue;if(!judge_edge(next.mr, next.mc))continue;string mway = man_can_go(cur, next);if(mway != "Impossible."){next.way.append(mway);next.way.append(1, dir_box[i]);// 箱子已经移动,人的位置为原来箱子的位置next.mr = cur.r;next.mc = cur.c;que.push(next);vis[i][next.r][next.c] = 1;}}}return "Impossible.";}int main(){#if 0freopen("in.txt","r",stdin);#endifint w = 0;while(scanf("%d%d", &map_r, &map_c) == 2, map_r && map_c){int i, j;Point box;Point tar;for(i = 0; i < map_r; i++){scanf("%s", Map[i]);for(j = 0; j < map_c; j++){if(Map[i][j] == 'S') {box.mr = i; box.mc = j;}if(Map[i][j] == 'B') {box.r = i; box.c = j; box.way = "";}if(Map[i][j] == 'T') {tar.r = i; tar.c = j; tar.way = "";}}}cout << "Maze #" << ++w << endl;cout << box_bfs(box, tar) << endl << endl;}return 0;}








0 0
原创粉丝点击