HDU_1254——推箱子,两次BFS
这题做的一把鼻涕一把泪,果断考虑不周555
现在给定房间的结构,箱子的位置,搬运工的位置和箱子要被推去的位置,请你计算出搬运工至少要推动箱子多少格.

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
#include <cstdio>
#include <queue>
using namespace std;
const int dir[][] = {,,,-,,,-,};
int m, n, map[][]; struct node
{
int x, y, step;
int man_x, man_y; bool check(void)
{
if(x>= && x<m && y>= && y<n)
{
if(map[x][y] != )
{
return true;
}
}
return false;
}
}start, man, temp, next, u, v; bool BFS_Man(void)
{
start.x = temp.man_x;
start.y = temp.man_y; int mark[][] = {};
mark[start.x][start.y] = ; queue<node>que;
que.push(start); while(!que.empty())
{
u = que.front();
que.pop();
if(u.x == man.x && u.y == man.y)
{
return true;
}
for(int i=;i<;i++)
{
v.x = u.x + dir[i][];
v.y = u.y + dir[i][];
if(v.check() && !mark[v.x][v.y] && (v.x != temp.x || v.y != temp.y)) //越界,撞墙,重复,撞箱子
{
mark[v.x][v.y] = ;
que.push(v);
}
}
}
return false;
} int BFS_Box(void)
{
int mark[][][] = {}; //判重的时候需要一个三维数组,箱子从不同方向过来,人的位置是不一样的,也就意味着状态不一样 queue<node>que;
que.push(start); while(!que.empty())
{
temp = que.front();
que.pop(); if(map[temp.x][temp.y] == ) //找到返回步数
{
return temp.step;
} for(int i=;i<;i++)
{
next.x = temp.x + dir[i][];
next.y = temp.y + dir[i][];
next.step = temp.step + ;
if(next.check() && mark[next.x][next.y][i] == ) //判断越界,撞墙,重复
{
man.x = temp.x - dir[i][];
man.y = temp.y - dir[i][]; //人移动的目标坐标
if(man.check()) //判断目标坐标是否越界,撞墙
{
if(BFS_Man()) //搜索判断人是否可以移动到目标点
{
next.man_x = temp.x;
next.man_y = temp.y; //更新当前人坐标 mark[next.x][next.y][i] = ;
que.push(next);
}
}
}
}
}
return -;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&m,&n);
for(int i=;i<m;i++)
{
for(int j=;j<n;j++)
{
scanf("%d",&map[i][j]);
if(map[i][j] == ) //记录箱子起点
{
start.x = i;
start.y = j;
start.step = ;
}
else if(map[i][j] == ) //记录人起点
{
start.man_x = i;
start.man_y = j;
}
}
}
printf("%d\n",BFS_Box());
}
return ;
}
HDU_1254——推箱子,两次BFS的更多相关文章
- 推箱子 (hdu1254)(bfs双重广搜)
推箱子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission ...
- hdu 1254 推箱子(双重bfs)
题目链接 Problem Description 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能 ...
- 推箱子 HDU1254 (bfs)
较难的bfs 有两种方法做 一种双重bfs: 主bfs是箱子 还要通过dfs判断人是否能到箱子后面 用inmap函数的好处.. 箱子要用三位数组来标记 因为箱子可以回到原来到过的地方 因为推的 ...
- hdu_1254_推箱子(双BFS)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1254 题解:以箱子为主体,第一层BFS,然后用第二层BFS来判断人是否可以到达,这里细节比较多,要注意 ...
- poj 1475 Pushing Boxes 推箱子(双bfs)
题目链接:http://poj.org/problem?id=1475 一组测试数据: 7 3 ### .T. .S. #B# ... ... ... 结果: //解题思路:先判断盒子的四周是不是有空 ...
- 【GDOI2015】 推箱子 状态压缩+bfs
请注意$8$是一个美妙的数字 考虑到$8\times 8=64$,而一个unsigned long long是$64$位的,所以考虑用一个$01$状态存储箱子.考虑到箱子能转动,那么四种情况都存一下就 ...
- hdu 1254(两个BFS) 推箱子
http://acm.hdu.edu.cn/showproblem.php?pid=1254 首先,要判断人是不是可以从4到达箱子的位置2,而且不止判断一次,因为推动箱子一步后,人的位置也会改变,所以 ...
- 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到吐的节奏啊! 思 ...
随机推荐
- codevs2622数字序列( 连续子序列最大和O(n)算法)
/* 算法描述:维护一个s[p]表示累加和 并且更新最大值ans 如果s[p]<0 则从p+1重新累加 证明:设某个区间的起点和终点分别为s t 分两种情况 1.t<p:设s2表示1到s的 ...
- [Codeforces 501D] - Misha and Permutations Summation
题意是给你两个长度为$n$的排列,他们分别是$n$的第$a$个和第$b$个全排列.输出$n$的第$\left(a+b \right)\textrm{mod} \, n!$个全排列. 一种很容易的想法是 ...
- Ci 分页类的所有属性总结
//#######################自定义分页 $config['uri_segment'] = 3;//分页方法自动测定你 URI 的哪个部分包含页数 $config['num_lin ...
- IE浏览器中设为首页
<a onclick="this.style.behavior='url(#default#homepage)';this.setHomePage('<%=Configurati ...
- Protocol Buffer Xcode 正确使用思路 成功安装 Xcode7.1
1. 下载protobuf编译工具 序列化是将数据转换为一个特定的类 http://pan.baidu.com/s/1qWrxHxU 下载解压,它不是用来放在你的项目里 2.打开终端 依次输入并等待指 ...
- 在Xcode7中安装Alcatraz(Xcode插件管理, 字体主题等)
第一步:关闭 Xcode.第二步:如果你之前安装过Alcatraz,卸载它.在终端运行命令: rm -rf ~/Library/Application\ Support/Developer/Share ...
- Swift - 09 - Optionals
//: Playground - noun: a place where people can play import UIKit // swift中没有被赋值的变量是不能被使用的 //var str ...
- IDEA14下配置SVN
本来以为是很简单的事情,没想到也浪费我有一会的时间,中间也出现了多多少少的问题. 1.在官网下载SVN,版本要为1.8,切忌不能1.9,否则会出错. 在安装SVN的时候,不能一路下一步,我们要安装命令 ...
- 制作chm格式的帮助文档
学习java的人都用过jdk帮助文档,借助工具我们也可以自己生成chm格式的帮助文档, 原文:http://www.cnblogs.com/shenliang123/archive/2012/04/2 ...
- gdb调试带参数程序(转:笑笑小白,cnblog http://www.cnblogs.com/rosesmall/archive/2012/04/10/2440514.html)
一般来说GDB主要调试的是C/C++的程序.要调试C/C++的程序,首先在编译时,我们必须要 把调试信息加到可执行文件中.使用编译 器(cc/gcc/g++)的 -g 参数可以做到这一点.如: > ...