题目地址: 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. 与众不同 windows phone (26) - Contacts and Calendar(联系人和日历)

    原文:与众不同 windows phone (26) - Contacts and Calendar(联系人和日历) [索引页][源码下载] 与众不同 windows phone (26) - Con ...

  2. 14.4.3.4 Configuring InnoDB Buffer Pool Prefetching (Read-Ahead) 配置InnoDB Buffer pool 预读

    14.4.3.4 Configuring InnoDB Buffer Pool Prefetching (Read-Ahead) 配置InnoDB Buffer pool 预读 一个预读请求 是一个I ...

  3. POJ 3974 最长回文字串(manacher算法)

    题意:给出一个字符串,求出最长回文字串. 思路:一开始我直接上了后缀数组DC3的解法,然后MLE了.看了DISCUSS发现还有一种计算回文字串更加优越的算法,就是manacher算法.就去学习了一下, ...

  4. FindWindowEx使用方法

    函数功能:该函数获得一个窗体的句柄,该窗体的类名和窗体名与给定的字符串相匹配.这个函数查找子窗体,从排在给定的子窗体后面的下一个子窗体開始.在查找时不区分大写和小写. 函数原型:HWND FindWi ...

  5. Java 螺纹第三版 第一章Thread介绍、 第二章Thread创建和管理学习笔记

    第一章 Thread导论 为何要用Thread ? 非堵塞I/O      I/O多路技术      轮询(polling)      信号 警告(Alarm)和定时器(Timer) 独立的任务(Ta ...

  6. operation is executing and cannot be enqueued

    http://d2100.com/questions/29022 作为依赖关系的另一个 NSOperation 添加时不调用 NSOperation dealloc 使用文书我看到很多我自定义的 NS ...

  7. hdu Crazy Circuits

    Crazy Circuits 题目: 给出一个电路板,从+极出发到负极. 如今给你电路板上的最小电流限制,要你在电流平衡的时候求得从正极出发的最小电流. 算法: 非常裸的有源汇最小流.安有源汇最大流做 ...

  8. VSTO 为Office已有右键菜单添加自己的菜单项(word,Excel)

    原文:VSTO 为Office已有右键菜单添加自己的菜单项(word,Excel) private void AddRightMenu()         {            Microsoft ...

  9. 【UVA】10012 - How Big Is It?(暴力)

    使用DFS枚举所有的安排.每次加入后,当一个圆.他的立场是最大的,并已加入了圆环中的所有切线位置前面. 14383635 10012 option=com_onlinejudge&Itemid ...

  10. mysql视图学习总结

    转自http://www.cnblogs.com/wangtao_20/archive/2011/02/24/1964276.html 一.使用视图的理由是什么? 1.安全性.一般是这样做的:创建一个 ...