POJ Pushing Boxes 优先队列BFS
来源:互联网 发布:python画图教程 编辑:程序博客网 时间:2024/06/01 08:20
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.
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.
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
题意还是很简单因为大家都玩过这样的游戏,只有一个箱子,求完成游戏的最短步数,在推箱子的最短步数下加上最短的人行走的步数,总步数分为了两部分一个是人走一个是推箱子,但是大前提的箱子步数最小,所以我用优先队列,每次弹出的都是箱子步数最小,当箱子步数相等的时候弹出人走的步数最小,用use标记该状态是否走过,简单易行。
#include<stdio.h>#include<queue>#include<string>#include<string.h>#include<iostream>#include<algorithm>using namespace std;int use[30][30][30][30];char map[25][25];int dir[4][2]={-1,0,0,1,-1,0,0,1};char wr[4]={'n','s','w','e'};char wb[4]={'N','S','W','E'};int n,m;int check(int x,int y){ if(x>=0&&x<n&&y>=0&&y<m) return 1; else return 0;}struct node{ int rx,ry; int bx,by; int r_step; int b_step; string ans; bool operator <(const node &a) const { if(b_step==a.b_step) { return r_step>a.r_step; } return b_step>a.b_step; }};int BFS(int x1,int y1,int x2,int y2){ node next,now; use[x1][y1][x2][y2]=1; string a=""; now.rx=x1; now.ry=y1; now.bx=x2; now.by=y2; now.r_step=0; now.b_step=0; now.ans=a; priority_queue<node>que; que.push(now); while(!que.empty()) { now=que.top(); que.pop(); if(map[now.bx][now.by]=='T') { cout<<now.ans<<"\n\n"; return 1; } for(int i=0;i<4;i++) { next.rx=now.rx+dir[i][0]; next.ry=now.ry+dir[i][1]; if(check(next.rx,next.ry)==0||map[next.rx][next.ry]=='#') continue; if(next.rx==now.bx&&next.ry==now.by) { next.bx=now.bx+dir[i][0]; next.by=now.by+dir[i][1]; if(use[next.rx][next.ry][next.bx][next.by]==0&&check(next.bx,next.by)==1&&map[next.bx][next.by]!='#') { use[next.rx][next.ry][next.bx][next.by]=1; next.ans=now.ans; next.r_step=now.r_step; next.b_step=now.b_step+1; next.ans.append(1,wb[i]); que.push(next); } } else { next.bx=now.bx; next.by=now.by; if(!use[next.rx][next.ry][next.bx][next.by]) { use[next.rx][next.ry][next.bx][next.by]=1; next.ans=now.ans; next.r_step=now.r_step+1; next.b_step=now.b_step; next.ans.append(1,wr[i]); que.push(next); } } } } return -1;}int main(){ int o=0; while(scanf("%d%d",&n,&m)!=EOF) { o++; if(n==0&&m==0) break; for(int i=0;i<n;i++) scanf("%s",map[i]); memset(use,0,sizeof(use)); int x1,x2,y1,y2; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(map[i][j]=='S') { x1=i; y1=j; } if(map[i][j]=='B') { x2=i; y2=j; } } } printf("Maze #%d\n",o); int a=BFS(x1,y1,x2,y2); if(a==-1) printf("Impossible.\n\n"); }return 0;}
0 0
- POJ Pushing Boxes 优先队列BFS
- POJ-1475-Pushing Boxes(BFS)
- POJ 1475 Pushing Boxes 搜索- 两重BFS
- Pushing Boxes (poj 1475 嵌套bfs)
- poj 1475 Pushing Boxes(bfs)
- POJ 1475 Pushing Boxes(BFS)
- POJ 1475 Pushing Boxes(BFS)
- POJ 1475 Pushing Boxes(嵌套BFS)
- POJ 1475 Pushing Boxes 嵌套BFS -
- poj 1475 Pushing Boxes(推箱子游戏,bfs+bfs)
- POJ 1475 Pushing Boxes 【A*bfs套bfs】
- POJ1475 Pushing Boxes(BFS+BFS)
- POJ 1475 Pushing Boxes (双重BFS/推箱子游戏)
- POJ 1475 Pushing Boxes(推箱子游戏 内嵌BFS)
- 双重 BFS —— POJ 1475 Pushing Boxes
- poj 1475 Pushing Boxes
- (poj 1475) Pushing Boxes
- POJ - 1475 Pushing Boxes
- 改进后的客户端服务器聊天功能,此版本比上版本多了加锁处理,多了客户端数量控制,多了线程join操作
- Andriod 中dp dip px sp pt 的 区别
- 数据库 空格问题
- Sigmaplot 13下载有没有免费的
- 二叉查找树--插入、删除、查找
- POJ Pushing Boxes 优先队列BFS
- 使用SAX方式解析XML
- Writing an alsa driver译文-第二章 PCI驱动的基本流程
- SQLServer控制用户访问权限表
- 关于ajax前台验证用户名是否存在
- python笔记---OS模块内建方法
- 深入浅出 RecyclerView
- 判断客户端是否是iOS或者Android
- SecureCRT中常用linux命令