最少步数

时间限制:3000 ms  |  内存限制:65535 KB
难度:4
 
描述

这有一个迷宫,有0~8行和0~8列:

1,1,1,1,1,1,1,1,1
 1,0,0,1,0,0,1,0,1
 1,0,0,1,1,0,0,0,1
 1,0,1,0,1,1,0,1,1
 1,0,0,0,0,1,0,0,1
 1,1,0,1,0,1,0,0,1
 1,1,0,1,0,1,0,0,1
 1,1,0,1,0,0,0,0,1
 1,1,1,1,1,1,1,1,1

0表示道路,1表示墙。

现在输入一个道路的坐标作为起点,再如输入一个道路的坐标作为终点,问最少走几步才能从起点到达终点?

(注:一步是指从一坐标点走到其上下左右相邻坐标点,如:从(3,1)到(4,1)。)

 
输入
第一行输入一个整数n(0<n<=100),表示有n组测试数据;
随后n行,每行有四个整数a,b,c,d(0<=a,b,c,d<=8)分别表示起点的行、列,终点的行、列。
输出
输出最少走几步。
样例输入
  1. 2
  2. 3 1 5 7
  3. 3 1 6 7
样例输出
  1. 12
  2. 11
    TLE code
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <cstring>
  5. using namespace std;
  6. int a,b,c,t;
  7. int map[][][];
  8. int Min=;
  9. int dirx[]={,,,-},diry[]={,-,,},dirz[]={,-};
  10. int dfs(int z,int x,int y,int step)
  11. {
  12. int i,j;
  13. if(step>t||z<||z>=a||x<||x>=b||y<||y>=c)
  14. return ;
  15. if(map[z][x][y])
  16. return ;
  17. if(x==b-&&y==c-&&z==a-)
  18. {
  19. Min=min(Min,step);
  20. return ;
  21. }
  22. else
  23. {
  24. step++;
  25. map[z][x][y]=;
  26. for(i=;i<;i++)
  27. {
  28. int xx=x+dirx[i],yy=y+diry[i];
  29. dfs(z,xx,yy,step);
  30. }
  31. for(i=;i<;i++)
  32. {
  33. int zz=z+dirz[i];
  34. dfs(zz,x,y,step);
  35. }
  36. map[z][x][y]=;
  37. }
  38. return ;
  39. }
  40. int main()
  41. {
  42. int k,i,j,p;
  43. //freopen("in.txt","r",stdin);
  44. cin>>k;
  45. while(k--)
  46. {
  47. Min=;
  48. cin>>a>>b>>c>>t;
  49. for(i=;i<a;i++) //层
  50. for(j=;j<b;j++)
  51. for(p=;p<c;p++)
  52. cin>>map[i][j][p];
  53. dfs(,,,);
  54. if(Min>t)
  55. cout<<-<<endl;
  56. else
  57. cout<<Min<<endl;
  58. }
  59. }

AC code

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. using namespace std;
  5. int a,b,c,d,m;
  6. int x[][]={
  7. ,,,,,,,,,
  8. ,,,,,,,,,
  9. ,,,,,,,,,
  10. ,,,,,,,,,
  11. ,,,,,,,,,
  12. ,,,,,,,,,
  13. ,,,,,,,,,
  14. ,,,,,,,,,
  15. ,,,,,,,,,
  16. };
  17. int bfs(int p,int q,int s)
  18. {
  19. if(x[p][q]==)
  20. return ;
  21. if(p==c&&q==d)
  22. {
  23. m=min(s,m);
  24. return ;
  25. }
  26. s++;
  27. x[p][q]=;//不再返回
  28. bfs(p-,q,s);//向四周搜索
  29. bfs(p+,q,s);
  30. bfs(p,q+,s);
  31. bfs(p,q-,s);
  32. x[p][q]=;//不得返回。。。看了半天才明白
  33. return ;
  34. }
  35. int main()
  36. {
  37. int N;
  38. cin>>N;
  39. while(N--)
  40. {
  41. cin>>a>>b>>c>>d;
  42. m=;
  43. bfs(a,b,);
  44. cout<<m<<endl;
  45. }
  46. return ;
  47. }

最少步数(bfs)的更多相关文章

  1. 最少步数(bfs)

    最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...

  2. POJ 3126 Prime Path【从一个素数变为另一个素数的最少步数/BFS】

    Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26475 Accepted: 14555 Descript ...

  3. ny 58 最少步数 (BFS)

    题目:http://acm.nyist.net/JudgeOnline/problem.php?pid=58 就是一道简单的BFS 练习练习搜索,一次AC #include <iostream& ...

  4. 最少步数(dfs + bfs +bfs优化)

    最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...

  5. ACM 最少步数

    最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...

  6. [ACM_搜索] ZOJ 1103 || POJ 2415 Hike on a Graph (带条件移动3盘子到同一位置的最少步数 广搜)

    Description "Hike on a Graph" is a game that is played on a board on which an undirected g ...

  7. NYOJ 58 最少步数

    最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...

  8. nyoj 1022 最少步数【优先队列+广搜】

    最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...

  9. nyist 58 最小步数 BFS

    最少步数 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 1,0 ...

随机推荐

  1. label的for属性与inputde的id元素绑定

    <form> <label for="male">Male</label> <input type="radio" n ...

  2. 异常处理与调试2 - 零基础入门学习Delphi51

    异常处理与调试2 让编程改变世界 Change the world by program Delphi 异常类 利用异常机制,就是允许写代码时可以假设:如果用户可以得到子程序调用或计算的结果,这些结果 ...

  3. Linux上使用Azure CLI来管理Azure

    在Windows上我们有强大的Powershell提供各种命令来管理Azure的服务,在Linux上微软提供了基于Node.JS的跨平台的Azure Command Line来帮助Linux用户来管理 ...

  4. USB系列之七:ASPI介绍及命令测试

    在以前的一篇博文<关于构建DOS下编程平台的总结>中曾经介绍了一种在DOS下驱动U盘的方法,我们大致回顾一下.在config.sys中加入两个驱动程序,就可以驱动U盘:device = a ...

  5. I2C串行总线标准驱动程序(C51)-万能程序

    #include "reg51.h" #include "intrins.h" unsigned char SystemError; sbit SCL= P1^ ...

  6. windbg命令详解

      DLL 该扩展仅在内核模式下使用,即使它是在Ext.dll中的. Windows NT 4.0 Ext.dll Windows 2000 Ext.dll Windows XP和之后 Ext.dll ...

  7. EMMA: 免费java代码测试覆盖工具

    From:http://emma.sourceforge.net/ EMMA: a free Java code coverage tool   Code coverage for free: a b ...

  8. ZOJ 1008 Gnome Tetravex(DFS)

    Gnome Tetravex Time Limit: 10 Seconds      Memory Limit: 32768 KB Hart is engaged in playing an inte ...

  9. Linux如何实现开机启动程序详解

    我们假设大家已经熟悉其它操作系统的引导过程,了解硬件的自检引导步骤,就只从Linux操作系统的引导加载程序(对个人电脑而言通常是LILO)开始,介绍Linux开机引导的步骤. 加载内核LILO 启动之 ...

  10. 分享几种Linux软件的安装方法

    Linux软件安装由于不同的Linux分支,安装方法也互不相同,介绍几种常见的安装方法. 1. 源码安装,     对于本身具有开源血统的Linux系统来说,几乎所有的开源软件都支持在Linux平台运 ...