博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1254 推箱子
阅读量:4710 次
发布时间:2019-06-10

本文共 1972 字,大约阅读时间需要 6 分钟。

一个非常有意思的 BFS+DFS。附 数据。

本来今天的任务是多重背包,结果为了帮别人找WA点,自己也坑在这道题上了。

最后想了一组自己都没过的数据…发现想法都不正确…果断换思路了。

正确思路是以箱子为起点做BFS找最短。每次移动的时候DFS推断人能不能移动到箱子的后面。

開始就我写一个BFS,什么数据都过了。这组过不了

17 40 0 0 00 0 1 00 2 0 31 4 1 01 0 1 01 0 1 01 0 0 0

实际上答案是2.

我写的是总步数最短时,箱子的最短步数。给WA了。

然后我就换思路了,注意状态要用四维,由于可能存在要先把箱子推过去,然后再推回来的情况。

3 6
1 1 1 1 1 1
0 0 0 1 0 0
0 0 2 4 0 3

比方这样。

实际上是5.

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x7fffffff#define eps 1e-6using namespace std;int g[8][8];int xx[]={1,-1,0,0};int yy[]={0,0,-1,1};int n,m;struct node{ int x,y,lv;};node box,man,over;bool vis[8][8];bool ok;void dfs(int x1,int y1,int x2,int y2){ if(ok)return; if(x1==x2&&y1==y2) { ok=1; return; } int x,y; for(int k=0;k<4;k++) { x=x1+xx[k]; y=y1+yy[k]; if(x>=n||x<0||y>=m||y<0||g[x][y]==1||vis[x][y]) continue; vis[x][y]=1; dfs(x,y,x2,y2); }}int bfs(){ bool way[8][8][8][8]; queue
q; queue
human; memset(way,0,sizeof(way)); q.push(box); human.push(man); way[box.x][box.y][man.x][man.y]=1; node tmp,now,ma; while(!q.empty()) { tmp=q.front();q.pop(); ma=human.front();human.pop(); if(tmp.x==over.x&&tmp.y==over.y)return tmp.lv; for(int k=0;k<4;k++) { int x=tmp.x+xx[k]; int y=tmp.y+yy[k]; if(x>=n||x<0||y>=m||y<0||g[x][y]==1||way[x][y][tmp.x][tmp.y]) continue; int rex=tmp.x-xx[k]; int rey=tmp.y-yy[k]; if(rex>=n||rex<0||rey>=m||rey<0||g[rex][rey]==1) continue; memset(vis,0,sizeof(vis)); ok=0; vis[tmp.x][tmp.y]=1; //printf("box %d %d -> %d %d\n",tmp.x,tmp.y,x,y); //printf("human %d %d -> %d %d\n",ma.x,ma.y,rex,rey); //system("pause"); dfs(ma.x,ma.y,rex,rey); if(!ok) continue; now.x=x,now.y=y,now.lv=tmp.lv+1; way[x][y][tmp.x][tmp.y]=1; q.push(now); human.push(tmp); } } return -1;}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=0;i

转载于:https://www.cnblogs.com/hrhguanli/p/3938846.html

你可能感兴趣的文章
实验3 SQL注入原理-万能密码注入
查看>>
redis cluster
查看>>
feign传输String json串 自动转义 \ 解决方法
查看>>
本站已稳定运行了XX天,网页时间显示功能实现方法
查看>>
实习的开始阶段
查看>>
搭建第一个node服务器
查看>>
团队冲刺个人总结8
查看>>
Asp.Net Mvc Area二级域名
查看>>
android:intent flags
查看>>
Vue疑难杂症
查看>>
spring boot 错误处理之深度历险
查看>>
MySQL对于有大量重复数据表的处理方法
查看>>
Android应用开发学习笔记之多线程与Handler消息处理机制
查看>>
ubuntu 设置环境变量
查看>>
jquery之别踩白块游戏的实现
查看>>
索引的分类--B-Tree索引和Hash索引
查看>>
Python学习笔记——参数axis=0,1,2...
查看>>
kivy学习三:打包成window可执行文件
查看>>
兄弟连PHP培训教你提升效率的20个要点
查看>>
【快报】基于K2 BPM的新一代协同办公门户实践交流会
查看>>