POJ 1475 Pushing Boxes 搜索- 两重BFS
题目地址: http://poj.org/problem?id=1475
两重BFS就行了,第一重是搜索箱子,第二重搜索人能不能到达推箱子的地方。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std; typedef long long LL;
const int N=21;
const LL II=1000000007; int r,c,begx,begy,endx,endy,begsx,begsy;
char maps[21][21];
int t[4][2]={-1,0,1,0,0,1,0,-1};
char P[4]={'N', 'S', 'E', 'W'};
char M[4]={'n', 's', 'e', 'w'};
int mark[21][21]; struct xhr
{
int x,y;
string ans;
}F,G; struct xhx
{
int bx,by,px,py;
string ans;
}f,g; bool inmap(int x,int y)
{
return x>=1&&x<=r&&y>=1&&y<=c;
} bool ren(int bx,int by,int ex,int ey)//人bfs
{
queue<xhr> P;
bool Mark[21][21];
memset(Mark,0,sizeof(Mark));
Mark[bx][by]=1;
Mark[f.bx][f.by]=1;
F.x=bx; F.y=by;
F.ans="";
if(bx==ex&&by==ey)
return true;
P.push(F);
while(!P.empty())
{
F=P.front();
P.pop();
for (int i=0;i<4;i++)
{
G.x=F.x+t[i][0];
G.y=F.y+t[i][1];
if(!inmap(G.x, G.y)||maps[G.x][G.y]=='#') continue;
if(!Mark[G.x][G.y])
{
Mark[G.x][G.y]=1;
G.ans=F.ans+M[i];
if(G.x==ex&&G.y==ey)
{
F=G;
return true;
}
P.push(G);
}
}
}
return false;
} void bfs()//箱子bfs
{
int i;
queue<xhx> Q;
f.bx=begx; f.by = begy;
f.px=begsx; f.py = begsy;
f.ans="";
mark[begx][begy]=1;
Q.push(f);
while (!Q.empty())
{
f=Q.front();
Q.pop();
for (i=0;i<4;i++)
{
int newx=f.bx+t[i][0],newy=f.by+t[i][1];//箱子要到达的地方
int tx=f.bx-t[i][0],ty=f.by-t[i][1];//人要到达的地方
if (!inmap(newx, newy)||maps[newx][newy]=='#'||!inmap(tx, ty)||maps[tx][ty]=='#'||mark[newx][newy])
continue;
if (ren(f.px,f.py,tx,ty))
{
g.bx=newx; g.by=newy;
g.px=f.bx; g.py=f.by;
g.ans=f.ans+F.ans+P[i];
if(g.bx==endx&&g.by==endy)
{
cout<<g.ans<<endl<<endl;
return ;
}
mark[newx][newy]=1;
Q.push(g);
}
}
}
printf("Impossible.\n\n");
} int main()
{
int ci=1,i,j;
while(scanf("%d%d",&r,&c)&&(r + c))
{
memset(mark,0,sizeof(mark));
for (i=1;i<=r;++i)
scanf("%s",maps[i]+1);
for (i=1;i<=r;++i)
for (j=1;j<=c;++j)
{
if(maps[i][j]=='B') begx=i, begy=j;
if(maps[i][j]=='T') endx=i, endy=j;
if(maps[i][j]=='S') begsx=i, begsy=j;
}
printf("Maze #%d\n",ci++);
bfs();
}
return 0;
}
POJ 1475 Pushing Boxes 搜索- 两重BFS的更多相关文章
- poj 1475 Pushing Boxes 推箱子(双bfs)
题目链接:http://poj.org/problem?id=1475 一组测试数据: 7 3 ### .T. .S. #B# ... ... ... 结果: //解题思路:先判断盒子的四周是不是有空 ...
- (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 ...
- poj 1475 || zoj 249 Pushing Boxes
http://poj.org/problem?id=1475 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=249 Pushin ...
- Pushing Boxes(广度优先搜索)
题目传送门 首先说明我这个代码和lyd的有点不同:可能更加复杂 既然要求以箱子步数为第一关键字,人的步数为第二关键字,那么我们可以想先找到箱子的最短路径.但单单找到箱子的最短路肯定不行啊,因为有时候不 ...
- 『Pushing Boxes 双重bfs』
Pushing Boxes Description Imagine you are standing inside a two-dimensional maze composed of square ...
- POJ1475 Pushing Boxes(双搜索)
POJ1475 Pushing Boxes 推箱子,#表示墙,B表示箱子的起点,T表示箱子的目标位置,S表示人的起点 本题没有 Special Judge,多解时,先最小化箱子被推动的次数,再最小化 ...
- UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题
很少用bfs进行最短路搜索,实际BFS有时候挺方便得,省去了建图以及复杂度也降低了O(N*M): UVA 11624 写的比较挫 #include <iostream> #include ...
随机推荐
- js获取并设置<p></p>的显示的值。
原文链接:http://www.nowamagic.net/librarys/posts/jquery/23 html()方法 此方法类似于JavaScript中的innerHTML属性,能够用来读取 ...
- Maven-1:下载&安装
(一)下载 下载网址:http://maven.apache.org/download.cgi 版本:3.0.5 (二)安装 1.解压至目录:F:\Kevin\software\apache-mave ...
- Java.util.zip adding a new file overwrites entire jar?(转)
ZIP and TAR fomats (and the old AR format) allow file append without a full rewrite. However: The Ja ...
- PHP+lghttpd + postgresql 环境搭建
PHP+lghttpd + postgresql 环境搭建 Linux 下PHP环境搭建 安装环境:PHP+lghttpd + postgresql 1:yum install lighttpd 红旗 ...
- 公司内网Debian Server上的openvpnserver搭建并通过SSH隧道转发到广域网
因为戴维营公司在长沙的学员,研发人员和北京的研发人员须要协同研发,故须要让北京的兄弟们增加到长沙办公室的内网,訪问版本号 管理server,于是採用在内网server上搭建一个OpenVPN服务,并把 ...
- IBinder在进程之间传递一个对象的形式(一)
主张 什么时候service通常被称为远程时的,用到aidl来定一个接口供service和client来使用,这个事实上就是使用Binder机制的IPC通信.当client bind service成 ...
- 整理自百度知道提问的几道Java编程题
蚂蚁爬杆 问题描述: 有一根27厘米的细木杆,在第3厘米.7厘米.11厘米.17厘米.23厘米这五个位置上各有一只蚂蚁.木杆很细,不能同时通过一只蚂蚁.开始时,蚂蚁的头朝左还是朝右是任意的,它们只会朝 ...
- Android 推断当前的界面是否是桌面的方法
在开发桌面飘浮控件的时候,须要通过service查看当前是不是桌面,从而控制漂浮窗的显现与消失,以下的代码就是推断是否是桌面的方法 /** * 推断当前界面是否是桌面 */ private boole ...
- 《Java并发编程实战》第十六章 Java内存模型 读书笔记
Java内存模型是保障多线程安全的根基,这里不过认识型的理解总结并未深入研究. 一.什么是内存模型,为什么须要它 Java内存模型(Java Memory Model)并发相关的安全公布,同步策略的规 ...
- hdu2563(递推dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2563 解题思路:要分两种情况来考虑,a(n)为向上,b(n)为向左跟向右,f(n)为当前方案数.a(n ...