hdu - 1254 推箱子 (bfs+bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1254
题目意思很简单,只要思路对就好。
首先考虑搬运工能否到达推箱子的那个点,这个可以根据箱子前进方向得出搬运工需要到达的目的地,用另一个bfs判断,然后就类似两个点的bfs那样用一个数组标记状态,
需要注意箱子在边上的情况。
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std; struct point
{
int x,y,nx,ny,step;
}; int n,m;
int maze[][];
int used[][][][];
int dir[][]={{-,},{,},{,-},{,}}; bool bfs1(int a,int b,int c,int d,int p,int q)
{
int mark[][];
memset(mark,,sizeof(mark));
queue<point>que;
point s;
s.x=a,s.y=b;
mark[s.x][s.y]=;
que.push(s);
while(!que.empty())
{
point e=que.front();que.pop();
//printf("%d %d\n",e.x,e.y);
if(e.x==c&&e.y==d) return true;
for(int i=;i<;i++)
{
s.x=e.x+dir[i][];
s.y=e.y+dir[i][];
if(s.x==p&&s.y==q) continue;
//printf("%d %d\n",s.x,s.y);
if(s.x>=&&s.x<n&&s.y>=&&s.y<m&&maze[s.x][s.y]!=&&!mark[s.x][s.y])
{
mark[s.x][s.y]=;
que.push(s);
}
}
}
return false;
} void bfs2(int a,int b,int c,int d)
{
memset(used,,sizeof(used));
queue<point>que;
point s,e;
s.x=a,s.y=b,s.nx=c,s.ny=d,s.step=;
que.push(s);
used[s.x][s.y][s.nx][s.ny]=;
while(!que.empty())
{
e=que.front();que.pop();
//printf("%d %d %d %d %d\n",e.x,e.y,e.nx,e.ny,e.step);
if(maze[e.x][e.y]==) {printf("%d\n",e.step);return;}
for(int i=;i<;i++)
{
s.x=e.x+dir[i][];
s.y=e.y+dir[i][];
s.nx=e.x;
s.ny=e.y;
if(s.x<||s.x>=n||s.y<||s.y>=m||maze[s.x][s.y]==||used[s.x][s.y][s.nx][s.ny]) continue;
if(i==)
{
if(e.x+>=n||maze[e.x+][e.y]==||(bfs1(e.nx,e.ny,e.x+,e.y,e.x,e.y)==false)) continue;
}
else if(i==)
{
if(e.x-<||maze[e.x-][e.y]==||(bfs1(e.nx,e.ny,e.x-,e.y,e.x,e.y)==false)) continue;
}
else if(i==)
{
if(e.y+>=m||maze[e.x][e.y+]==||(bfs1(e.nx,e.ny,e.x,e.y+,e.x,e.y)==false)) continue;
}
else if(i==)
{
if(e.y-<||maze[e.x][e.y-]==||bfs1(e.nx,e.ny,e.x,e.y-,e.x,e.y)==false) continue;
}
used[s.x][s.y][s.nx][s.ny]=;
s.step=e.step+;
que.push(s);
}
}
printf("-1\n");
} int main()
{
//freopen("a.txt","r",stdin);
int t,a,b,c,d;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
scanf("%d",&maze[i][j]);
if(maze[i][j]==)
{
a=i;
b=j;
}
else if(maze[i][j]==)
{
c=i;
d=j;
}
}
bfs2(a,b,c,d);
}
return ;
}
hdu - 1254 推箱子 (bfs+bfs)的更多相关文章
- HDU 1254 推箱子(BFS加优先队列)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1254 推箱子 Time Limit: 2000/1000 MS (Java/Others) Me ...
- HDU 1254 推箱子(BFS)
Problem Description 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不 ...
- HDU 1254 推箱子 BFS
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1254 题目分析: 做这道题,感觉挺简单的,做着做着就错了20次, 我也是醉了, WA到吐的节奏啊! 思 ...
- hdu.1254.推箱子(bfs + 优先队列)
推箱子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- hdu 1254 推箱子(双重bfs)
题目链接 Problem Description 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能 ...
- hdu 1254 推箱子(嵌套搜索,bfs中有dfs)
推箱子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- hdu 1254 推箱子(搜索)
我写的第一道感觉比较难的搜索 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1254 首先要推箱子的话要满足人能够在箱子旁边,而且人的对面也是可通的. ...
- [HDU 1254] 推箱子
推箱子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- HDU 1254 推箱子游戏(搞了一下午。。。)
中文题目:http://acm.hdu.edu.cn/showproblem.php?pid=1254 一开始常规的人用来做主导,想着想着不对劲,其实是箱子为主导,人只是箱子能否推进的一个判断. 可以 ...
随机推荐
- Ajax记录
Ajax简介 在传统的Web应用中,每次请求服务器都会生成新的页面,用户在提交请求后,总是要等待服务器的相应.如果前一个请求没有得到相应,则后一个请求就不能发送.由于这是一种独占式的请求,因此如果服务 ...
- 工作记录 angular页面操作 MD5加密
今天只是做页面,基于angularjs,有美工做的图打底,确实好用 密码保存,用到了C# MD5加密: https://www.cnblogs.com/healer007/p/5062189.html
- Linux 之 2>&1
我们在Linux下经常会碰到nohup command>/dev/null 2>&1 &这样形式的命令.首先我们把这条命令大概分解下首先就是一个nohup表示当前用户和系统 ...
- (二)Redis for 阿里云公网连接
目录 (一)Redis for Windows正确打开方式 (二)Redis for 阿里云公网连接 (三)Redis for StackExchange.Redis 阿里云目前仅支持内网连接Redi ...
- 【译】x86程序员手册32-9.4 中断描述符表
9.4 Interrupt Descriptor Table 中断描述符表 The interrupt descriptor table (IDT) associates each interrupt ...
- A Convolution Tree with Deconvolution Branches: Exploiting Geometric Relationships for Single Shot Keypoint Detection
作者:嫩芽33出处:http://www.cnblogs.com/nenya33/p/6817781.html 版权:本文版权归作者和博客园共有 转载:欢迎转载,但未经作者同意,必须保留此段声明:必须 ...
- Cygwin, MinGW/MSYS, MinGW-W64/MSYS2
1. Cygwin http://www.cygwin.com/ Cygwin is a large collection of GNU and Open Source tools which pro ...
- swift @objc dynamic
@objc vs @objc dynamic @objc: Objective-C entry points One can explicitly write @objc on any Swift ...
- 输入一个字符串输出ASCII的十六进制值
#include <stdio.h> #include <string.h> #define LEN 1024 void main() { char s[LEN] = &quo ...
- CAD使用GetAllAppName读所有名称(com接口)
主要用到函数说明: MxDrawEntity::GetAllAppName 得到所有扩展数据名称,详细说明如下: 参数 说明 [out, retval] IMxDrawResbuf** ppRet 返 ...