HDU1254:推箱子(bfs+dfs)
传送门
题意
给出一副图
0.空地1.墙2.箱子3.目的地4.人所在的位置
问最少几步能将箱子推到目的地
分析
这道题难度略大(菜鸡),首先用vis[bx][by][mx][my]记录当箱子(bx,by)和人(mx,my)是否被访问过,用一个bfs求出最短路,另外dfs判断人是否能够到达推箱子的位置,写起来比较复杂,(弱鸡)
trick
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int t,n,m,mp[10][10],bx,by,mx,my,prex,prey,flag,ans;
int dir[4][2]={0,1,-1,0,0,-1,1,0};
int vis_p[10][10],vis[10][10][10][10];
struct node
{
int bx,by;
int mx,my;
int step;
}tmp,p;
bool check(int x,int y)
{
if(x>=1&&x<=n&&y>=1&&y<=m&&mp[x][y]!=1) return 1;
return 0;
}
void dfs(int prex,int prey,int mx,int my)
{
if(prex==mx&&prey==my) { flag=1;return ; }
if(flag) return ;
for(int i=0;i<4;++i)
{
int x=prex+dir[i][0];
int y=prey+dir[i][1];
if(check(x,y)&&!vis_p[x][y])
{
vis_p[x][y]=1;
dfs(x,y,mx,my);
}
}
}
void bfs2(int bx,int by,int mx,int my)
{
memset(vis,0,sizeof(vis));
ans=-1;
queue<node>pq;
while(!pq.empty()) pq.pop();
tmp={bx,by,mx,my,0};
pq.push(tmp);
while(!pq.empty())
{
tmp=pq.front();pq.pop();
if(mp[tmp.bx][tmp.by]==3) { ans=tmp.step;return ; }
for(int i=0;i<4;++i)
{
p=tmp;
p.bx+=dir[i][0],p.by+=dir[i][1];
prex=tmp.bx-dir[i][0],prey=tmp.by-dir[i][1];
if(!check(p.bx,p.by)) continue;
if(!check(prex,prey)) continue;
if(vis[p.bx][p.by][prex][prey]) continue;
memset(vis_p,0,sizeof(vis_p));
vis_p[tmp.bx][tmp.by]= vis_p[prex][prey]=1;
flag=0;
dfs(prex,prey,tmp. mx,tmp.my);
if(!flag) continue;
vis[p.bx][p.by][prex][prey]=1;
p.mx=prex,p.my=prey;
p.step++;
pq.push(p);
}
}
}
int main()
{
for(scanf("%d",&t);t--;)
{
scanf("%d %d",&n,&m);
for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)
{
scanf("%d",&mp[i][j]);
if(mp[i][j]==4) { mx=i,my=j; }
if(mp[i][j]==2) { bx=i,by=j; }
}
bfs2(bx,by,mx,my);
printf("%d\n",ans);
}
}
/*
6666
5 5
3 0 1 0 0
1 0 1 4 0
0 0 1 0 0
1 0 2 0 0
0 0 0 0 0
5 5
0 3 0 0 0
1 0 1 4 0
0 0 1 0 0
1 0 2 0 0
0 0 0 0 0
5 5
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 1 2 1 1
4 0 3 0 0
*/
HDU1254:推箱子(bfs+dfs)的更多相关文章
- HDU1254 推箱子(BFS) 2016-07-24 14:24 86人阅读 评论(0) 收藏
推箱子 Problem Description 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推 ...
- hdu.1254.推箱子(bfs + 优先队列)
推箱子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- HDU 1254 推箱子 BFS
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1254 题目分析: 做这道题,感觉挺简单的,做着做着就错了20次, 我也是醉了, WA到吐的节奏啊! 思 ...
- 推箱子 BFS
[编程题] 推箱子 大家一定玩过“推箱子”这个经典的游戏.具体规则就是在一个N*M的地图上,有1个玩家.1个箱子.1个目的地以及若干障碍,其余是空地.玩家可以往上下左右4个方向移动,但是不能移动出地图 ...
- hdu - 1254 推箱子 (bfs+bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1254 题目意思很简单,只要思路对就好. 首先考虑搬运工能否到达推箱子的那个点,这个可以根据箱子前进方向得出搬运工 ...
- hdu 1254 推箱子(嵌套搜索,bfs中有dfs)
推箱子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- 推箱子 (hdu1254)(bfs双重广搜)
推箱子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission ...
- HDU1254--推箱子(BFS+DFS)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s) ...
- HDU 1254 推箱子(BFS)
Problem Description 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不 ...
随机推荐
- python学习之-- 进程 和 线程
python 进程/线程详解 进程定义:以一个整体的形式暴露给操作系统管理,它里面包含对各种资源的调用,内存的管理,网络接口的调用等等,对各种资源管理的集合,就可以叫做一个进程. 线程定义:线程是操作 ...
- 无向图生成树计数 基尔霍夫矩阵 SPOJ Highways
基尔霍夫矩阵 https://blog.csdn.net/w4149/article/details/77387045 https://blog.csdn.net/qq_29963431/articl ...
- Python 列表的复制操作
2013-10-18 10:07:03| import copy a = [1,2,3,['a','b']] b = a c = a[:] d = copy.copy(a) e = copy.de ...
- Spring4MVC 请求参数映射和Content-type
目录 前言 不使用注解(不传则为null) 基本数据类型和日期类型 自定义类型POJO @PathVariable注解 @RequestParam 注解 @RequestBody注解 复杂对象Arra ...
- vm 安装CentOS7
1.首先需要到CentOS官网下载CentOS7的iso镜像文件,地址http://mirrors.cn99.com/centos/7/isos/x86_64/ 这里我选择的是迅雷种子文件 2.下载完 ...
- win7 休眠功能的开启与关闭
1. 打开控制面板得下面目录,编辑计划设置
- coco2dx新建项目报错,ld: -pie can only be used when targeting iOS 4.2 or later clang: error: linker command
在新建cocos2d-x以后,执行发现下面错误: ld: -pie can only be used when targeting iOS 4.2 or later clang: error: lin ...
- 为经典版eclipse添加web and JavaEE插件
地址:http://download.eclipse.org/releases/juno. 选择Web,XML,Java EE and OSGI Enterprise Development,之后ne ...
- Angular45
Angular 4 Tutorial for Beginners: Learn Angular 4 from Scratch https://www.youtube.com/watch?v=k5E2A ...
- 云上领跑,快人一步:华为云抢先发布Redis5.0
12月17日,华为云在DCS2.0的基础上,快人一步,抢先推出了新的Redis 5.0产品,这是一个崭新的突破.目前国内在缓存领域的发展普遍停留在Redis4.0阶段,华为云率先发布了Redis5.0 ...