题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1254

分析:

真正移动的是箱子,但是要移动箱子需要满足几个条件。

1.移动方向上没有障碍。

2.箱子后方没有障碍。

3.人可以到达箱子后方的地方。这里dfs或bfs都可以实现

按条件搜索即可。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <queue>
  7. #include <cstdlib>
  8. #include <stack>
  9. #include <vector>
  10. #include <set>
  11. #include <map>
  12. #define LL long long
  13. #define mod 1000000007
  14. #define inf 0x3f3f3f3f
  15. #define N 100010
  16. using namespace std;
  17. typedef struct node
  18. {
  19. int Bx,By;
  20. int Mx,My;
  21. int step;
  22. bool operator<(const node &a)const
  23. {
  24. return step>a.step;
  25. }
  26. }node;
  27. int dx[]={,,,-};
  28. int dy[]={,-,,};
  29. int hash[][][][];
  30. int vis[][],s[][];
  31. int Bx,By,Mx,My,Nx,Ny;
  32. int found,n,m;
  33. node make_node(int a,int b,int x,int y)
  34. {
  35. node temp;
  36. temp.Bx=a;temp.By=b;
  37. temp.Mx=x;temp.My=y;
  38. temp.step=;
  39. return temp;
  40. }
  41.  
  42. int judge(int x,int y)
  43. {
  44. return x>=&&x<n&&y>=&&y<m&&s[x][y]!=;
  45. }
  46. void dfs(int Nx,int Ny,int Mx,int My)
  47. {
  48. if(Nx==Mx&&Ny==My)
  49. {
  50. found=;return;
  51. }
  52. for(int i=;i<&&!found;i++)
  53. {
  54. int x=Nx+dx[i];
  55. int y=Ny+dy[i];
  56. if(judge(x,y)&&!vis[x][y])
  57. vis[x][y]=,dfs(x,y,Mx,My);
  58. }
  59. }
  60.  
  61. void bfs(int Bx,int By,int Mx,int My)
  62. {
  63. priority_queue<node>que;
  64. node p,q;
  65. p=make_node(Bx,By,Mx,My);
  66. que.push(p);
  67. while(!que.empty())
  68. {
  69. node p=que.top();que.pop();
  70. if(s[p.Bx][p.By]==)
  71. {
  72. printf("%d\n",p.step);return;
  73. }
  74. for(int i=;i<;i++)
  75. {
  76. q=p;
  77. q.Bx=p.Bx+dx[i];//箱子移动的地方
  78. q.By=p.By+dy[i];
  79. Nx=p.Bx-dx[i];//箱子的后方
  80. Ny=p.By-dy[i];
  81. if(judge(q.Bx,q.By)&&judge(Nx,Ny)&&!hash[q.Bx][q.By][Nx][Ny])
  82. {
  83. memset(vis,,sizeof(vis));
  84. vis[p.Bx][p.By]=vis[Nx][Ny]=;//注意这里箱子将成为阻碍物
  85. found=;
  86. dfs(Nx,Ny,p.Mx,p.My);//判断人是否可达箱子后面
  87. if(found)
  88. {
  89. hash[q.Bx][q.By][Nx][Ny]=;
  90. q.Mx=p.Bx;q.My=p.By;q.step++;
  91. que.push(q);
  92. }
  93. }
  94. }
  95. }
  96. printf("-1\n");
  97. return;
  98. }
  99. void init()
  100. {
  101. memset(hash,,sizeof(hash));
  102. memset(s,,sizeof(s));
  103. for(int i=;i<n;i++)
  104. for(int j=;j<m;j++)
  105. {
  106. scanf("%d",&s[i][j]);
  107. if(s[i][j]==)
  108. {
  109. Bx=i;By=j;
  110. }
  111. if(s[i][j]==)
  112. {
  113. Mx=i;My=j;
  114. }
  115. }
  116. }
  117. int main()
  118. {
  119. int T;
  120. scanf("%d",&T);
  121. while(T--)
  122. {
  123. scanf("%d%d",&n,&m);
  124. init();
  125. bfs(Bx,By,Mx,My);
  126. }
  127. }

hdu1254(bfs+dfs)的更多相关文章

  1. Cleaning Robot (bfs+dfs)

    Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...

  2. hdu 4771 求一点遍历全部给定点的最短路(bfs+dfs)

    题目如题.题解如题. 因为目标点最多仅仅有4个,先bfs出俩俩最短路(包含起点).再dfs最短路.)0s1A;(当年弱跪杭州之题,现看如此简单) #include<iostream> #i ...

  3. HDU1254--推箱子(BFS+DFS)

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

  4. 图的基本遍历算法的实现(BFS & DFS)复习

    #include <stdio.h> #define INF 32767 typedef struct MGraph{ ]; ][]; int ver_num, edge_num; }MG ...

  5. HDU 1044 Collect More Jewels(BFS+DFS)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  6. 图的遍历(bfs+dfs)模板

    bfs #include<iostream> #include<queue> #include<cstdio> using namespace std; queue ...

  7. POJ-3083 Children of the Candy Corn (BFS+DFS)

    Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and mus ...

  8. POJ 3126 Prime Path(BFS算法)

    思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...

  9. Addition Chains POJ - 2248 (bfs / dfs / 迭代加深)

    An addition chain for n is an integer sequence <a0, a1,a2,...,am=""> with the follow ...

随机推荐

  1. EasyUI - Tree 树组件

    效果: 数据库设计: 使用的数据: 其中的字段,是跟据要生成的树节点的属性定义的. text:代表要显示的字段名称. state:是否是目录节点. iconCls:节点的图标是什么. url:跳转的链 ...

  2. linux shell脚本:在脚本中实现读取键盘输入,根据输入判断下一步的分支

    echo please input “runbip” to run bip. variableName="null" while [ $variableName != " ...

  3. 8天玩转并行开发——第二天 Task的使用

    原文 8天玩转并行开发——第二天 Task的使用 在我们了解Task之前,如果我们要使用多核的功能可能就会自己来开线程,然而这种线程模型在.net 4.0之后被一种称为基于 “任务的编程模型”所冲击, ...

  4. perl 匿名函数传参

    $subref=sub { my $a=shift; return $a; }; print $subref->("xxyyzz");

  5. 基于visual Studio2013解决C语言竞赛题之1052求根

       题目 解决代码及点评 /* 功能:用简单迭代法解方程 e^x - x - 2 = 0 它有两个根(如图),其迭代公式为: 1) x[n+1]= e^x*n-2 (初值X<0时) ...

  6. Vertica数据库操作

    删除主键(Vertica数据库的主键值并非唯一的): SELECT ANALYZE_CONSTRAINTS('fb_s.c_log'); 找到key名,再: ALTER TABLE fb_s.c_lo ...

  7. [置顶] oracle 数据库表中转换成java代码

    --数据库中字段java代码 select col.TABLE_NAME,replace(initcap(col.TABLE_NAME),'_', '')   , 'private '||decode ...

  8. 轻应用 lapp

    轻应用 LAPP (Light App) 即轻应用是一种无需下载.即搜即用的全功能 App,既有媲美甚至超越native app的用户体验,又具备webapp的可被检索与智能分发的特性,将有效解决优质 ...

  9. Redis:安装、配置、操作和简单代码实例(C语言Client端)

    Redis:安装.配置.操作和简单代码实例(C语言Client端) - hj19870806的专栏 - 博客频道 - CSDN.NET Redis:安装.配置.操作和简单代码实例(C语言Client端 ...

  10. Get RSA public key ASN.1 encode from a certificate in DER format

    RSA public key ASN.1 encode is defined in PKCS#1 as follows: RSAPublicKey :: = SEQUENCE  {     modul ...