题意:给一个w*h的迷宫,其中矩阵里面 S是起点,E是终点,“#”不可走,“.”可走,而且,S、E都只会在边界并且,不会在角落,例如(0,0),输出的话,每组数据就输出三个整数,第一个整数,指的是,以S的起点为当前所对着的路径为正方向,如果正方向的左边能走的话,就走左边,不能就按正方向走,不行的话就就往回走,如此反复,记录步数,并输出,第二个整数也是如此,只不过搜的方向改成正方向的右边。第三个就是最短路,

分析:前两个用DFS求出,最短路直接BFS解决,,

单就沿着左走看一下:

当前方向       检索顺序

Sx=n-1   ↑ :      ← ↑ → ↓

Sy=0    → :        ↑ → ↓ ←

Sx=0    ↓ :      → ↓ ← ↑

Sy=n-1   ← :        ↓ ← ↑ →

如此,规律很明显,假设数组存放方向为 ← ↑ → ↓, 如果当前方向为 ↑, 就从 ← 开始依次遍历,找到可以走的,如果 ← 可以走,就不用再看 ↑ 了。

这个题的麻烦处理就在于怎么按什么样的顺序dfs

在网上搜题解感觉这种想法特别好,不繁琐,代码还简洁

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<queue>
  5.  
  6. using namespace std;
  7.  
  8. int dx[]={,-,,};
  9. int dy[]={-,,,};
  10.  
  11. int dl[][]={{,-},{-,},{,},{,}};
  12. int dr[][]={{,},{-,},{,-},{,}};
  13.  
  14. int sx,sy,ex,ey,n,m;
  15. char G[][];
  16.  
  17. struct node
  18. {
  19. int x,y,s;
  20. };
  21.  
  22. int dfs(int x,int y,int d,int step,int dir[][])
  23. {
  24. for(int i=;i<;++i)
  25. {
  26. int j=((d-+)%+i)%;
  27. int nx=x+dir[j][];
  28. int ny=y+dir[j][];
  29. if(nx==ex&&ny==ey)
  30. return step+;
  31. if(nx<||ny<||nx>n||ny>m||G[nx][ny]=='#')
  32. continue;
  33. return dfs(nx,ny,j,step+,dir);
  34. }
  35. }
  36.  
  37. int BFS(int sx,int sy)
  38. {
  39. bool vis[][];
  40. memset(vis, false, sizeof(vis));
  41. queue<node>q;
  42. node a;
  43. a.x=sx,a.y=sy,a.s=;
  44. q.push(a);
  45. vis[sx][sy]=true;
  46. while(!q.empty())
  47. {
  48. node p=q.front();
  49. q.pop();
  50. if(p.x==ex&&p.y==ey)
  51. return p.s;
  52. node p1;
  53. for(int i=;i<;++i) {
  54. p1.x=p.x+dx[i];
  55. p1.y=p.y+dy[i];
  56. p1.s=p.s+;
  57. if(p1.x<||p1.x>n||p1.y<||p1.y>m||vis[p1.x][p1.y])
  58. continue;
  59. if(G[p1.x][p1.y]!='#')
  60. {
  61. vis[p1.x][p1.y]=true;
  62. q.push(p1);
  63. }
  64. }
  65. }
  66. return -;
  67. }
  68.  
  69. int main()
  70. {
  71. int T,d1,d2;
  72. scanf("%d",&T);
  73. while(T--)
  74. {
  75. scanf("%d%d",&m,&n);
  76. for(int i=;i<n;++i)
  77. {
  78. scanf("%s",G[i]);
  79. for(int j=;j<m;++j)
  80. {
  81. if(G[i][j]=='S')
  82. {
  83. sx=i;
  84. sy=j;
  85. }
  86. else if(G[i][j]=='E')
  87. {
  88. ex=i;
  89. ey=j;
  90. }
  91. }
  92. }
  93. if(sx==)
  94. {
  95. d1=;
  96. d2=;
  97. }
  98. else if(sx==n-)
  99. {
  100. d1=;
  101. d2=;
  102. }
  103. else if(sy==)
  104. {
  105. d1=;
  106. d2=;
  107. }
  108. else if(sy==m-)
  109. {
  110. d1=;
  111. d2=;
  112. }
  113. printf("%d ",dfs(sx,sy,d1,,dl));
  114. printf("%d ",dfs(sx,sy,d2,,dr));
  115. printf("%d\n",BFS(sx,sy));
  116. }
  117. return ;
  118. }

POJ3083 Children of the Candy Corn(Bfs + Dfs)的更多相关文章

  1. 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 ...

  2. POJ3083 Children of the Candy Corn(搜索)

    题目链接. 题意: 先沿着左边的墙从 S 一直走,求到达 E 的步数. 再沿着右边的墙从 S 一直走,求到达 E 的步数. 最后求最短路. 分析: 最短路好办,关键是沿着墙走不太好想. 但只要弄懂如何 ...

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

    POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...

  4. POJ3083——Children of the Candy Corn(DFS+BFS)

    Children of the Candy Corn DescriptionThe cornfield maze is a popular Halloween treat. Visitors are ...

  5. poj3083 Children of the Candy Corn BFS&&DFS

    Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11215   Acce ...

  6. POJ 3083:Children of the Candy Corn(DFS+BFS)

    Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: ...

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

    做了1天,总是各种错误,很无语 最后还是参考大神的方法 题目:http://poj.org/problem?id=3083 题意:从s到e找分别按照左侧优先和右侧优先的最短路径,和实际的最短路径 DF ...

  8. poj3083 Children of the Candy Corn 深搜+广搜

    这道题有深搜和广搜.深搜还有要求,靠左或靠右.下面以靠左为例,可以把简单分为上北,下南,左西,右东四个方向.向东就是横坐标i不变,纵坐标j加1(i与j其实就是下标).其他方向也可以这样确定.通过上一步 ...

  9. poj 3083 Children of the Candy Corn (广搜,模拟,简单)

    题目 靠墙走用 模拟,我写的是靠左走,因为靠右走相当于 靠左走从终点走到起点. 最短路径 用bfs. #define _CRT_SECURE_NO_WARNINGS #include<stdio ...

随机推荐

  1. Kotlin——最详细的接口使用、介绍

    在Kotlin中,接口(Interface)的使用和Java中的使用方式是有很大的区别.不管是接口中的属性定义,方法等.但是定义方式还是相同的. 目录 一.接口的声明 1.接口的声明 关键字:inte ...

  2. python抽象篇:面向对象

    1.面向对象概述 面向过程编程:根据操作数据的函数或语句块来设计程序的. 函数式编程:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象编程:数据和功能结合起来,用称为对象的东西包 ...

  3. Visual paradigm软件介绍

    Visual paradigm软件介绍 说起Visual Paradigm你可能并不陌生,因为此前有一款功能强大的UML软件叫Visual Paradigm for UML,在这款软件在v11.1的时 ...

  4. Problem P

    题意:FJ养牛,他想知道中间的牛是那一头: 思路:这道题有点水,思路就不写了 #include #include #include #define maxn 10005 using namespace ...

  5. POJ2411 Mondriaan's Dream(状态压缩)

    Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 15295   Accepted: 882 ...

  6. Android 开发笔记___shape

    shape_oval <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android= ...

  7. AngularJS学习篇(三)

    创建自定义的指令 除了 AngularJS 内置的指令外,我们还可以创建自定义指令. 你可以使用 .directive 函数来添加自定义的指令. 要调用自定义指令,HTML 元素上需要添加自定义指令名 ...

  8. HashMap面试题:90%的人回答不上来

    在java面试中集合类似乎已经是绕不开的话题,对于一个中高级java程序员来说如果对集合类的内部原理不了解,基本上面试都会被pass掉.下面从面试官的角度来聊聊一个候选者应该对HashMap了解到什么 ...

  9. linux-cp

    cp 更改时间: 2017-10-26 - 21:00:54 cp:用来复制文件或者目录的命令,当源文件与目标文件名字相同的时候,当cp 没有参数,源文件会覆盖目标文件 参数 -p:保留源文件或者目录 ...

  10. 《Linux命令行与shell脚本编程大全》 第四章

    4.1 监测程序 1. ps  默认只显示运行在当前控制台下的属于当前用户的进程.  可以接很多选项,比如 -A表示所有进程  -e等. 2. ps -l  查看进程更多信息 UID:启动这些进程的用 ...