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 一开始常规的人用来做主导,想着想着不对劲,其实是箱子为主导,人只是箱子能否推进的一个判断. 可以 ...
随机推荐
- BZOJ1691: [Usaco2007 Dec]挑剔的美食家
传送门: 一句话题解:贪心+treap 好几天前刚学的treap,然后真到了考treap又写不出来,这么辣鸡还搞什么OI 先按$A_i$递减排序,然后把$C_i$也递减排序,然后用一个指针指向$M$序 ...
- 2-SAT 问题
2-SAT 问题是k-SAT问题在k==2时的特殊情况,因为已经证明k>=3时的k-sat问题属于npc问题.所以在这里仅研究2-SAT的特殊情况. 何为2-sat问题? 简单地说就是有N个 ...
- 回调函数通俗解析(之前看了很久都不理解,今天终于ok啦)
自学jquery的时候,看到一英文词(Callback),顿时背部隐隐冒冷汗.迅速google之,发现原来中文翻译成回调.也就是回调函数了.不懂啊,于是在google回调函数,发现网上的中文解释实在是 ...
- Windows系统自带工具的 cmd 命令
目标 与计算机高手无关,只是为了减少鼠标点击的次数,提高效率. 适用范围 Windows XP,Windows 7,Window 8 (在Windows 7 下验证通过.) 使用方法 在 “运行“ 对 ...
- wpf 窗体内容旋转效果 网摘
<Window x:Class="simplewpf.chuangtixuanzzhuan" xmlns="http://schemas.micros ...
- js网页如何获取手机屏幕宽度
function a(){"屏幕宽高为:"+screen.width+"*"+screen.height:}其它:网页可见区域宽:document.body.c ...
- js字符串RTrim方法(right trim)
String.prototype.RTrim = function (c) { if (!c) { c = ' '; } var reg = new RegExp('([' + c + ']*$)', ...
- Tengine 常用模块使用介绍
Tengine 和 Nginx Tengine简介 从2011年12月开始:Tengine是由淘宝网发起的Web服务器项目.它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能 和特性. ...
- centos 7.0VI命令 和固定内网IP 查看外网IP
VI 命令的使用 直接例子 省的忘记 编辑网卡配置文件 vi /etc/sysconfig/network-scripts/ifcfg-enp2s0 不知道那个网卡 ip addr命令 PS:cent ...
- C# MVC EF中匿名类使用
控制器中代码: var list = context.Says.Join( context.Users, a => a.UserId, b => b.Id, (a, b) => ne ...