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 ...
随机推荐
- Swift - UIColor使用自定义的RGB配色
1,比如rgb 色值为55. 186 .89 那么给UIColor设置里面要除以255 1 UIColor(red: 55/255, green: 186/255, blue: 89/255, alp ...
- C#对HTTP数据还原
使用C#对HTTP数据还原 [创建时间:2016-05-12 00:19:00] NetAnalyzer下载地址 在NetAnalyzer2016中加入了一个HTTP分析功能,很过用户对此都很感兴 ...
- Oracle中四种循环(GOTO、For、While、Loop)
DECLARE x number; BEGIN x:=9; <<repeat_loop>> --循环点 x:=x-1; DBMS_OUTPUT.PUT_LINE(X); IF ...
- 那些年我们装过的数据库---盘点sqlserver2008安装时遇到的各种的问题(持续更新中)
给自己安过sqlServer2008,也给好多同学安过sqlServer2008,期间遇到了好多不同的另人心烦的问题,在这里整理一下,(涉及到的部分方法是在网上找的,有些也没试过,仅仅是在这里整理一下 ...
- [cocos2d-x]屏幕自适应解决的方法
近期在写一个项目,要求pc,ipad,andriod平台上都能够执行,所以选择用cocos2d-x来开发. 我们的资源大小是1024*768的,在pc上和苹果上都是没有问题的,但是到了andriod上 ...
- 【机器学习实验】学习Python来分类现实世界的数据
引入 一个机器能够依据照片来辨别鲜花的品种吗?在机器学习角度,这事实上是一个分类问题.即机器依据不同品种鲜花的数据进行学习.使其能够对未标记的測试图片数据进行分类. 这一小节.我们还是从scikit- ...
- 《TCP/IP作品详细解释2:实现》笔记--Radix树路由表
通过IP完整的路由是路由机制,它通过搜索路由表来确定从哪个分组被发送的接口执行此,它是不一样的路由策略,路由策略 它是一组规则,这些规则可以被用来确定哪些路由编程到路由表,Net/3内核实现的路由机制 ...
- hdu1695(莫比乌斯反演)
传送门:GCD 题意:求[1,n],[1,m]gcd为k的对数. 分析:莫比乌斯入反演门题,gcd(x,y)==k等价于gcd(x/k,y/k)==1,求出[1,n][1,m]互质的对数,在减去[1, ...
- C语言数组
在C语言中,对于三维或三维以上数组的使用并没有很好的支持,而且使用率也非常的低,后面会对三维数组做一些简单的分析,这篇文章主要以二维数组来探讨一些C语言中数组使用的相关概念和技巧. 1 一个var[i ...
- hdu1881(贪心+dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1881 分析:按照结束时间从小到大排序,然后以每个结束点为容量进行01背包,选入的必定符合条件的. 因为 ...