推箱子

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6021    Accepted Submission(s): 1718

Problem Description
推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不能拉箱子,因此如果箱子被推到一个角上(如图2)那么箱子就不能再被移动了,如果箱子被推到一面墙上,那么箱子只能沿着墙移动.
现在给定房间的结构,箱子的位置,搬运工的位置和箱子要被推去的位置,请你计算出搬运工至少要推动箱子多少格.
 
Input
输入数据的第一行是一个整数T(1<=T<=20),代表测试数据的数量.然后是T组测试数据,每组测试数据的第一行是两个正整数M,N(2<=M,N<=7),代表房间的大小,然后是一个M行N列的矩阵,代表房间的布局,其中0代表空的地板,1代表墙,2代表箱子的起始位置,3代表箱子要被推去的位置,4代表搬运工的起始位置.
 
Output
对于每组测试数据,输出搬运工最少需要推动箱子多少格才能帮箱子推到指定位置,如果不能推到指定位置则输出-1.
 
Sample Input
1
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
 
Sample Output
4

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<queue>
  5. int T ;
  6. int n , m ;
  7. const int M = ;
  8. int map[M][M] ;
  9. bool vis[M][M][M][M] ;
  10. int move[][] = {{,} , {- , } , {,} , { , -} } ;
  11. struct node
  12. {
  13. int x , y ;
  14. int a , b ;
  15. int time ;
  16. bool operator < (const node &rhs ) const
  17. {
  18. return time > rhs.time ;
  19. }
  20. };
  21.  
  22. int bfs (int sx , int sy , int mx , int my , int ex , int ey)
  23. {
  24. //printf ("Last---> (%d,%d)\n" , ex , ey ) ;
  25. node ans , tmp ;
  26. std::priority_queue<node> q ;
  27. memset (vis , , sizeof(vis)) ;
  28. while ( !q.empty ()) q.pop () ;
  29. q.push ( (node) {sx , sy , mx , my , }) ;
  30. vis[sx][sy][mx][my] = ;
  31. if (mx == ex && my == ey) return ;
  32. while ( !q.empty ()) {
  33. ans = q.top () ; q.pop () ;
  34. // printf ("S----(%d,%d) tui (%d,%d)\n" , ans.x , ans.y , ans.a , ans.b ) ;
  35. for (int i = ; i < ; i ++) {
  36. tmp = ans ;
  37. tmp.x += move[i][] ; tmp.y += move[i][] ;
  38. if (tmp.x < || tmp.y < || tmp.x == n || tmp.y == m) continue ;
  39. if (map[tmp.x][tmp.y] == ) continue ;
  40. if (tmp.x == tmp.a && tmp.y == tmp.b ) {
  41. int x = tmp.x + move[i][] , y = tmp.y + move[i][] ;
  42. if (x < || y < || x == n || y == m) continue ;
  43. if (map[x][y] == ) continue ;
  44. tmp.a = x , tmp.b = y ;
  45. tmp.time ++ ;
  46. }
  47. if (vis[tmp.x][tmp.y][tmp.a][tmp.b]) continue ;
  48. vis[tmp.x][tmp.y][tmp.a][tmp.b] = ;
  49. q.push (tmp ) ;
  50. // printf ("(%d,%d) tui (%d,%d)\n" , tmp.x , tmp.y , tmp.a , tmp.b ) ;
  51. if (tmp.a == ex && tmp.b == ey) return tmp.time ;
  52. }
  53. }
  54. return - ;
  55. }
  56.  
  57. int main ()
  58. {
  59. //freopen ("a.txt" , "r" , stdin ) ;
  60. scanf ("%d" , &T ) ;
  61. while (T --) {
  62. scanf ("%d%d" , &n , &m ) ;
  63. int k ;
  64. int sx , sy , ex , ey , mx , my ;
  65. for (int i = ; i < n ; i ++) for (int j = ; j < m ; j ++) scanf ("%d" , &map[i][j]) ;
  66. for (int i = ; i < n ; i ++) {
  67. for (int j = ; j < m ; j ++) {
  68. if (map[i][j] == ) sx = i , sy = j ;
  69. else if (map[i][j] == ) mx = i , my = j ;
  70. else if (map[i][j] == ) ex = i , ey = j ;
  71. }
  72. }
  73. if ( (k = bfs (sx , sy , mx , my , ex , ey )) == -) puts ("-1") ;
  74. else printf ("%d\n" , k ) ;
  75. }
  76. return ;
  77. }
  78. [ Copy to Clipboard ] [ Save to File]

hdu.1254.推箱子(bfs + 优先队列)的更多相关文章

  1. HDU 1254 推箱子 BFS

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1254 题目分析: 做这道题,感觉挺简单的,做着做着就错了20次, 我也是醉了, WA到吐的节奏啊! 思 ...

  2. hdu - 1254 推箱子 (bfs+bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1254 题目意思很简单,只要思路对就好. 首先考虑搬运工能否到达推箱子的那个点,这个可以根据箱子前进方向得出搬运工 ...

  3. HDU 1254 推箱子(BFS加优先队列)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1254 推箱子 Time Limit: 2000/1000 MS (Java/Others)    Me ...

  4. HDU 1254 推箱子(BFS)

    Problem Description 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不 ...

  5. hdu 1254 推箱子(双重bfs)

    题目链接 Problem Description 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能 ...

  6. hdu 1254 推箱子(嵌套搜索,bfs中有dfs)

    推箱子 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  7. hdu 1254 推箱子(搜索)

    我写的第一道感觉比较难的搜索 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1254 首先要推箱子的话要满足人能够在箱子旁边,而且人的对面也是可通的. ...

  8. [HDU 1254] 推箱子

    推箱子 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  9. HDU 1254 推箱子游戏(搞了一下午。。。)

    中文题目:http://acm.hdu.edu.cn/showproblem.php?pid=1254 一开始常规的人用来做主导,想着想着不对劲,其实是箱子为主导,人只是箱子能否推进的一个判断. 可以 ...

随机推荐

  1. BZOJ1691: [Usaco2007 Dec]挑剔的美食家

    传送门: 一句话题解:贪心+treap 好几天前刚学的treap,然后真到了考treap又写不出来,这么辣鸡还搞什么OI 先按$A_i$递减排序,然后把$C_i$也递减排序,然后用一个指针指向$M$序 ...

  2. 2-SAT 问题

    2-SAT 问题是k-SAT问题在k==2时的特殊情况,因为已经证明k>=3时的k-sat问题属于npc问题.所以在这里仅研究2-SAT的特殊情况.   何为2-sat问题? 简单地说就是有N个 ...

  3. 回调函数通俗解析(之前看了很久都不理解,今天终于ok啦)

    自学jquery的时候,看到一英文词(Callback),顿时背部隐隐冒冷汗.迅速google之,发现原来中文翻译成回调.也就是回调函数了.不懂啊,于是在google回调函数,发现网上的中文解释实在是 ...

  4. Windows系统自带工具的 cmd 命令

    目标 与计算机高手无关,只是为了减少鼠标点击的次数,提高效率. 适用范围 Windows XP,Windows 7,Window 8 (在Windows 7 下验证通过.) 使用方法 在 “运行“ 对 ...

  5. wpf 窗体内容旋转效果 网摘

    <Window x:Class="simplewpf.chuangtixuanzzhuan"        xmlns="http://schemas.micros ...

  6. js网页如何获取手机屏幕宽度

    function a(){"屏幕宽高为:"+screen.width+"*"+screen.height:}其它:网页可见区域宽:document.body.c ...

  7. js字符串RTrim方法(right trim)

    String.prototype.RTrim = function (c) { if (!c) { c = ' '; } var reg = new RegExp('([' + c + ']*$)', ...

  8. Tengine 常用模块使用介绍

    Tengine 和 Nginx Tengine简介 从2011年12月开始:Tengine是由淘宝网发起的Web服务器项目.它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能 和特性. ...

  9. centos 7.0VI命令 和固定内网IP 查看外网IP

    VI 命令的使用 直接例子 省的忘记 编辑网卡配置文件 vi /etc/sysconfig/network-scripts/ifcfg-enp2s0 不知道那个网卡 ip addr命令 PS:cent ...

  10. C# MVC EF中匿名类使用

    控制器中代码: var list = context.Says.Join( context.Users, a => a.UserId, b => b.Id, (a, b) => ne ...