hdu.1254.推箱子(bfs + 优先队列)
推箱子
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6021 Accepted Submission(s): 1718
现在给定房间的结构,箱子的位置,搬运工的位置和箱子要被推去的位置,请你计算出搬运工至少要推动箱子多少格.

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<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
int T ;
int n , m ;
const int M = ;
int map[M][M] ;
bool vis[M][M][M][M] ;
int move[][] = {{,} , {- , } , {,} , { , -} } ;
struct node
{
int x , y ;
int a , b ;
int time ;
bool operator < (const node &rhs ) const
{
return time > rhs.time ;
}
}; int bfs (int sx , int sy , int mx , int my , int ex , int ey)
{
//printf ("Last---> (%d,%d)\n" , ex , ey ) ;
node ans , tmp ;
std::priority_queue<node> q ;
memset (vis , , sizeof(vis)) ;
while ( !q.empty ()) q.pop () ;
q.push ( (node) {sx , sy , mx , my , }) ;
vis[sx][sy][mx][my] = ;
if (mx == ex && my == ey) return ;
while ( !q.empty ()) {
ans = q.top () ; q.pop () ;
// printf ("S----(%d,%d) tui (%d,%d)\n" , ans.x , ans.y , ans.a , ans.b ) ;
for (int i = ; i < ; i ++) {
tmp = ans ;
tmp.x += move[i][] ; tmp.y += move[i][] ;
if (tmp.x < || tmp.y < || tmp.x == n || tmp.y == m) continue ;
if (map[tmp.x][tmp.y] == ) continue ;
if (tmp.x == tmp.a && tmp.y == tmp.b ) {
int x = tmp.x + move[i][] , y = tmp.y + move[i][] ;
if (x < || y < || x == n || y == m) continue ;
if (map[x][y] == ) continue ;
tmp.a = x , tmp.b = y ;
tmp.time ++ ;
}
if (vis[tmp.x][tmp.y][tmp.a][tmp.b]) continue ;
vis[tmp.x][tmp.y][tmp.a][tmp.b] = ;
q.push (tmp ) ;
// printf ("(%d,%d) tui (%d,%d)\n" , tmp.x , tmp.y , tmp.a , tmp.b ) ;
if (tmp.a == ex && tmp.b == ey) return tmp.time ;
}
}
return - ;
} int main ()
{
//freopen ("a.txt" , "r" , stdin ) ;
scanf ("%d" , &T ) ;
while (T --) {
scanf ("%d%d" , &n , &m ) ;
int k ;
int sx , sy , ex , ey , mx , my ;
for (int i = ; i < n ; i ++) for (int j = ; j < m ; j ++) scanf ("%d" , &map[i][j]) ;
for (int i = ; i < n ; i ++) {
for (int j = ; j < m ; j ++) {
if (map[i][j] == ) sx = i , sy = j ;
else if (map[i][j] == ) mx = i , my = j ;
else if (map[i][j] == ) ex = i , ey = j ;
}
}
if ( (k = bfs (sx , sy , mx , my , ex , ey )) == -) puts ("-1") ;
else printf ("%d\n" , k ) ;
}
return ;
}
[ Copy to Clipboard ] [ Save to File]
hdu.1254.推箱子(bfs + 优先队列)的更多相关文章
- HDU 1254 推箱子 BFS
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1254 题目分析: 做这道题,感觉挺简单的,做着做着就错了20次, 我也是醉了, WA到吐的节奏啊! 思 ...
- hdu - 1254 推箱子 (bfs+bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1254 题目意思很简单,只要思路对就好. 首先考虑搬运工能否到达推箱子的那个点,这个可以根据箱子前进方向得出搬运工 ...
- 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)
题目链接 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 一开始常规的人用来做主导,想着想着不对劲,其实是箱子为主导,人只是箱子能否推进的一个判断. 可以 ...
随机推荐
- eclipse+SVN文件只显示版本号,不显示时间和作者解决办法
SVN默认是显示提交次数的 改成这样 就可以了...
- AngularJs 键盘事件和鼠标事件
ngKeydown/ngKeypress/ngKeyup 该指令在按键按下/按键按下/按键松开时执行指定表达式. ngKeydown和ngKeypress略有不同,目前的测试是ngKeypress针对 ...
- Markdown的使用简介
以前有摘抄过,然而onenote速度感人,现在又主要用Linux,所以在这里备份一下,好方便用 Linux下推荐remakeble软件,或者直接sublime text,再或者vim,反正我不会ema ...
- django-redis和redis-py
项目之前使用memcache做缓存,现在转到redis,改写几个语句的事情,然后就这种我把django-redis和py-redis搞混了,记录一下. django默认使用memcache做缓存,这里 ...
- js021-Ajax与Comet
js021-Ajax与Comet 本章内容: 使用XMLHttpRequet对象 使用XMLHttpRequet事件 跨域Ajax通信的限制 Ajax技术的核心是XMLHttpRequet对象 21. ...
- c++ operator
这篇博文是以前很久写的,贴在我的早期一个blog中,今天google一下,发现还真有不少人转载,可惜并不注明出处.那时觉得operator比较好玩.C++有时它的确是个耐玩的东东.operator它有 ...
- Oralce配置正确,报监听错误或无法识别描述中的服务
出差客户现场,修改过网络配置,回来后本地虚拟机的Oracle数据库就不能登陆了 报监听错误,在服务器中使用Net Configration Assistant删除以前的,重新配置新的,还是不行,重启系 ...
- C#中使用DateTimePicker控件显示修改日期时间
1.只显示日期 默认就是 2.只显示时间 修改属性 Format 设为Time ShowUpDown设为true 3.同时显示日期时间 Format设为Custom CustomF ...
- Unity Particle System Sorting Order
http://answers.unity3d.com/questions/577288/particle-system-rendering-behind-sprites.html
- sqlserver权限体系(下)
简介 在上一篇文章中,我对主体的概念做了全面的阐述.本篇文章接着讲述主体所作用的安全对象以及所对应的权限. 理解安全对象(Securable) 安全对象,是SQL Server 数据库引擎授权系统控制 ...