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 一开始常规的人用来做主导,想着想着不对劲,其实是箱子为主导,人只是箱子能否推进的一个判断. 可以 ...
随机推荐
- 第二次作业——C++学习
课程选择: 以往在自学的过程就比较留意一些自学的网站,所以这次"C++自学"感觉找课程还是比较轻松的. 因为之前网页等学习都是在慕课网(视频学习个人感觉有时挺费时间的,特别是有时以 ...
- Mouse.OverrideCursor
介绍: 获取和设置整个应用程序的光标,WPF父元素将覆盖所有子元素的光标. WPF设置控件的光标: WPF 中每个光标通过一个System.Windows.Input.Cursor表示, 获取Curs ...
- ThinkPHP中field 方法与getField 方法的区别。
做项目的时候遇到了一个问题,框架生成的sql与放到navicat中执行.和页面显示出来的结果不太一样. 排查了很久,也没有找到问题. 出现问题的sql如下. $fuck = M(null, null, ...
- MSMQ 学习(1)
在 Windows Server 2008 or Windows Server 2008 R2 上安装消息队列 4.0 在服务器管理器中,单击“功能”. 在“功能摘要”下的右窗格中,单击“添加功能”. ...
- easyUI 树的上下文菜单
一.属性:onContextMenu onContextMenu: function(e,node){ e.preventDefault(); $(this).tree('select',node.t ...
- python --- Python中的callable 函数
python --- Python中的callable 函数 转自: http://archive.cnblogs.com/a/1798319/ Python中的callable 函数 callabl ...
- 使用docker安装lamp
docker search -s 10 lamp #搜索被收藏或使用较多的LAMP镜像,小伙伴们都推荐使用tutum/lamp docker pull tutum/lamp #下载镜像 #使用默认方式 ...
- 有scp命令,传输文件却显示报错无此命令
今天下午在一台服务器上使用scp命令向另外一台服务器传文件的时候,报此错误 bash: scp: command not found ,lost connection,以为是该服务器没有安装此命令,w ...
- Which hashing algorithm is best for uniqueness and speed?
http://programmers.stackexchange.com/questions/49550/which-hashing-algorithm-is-best-for-uniqueness- ...
- Ctrl+Scroll改变所有Editor的缩放比例 (Code::Blocks)
Settings > Editor > Zooming resizes all editors