博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Pushing Boxes POJ - 1475 (嵌套bfs)
阅读量:5218 次
发布时间:2019-06-14

本文共 5482 字,大约阅读时间需要 18 分钟。

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 题意:推箱子,要求出箱子移动最少(其次是人移动最少)的路径 思路: ①最优先的是箱子移动的步数,我们可以先对箱子进行bfs,则可以找到箱子移动最少的路径 ②箱子移动最少但不代表合法,(因为也许这条路径中箱子在某处处于角落),其次是除了箱子的步数,人的步数也是尽可能的小,那么对于人移动的最小路径,我们仍然进行一次bfs ③首先是箱子bfs枚举四个方向(x+ways【i】【0】,y+ways【i】【1】),然后为了验证箱子移动是否合法,对人进行一次bfs,从人起点到(x-ways【i】【0】,y-ways【i】【1】),如果能到就返回1(一定最短),说明合法。 因为箱子需要向一个方向移动时,人必须站在(x-ways【i】【0】,y-ways【i】【1】)的位置推,然后才是箱子和人一起向该方向在移动一次 ④至于路劲记录,可以用记录前置路径,然后递归的方式,(这样的话需要记录下所有移动的路径信息,从答案向前推到)。 也可以在结构体中加入string,然后  每次移动 = 原来路径+人移动路径 + 人箱子移动路径 注:局部变量不会自动初始化,全局变量才会。
#include
#include
#include
;#include
using namespace std;int r,c;char maps[22][22];int ways[4][2] = {
1,0,-1,0,0,1,0,-1};string forw[4] = {
"s","n","e","w"};string FORW[4] = {
"S","N","E","W"};string ans;struct Node{ int sx,sy; int bx,by; string path; Node(int sx=0,int sy=0,int bx=0,int by=0,string path=""):sx(sx),sy(sy),bx(bx),by(by),path(path) {}}start;struct Peo{ int x,y; string path; Peo(int x=0,int y=0,string path=""):x(x),y(y),path(path) {}};bool check(int x,int y){ if(x < 1 || x > r || y < 1 || y > c) return 0; if(maps[x][y] == '#') return 0; return 1;}bool cal(int sx,int sy,int ex,int ey,int tx,int ty,string &path){ queue
que; while(!que.empty()) que.pop(); que.push(Peo(sx,sy)); bool vis[22][22]; memset(vis,0,sizeof(vis)); vis[tx][ty] = vis[sx][sy] = 1; while(!que.empty()) { Peo tmp = que.front(); que.pop(); if(tmp.x == ex && tmp.y == ey) { path = tmp.path; return 1; } for(int i=0; i<4; i++) { int xx = tmp.x + ways[i][0]; int yy = tmp.y + ways[i][1]; if(check(xx,yy) && !vis[xx][yy]) { string t_path = tmp.path + forw[i]; vis[xx][yy] = 1; que.push(Peo(xx,yy,t_path)); } } } return 0;}bool bfs(){ bool vis[22][22]; memset(vis,0,sizeof(vis)); queue
que; while(!que.empty()) que.pop(); start.path = ""; que.push(start); vis[start.bx][start.by] = 1; while(!que.empty()) { Node tmp = que.front(); que.pop(); for(int i=0; i<4; i++) { int bx = tmp.bx + ways[i][0]; int by = tmp.by + ways[i][1]; int ex = tmp.bx - ways[i][0]; int ey = tmp.by - ways[i][1]; string path = ""; if(check(bx,by) && check(ex,ey) && !vis[bx][by]) { if(cal(tmp.sx,tmp.sy,ex,ey,tmp.bx,tmp.by,path)) { vis[bx][by] = 1; string t_path = tmp.path + path + FORW[i]; if(maps[bx][by] == 'T') { ans = t_path; return 1; } que.push(Node(tmp.bx,tmp.by,bx,by,t_path)); } } } } return 0;}int main(){ int cas = 0; while(~scanf("%d%d",&r,&c) && r && c) { char s[22]; for(int i=1; i<=r; i++) { scanf("%s",&s); for(int j=1; j<=c; j++) { maps[i][j] = s[j-1]; if(maps[i][j] == 'S') { start.sx = i; start.sy = j; } else if(maps[i][j] == 'B') { start.bx = i; start.by = j; } } } printf("Maze #%d\n", ++cas); int flag = bfs(); if(!flag) printf("Impossible.\n"); else cout<< ans << endl; puts(""); }}
View Code

 

 

转载于:https://www.cnblogs.com/iwannabe/p/10591819.html

你可能感兴趣的文章
WSDL 详解
查看>>
[转]ASP数组全集,多维数组和一维数组
查看>>
git学习
查看>>
C# winform DataGridView 常见属性
查看>>
逻辑运算和while循环.
查看>>
Nhiberate (一)
查看>>
c#后台计算2个日期之间的天数差
查看>>
安卓开发中遇到的小问题
查看>>
ARTS打卡第3周
查看>>
linux后台运行和关闭SSH运行,查看后台任务
查看>>
cookies相关概念
查看>>
CAN总线波形中ACK位电平为什么会偏高?
查看>>
MyBatis课程2
查看>>
桥接模式-Bridge(Java实现)
查看>>
svn客户端清空账号信息的两种方法
查看>>
springboot添加servlet的两种方法
查看>>
java的Array和List相互转换
查看>>
java的byte[]与String相互转换
查看>>
layui父页面执行子页面方法
查看>>
如何破解域管理员密码
查看>>