题目地址: 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的更多相关文章

  1. poj 1475 Pushing Boxes 推箱子(双bfs)

    题目链接:http://poj.org/problem?id=1475 一组测试数据: 7 3 ### .T. .S. #B# ... ... ... 结果: //解题思路:先判断盒子的四周是不是有空 ...

  2. (poj 1475) Pushing Boxes

    Imagine you are standing inside a two-dimensional maze composed of square cells which may or may not ...

  3. [poj P1475] Pushing Boxes

    [poj P1475] Pushing Boxes Time Limit: 2000MS   Memory Limit: 131072K   Special Judge Description Ima ...

  4. HDU 1475 Pushing Boxes

    Pushing Boxes Time Limit: 2000ms Memory Limit: 131072KB This problem will be judged on PKU. Original ...

  5. poj 1475 || zoj 249 Pushing Boxes

    http://poj.org/problem?id=1475 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=249 Pushin ...

  6. Pushing Boxes(广度优先搜索)

    题目传送门 首先说明我这个代码和lyd的有点不同:可能更加复杂 既然要求以箱子步数为第一关键字,人的步数为第二关键字,那么我们可以想先找到箱子的最短路径.但单单找到箱子的最短路肯定不行啊,因为有时候不 ...

  7. 『Pushing Boxes 双重bfs』

    Pushing Boxes Description Imagine you are standing inside a two-dimensional maze composed of square ...

  8. POJ1475 Pushing Boxes(双搜索)

    POJ1475 Pushing Boxes  推箱子,#表示墙,B表示箱子的起点,T表示箱子的目标位置,S表示人的起点 本题没有 Special Judge,多解时,先最小化箱子被推动的次数,再最小化 ...

  9. UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题

    很少用bfs进行最短路搜索,实际BFS有时候挺方便得,省去了建图以及复杂度也降低了O(N*M): UVA 11624 写的比较挫 #include <iostream> #include ...

随机推荐

  1. HTML属性

  2. 忽然想到:QProcess的Read功能太强,如果有什么搞不定的,可以调用外部程序 good

    这样就可以用其它语言来无限扩展它的功能了,比如golang,比如Delphi

  3. Delphi中复制带有String的记录结构时不能使用Move之类的内存操作函数

    请看下面的代码: program TestRecord; {$APPTYPE CONSOLE} uses  SysUtils,  Math; type  TRecordA = record    Na ...

  4. navicat for mysql 显示中文乱码解决办法

      最近遇到一个问题,用navicat for mysql 打开数据库时全都显示的是乱码(在用程序代码插入数据之前确保字符不是乱码),遇到问题就的寻求解决之道,百度了好长时间也没解决,网上那些解决办法 ...

  5. PhoneGap-----Contacts

    Everything in the code!!! <!DOCTYPE html> <html> <head> <title>Contact Examp ...

  6. Qt 4.7.4 完美动态编译发布动态调试,以及静态编译发布

    首先是准备工作,去QT主页下载独立的QT类库安装包以及完整QT SDK安装包,还有QT Creator for windows 版 下载地址:http://qt.nokia.com/downloads ...

  7. C++ 指针—02 指针与引用的对照

    ★同样点: ●都是地址的概念: 指针指向一块内存,它的内容是所指内存的地址:而引用则是某块内存的别名. ★不同点: ●指针是一个实体,而引用仅是个别名: ●引用仅仅能在定义时被初始化一次,之后不可变: ...

  8. [Cocos2d-x]节点之间的相互通讯

    在做.NET开发时,对象之间的相互通讯一般使用事件(event)实现,事件概念是.NET对Delegate的封装. 在Cocos2d-x开发过程中,对象之间的通讯刚开始时不知道如何实现,于是想到c++ ...

  9. 使用MYCAT作为Mysql HA的中间件(转)

    记得在上一篇文章“Java集群--大型网站是怎样解决多用户高并发访问的”的结尾处本人阐述了数据库的高可用的一种方案----实现主从部署,那么今天,就让我聊聊本人关于数据库的一些所思所想吧! 下面是本人 ...

  10. 将svnkit转成dlls时的问题

    未处理 System.TypeInitializationException Message="“org.tmatesoft.svn.core.internal.wc.DefaultSVNO ...