poj 1475 Pushing Boxes 推箱子(双bfs)
题目链接:http://poj.org/problem?id=1475
一组测试数据:
7 3
###
.T.
.S.
#B#
...
...
...
结果:
//解题思路:先判断盒子的四周是不是有空位,如果有,则判断人是否能到达盒子的那一边,能的话,把盒子推到空位处,然后继续
AC代码:
- //解题思路:先判断盒子的四周是不是有空位,如果有,则判断人是否能到达盒子的那一边,能的话,把盒子推到空位处,然后继续
- #include<iostream>
- #include<algorithm>
- #include<cstdio>
- #include<cstring>
- #include<queue>
- #include<string>
- #include<cmath>
- using namespace std;
- int bx,by,sx,sy,tx,ty;
- int m,n,dir[][]={-,,,,,-,,};//注意题目要求的是n、s、w、e的顺序,因为这个wa了一次
- char op[]={'n','s','w','e'};
- bool mark[][][];//标记箱子四周的位置时候已被用过
- int vis[][];//标记人走过的位置
- char map[][];
- struct BB//盒子
- {
- int x,y,SX,SY;//SX,SY表示当前箱子固定了,人所在的位置
- string ans;
- }bnow,beed;
- struct YY//人
- {
- int x,y;
- string ans;
- }ynow,yeed;
- char up(char c)
- {
- return (c-'a'+'A');
- }
- //aa,bb 表示当前盒子的位置;ss,ee表示起点;
- bool bfs2(int s,int e,int aa,int bb,int ss,int ee)//寻找当前人,是否能够到达盒子指定的位置;
- {
- queue<YY>yy;
- if(s< || s>m || e< || e>n || map[s][e] == '#') return false;
- ynow.x = ss; ynow.y = ee; ynow.ans="";
- memset(vis,,sizeof(vis));
- vis[aa][bb] =;//不能经过盒子
- vis[ss][ee] = ;//起点标记为
- yy.push(ynow);
- while(!yy.empty())
- {
- ynow = yy.front();
- yy.pop();
- if(ynow.x == s && ynow.y == e)
- {
- return true;
- }
- for(int i=;i<;i++)
- {
- yeed.x = ynow.x+dir[i][];
- yeed.y = ynow.y+dir[i][];
- if(yeed.x> && yeed.x<=m && yeed.y> && yeed.y<=n && !vis[yeed.x][yeed.y] && map[yeed.x][yeed.y]!='#')
- {
- yeed.ans = ynow.ans+op[i];//记录走过的路径
- vis[yeed.x][yeed.y] = ;
- yy.push(yeed);
- }
- }
- }
- return false;
- }
- bool bfs1()
- {
- queue<BB>bb;
- bnow.x = bx;bnow.y=by;bnow.ans="";
- bnow.SX = sx;bnow.SY=sy;
- bb.push(bnow);
- while(!bb.empty())
- {
- bnow=bb.front();
- bb.pop();
- if(bnow.x == tx && bnow.y==ty)
- {
- return true;
- }
- for(int i=;i<;i++) //盒子周围的四个方向;
- {
- beed.x = bnow.x+dir[i][];
- beed.y = bnow.y+dir[i][];
- if(beed.x> && beed.x<=m && beed.y> && beed.y<=n && !mark[beed.x][beed.y][i] && map[beed.x][beed.y]!='#')
- {
- if(bfs2(beed.x-*dir[i][],beed.y-*dir[i][],bnow.x,bnow.y,bnow.SX,bnow.SY))//如果能推到yeed,则需要判断人是否能到达,它的上一个点;
- {
- beed.SX = bnow.x;//推完箱子后,人的位置在箱子上
- beed.SY = bnow.y;
- beed.ans=bnow.ans+ynow.ans+up(op[i]);//当前的加上推箱子的加上目前挨着推的;
- mark[beed.x][beed.y][i] = true;
- bb.push(beed);
- }
- }
- }
- }
- return false;
- }
- int main()
- {
- int T,k=;
- while(scanf("%d %d",&m,&n) && m+n)
- {
- memset(mark,false,sizeof(mark));
- for(int i=;i<=m;i++)
- for(int j=;j<=n;j++)
- {
- scanf(" %c",&map[i][j]);
- if(map[i][j] == 'S')
- {
- sx=i;sy =j;
- }
- if(map[i][j] == 'T')
- {
- tx = i;ty = j;
- }
- if(map[i][j] == 'B')
- {
- bx = i;by = j;
- }
- }
- printf("Maze #%d\n",k++);
- if(bfs1()) printf("%s\n\n",bnow.ans.c_str());//少个换行wa了一次
- else printf("Impossible.\n\n");
- }
- return ;
- }
poj 1475 Pushing Boxes 推箱子(双bfs)的更多相关文章
- POJ 1475 Pushing Boxes 搜索- 两重BFS
题目地址: http://poj.org/problem?id=1475 两重BFS就行了,第一重是搜索箱子,第二重搜索人能不能到达推箱子的地方. AC代码: #include <iostrea ...
- (poj 1475) Pushing Boxes
Imagine you are standing inside a two-dimensional maze composed of square cells which may or may not ...
- [poj P1475] Pushing Boxes
[poj P1475] Pushing Boxes Time Limit: 2000MS Memory Limit: 131072K Special Judge Description Ima ...
- HDU 1475 Pushing Boxes
Pushing Boxes Time Limit: 2000ms Memory Limit: 131072KB This problem will be judged on PKU. Original ...
- HDU 1254 推箱子(BFS)
Problem Description 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不 ...
- HDU1254 推箱子(BFS) 2016-07-24 14:24 86人阅读 评论(0) 收藏
推箱子 Problem Description 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推 ...
- HDU 1254 推箱子(BFS加优先队列)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1254 推箱子 Time Limit: 2000/1000 MS (Java/Others) Me ...
- suseoj 1212: 推箱子问题(bfs)
1212: 推箱子问题 时间限制: 1 Sec 内存限制: 128 MB提交: 60 解决: 13[提交][状态][讨论版][命题人:liyuansong] 题目描述 码头仓库是划分为n×m个格子 ...
- AcWing:172. 立体推箱子(bfs)
立体推箱子是一个风靡世界的小游戏. 游戏地图是一个N行M列的矩阵,每个位置可能是硬地(用”.”表示).易碎地面(用”E”表示).禁地(用”#”表示).起点(用”X”表示)或终点(用”O”表示). 你的 ...
随机推荐
- CDHtmlDialog 基本使用
跳转 Navigate("res://tt.exe/#138"); 138是html的资源号 输入框的Get,set HRESULT CTTDlg::OnButtonCancel( ...
- Eclipse中执行maven命令
1.如下图,右击需要执行maven命令的工程,选择"Debug As"或"Run As",再选择"Maven build..." 进行如上操 ...
- Openshift 用户,角色和RBAC
OCP中的权限管理沿用的Kubernetes RBAC机制,授权模式主要取决于下面几个因数 Rules 针对主要对象的操作权限,比如建立Pod Sets of permitted verbs on a ...
- vmware 下 ubuntu 不能全屏显示 的解决方法
vmware 下 ubuntu 不能全屏显示 在 vmware 下 安装 ubuntu后,默认分辨率是 800 * 600,可以设置以全屏显示: 设置步骤: vmware 下启动 虚拟机,即 启动 u ...
- vc6下unicode支持
最近在研究一个串口程序,要启用unicode支持,发现还挺麻烦的. VC6.0设定UNICODE编译环境 VC++ 6.0支持Unicode编程,但默认的是ANSI,所以开发人员只需要稍微改变一下编写 ...
- js 倒计时跳转页面
<script type="text/javascript">var i = 5; var intervalid; intervalid = setInterval(& ...
- 爪哇国新游记之十三----XML文件读写
/** * XML读写示例 * @author hx * */ public class XmlReaderWriter{ /** * 读取一个XML文件,返回一个雇员链表 * @param file ...
- 请远离include_once和require_once[转]
来自:http://www.poluoluo.com/jzxy/201306/216921.html 尽量使用include, 而不是include_once, 理由是 include_once需要查 ...
- docker基本
安装(centos): Docker 运行在 CentOS 7 上,要求系统为64位.系统内核版本为 3.10 以上.Docker 运行在 CentOS-6.5 或更高的版本的 CentOS 上,要求 ...
- 使用Firefly编写简易聊天室
1.创建工程命令行下输入firefly-admin.py createproject chat_rooms(linux在终端输入),<ignore_js_op> firefly会在C盘Us ...