注意求最短路的时候用Bfs。

  1. #include<iostream>
  2. #include<stdio.h>
  3. using namespace std;
  4. int w,h,ex,ey,sx,sy;
  5. int map[100][100],can[100][100];
  6. struct vid{
  7.  
  8. int x,y,step;
  9. }queue[5000];
  10. int zan[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
  11. int dirl[4][2]={0,-1,-1,0,0,1,1,0},dirr[4][2]={0,1,1,0,0,-1,-1,0};
  12. int dfsr(int dstep,int x,int y,int di )
  13. {
  14. int i,temp1,temp2;
  15.  
  16. for(i=0;i<4;i++)
  17. {
  18. temp1=x+dirr[i][0];
  19. temp2=y+dirr[i][1];
  20.  
  21. if((temp1>=0)&&(temp1<h)&&(temp2>=0)&&(temp2<w))
  22. {
  23. if(dfsr(dstep+1,x+dirr[i][0],y+dirr[i][1],i))
  24. return dstep;
  25. else
  26. return 0;
  27. }
  28.  
  29. }
  30. return 0;
  31.  
  32. }
  33. int dfsl(int dstep,int x,int y,int di )
  34. {
  35.  
  36. int i,temp1,temp2;
  37.  
  38. for(i=0;i<4;i++)
  39. {
  40. temp1=x+dirl[i][0];
  41. temp2=y+dirl[i][1];
  42.  
  43. if((temp1>=0)&&(temp1<h)&&(temp2>=0)&&(temp2<w))
  44. {
  45. if(dfsl(dstep+1,temp1,temp2,i))
  46. return dstep;
  47. else
  48. return 0;
  49. }
  50.  
  51. }
  52.  
  53. return 0;
  54. }
  55. int bfs()
  56. {
  57.  
  58. if (sx == ex && sy == ey)
  59. {
  60.  
  61. return 1;
  62. }
  63. int t,ww,x,y,t1,t2;
  64. t=ww=1;
  65. queue[t].x=sx;
  66. queue[t].y=sy;
  67. queue[t].step=0;
  68. can[sx][sy]=1;
  69. while(t<=ww&&!can[ex][ey])
  70. {
  71.  
  72. x=queue[t].x;
  73. y=queue[t].y;
  74. for(int i=0;i<4;i++)
  75. if((!map[x+zan[i][0]][y+zan[i][1]])&&(!can[x+zan[i][0]][y+zan[i][1]]))
  76. {
  77.  
  78. t1=x+zan[i][0];
  79. t2=y+zan[i][1];
  80. if(t1>=0&&(t1<h)&&(t2>=0)&&(t2<w)&&(!map[t1][t2])&&(!can[t1][t2]))
  81. {
  82.  
  83. queue[++ww].x=t1;
  84. queue[ww].y=t2;
  85. queue[ww].step=queue[t].step+1;
  86. can[t1][t2]=1;
  87. }
  88. }
  89. t++;
  90. }
  91. return queue[ww].step+1;
  92. }
  93. int main ()
  94. {
  95. int t;
  96. char c;
  97. scanf("%d",&t);
  98. getchar();
  99. while(t--)
  100. {
  101.  
  102. scanf("%d%d",&w,&h);
  103. getchar();
  104. for(int i=0;i<h;i++)
  105. {
  106. for(int j=0;j<w;j++)
  107. {
  108. can[i][j]=0;
  109. c=getchar();
  110. if(c=='#')
  111. map[i][j]=1;
  112. else if(c=='.')
  113. map[i][j]=0;
  114. else if(c=='S')
  115. {
  116. map[i][j]=0;
  117. sx=i,sy=j;
  118. }
  119. else
  120. if(c=='E')
  121. {
  122. map[i][j]=0;
  123. ex=i;ey=j;
  124. }
  125. }
  126. getchar();
  127. }
  128. // init();
  129. // dfsr();
  130. // printf("%d ",bfs());
  131. printf("%d %d %d\n",dfsl(0,sx,sy,0)+1,dfsr(0,sx,sy,0)+1,bfs());
  132. }
  133.  
  134. return 0;
  135. }

POJ 3083 Bfs+Dfs的更多相关文章

  1. POJ 3083 BFS+DFS 40行

    题意:给你一个迷宫. 先输出当左转优先的时候走的路程长度,再输出当右转优先时走的路程长度,最后输出从起点到终点的最短路程长度. 嗯嗯 奴哥活跃气氛的题.随便写了写.. 此题 知道了思路以后就是水题了. ...

  2. Q - 迷宫问题 POJ - 3984(BFS / DFS + 记录路径)

    Q - 迷宫问题 POJ - 3984 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, ...

  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. poj 3083 dfs,bfs

    传送门    Children of the Candy Corn Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  5. POJ 2227 The Wedding Juicer (优先级队列+bfs+dfs)

    思路描述来自:http://hi.baidu.com/perfectcai_/item/701f2efa460cedcb0dd1c820也可以参考黑书P89的积水. 题意:Farmer John有一个 ...

  6. 搜索入门_简单搜索bfs dfs大杂烩

    dfs题大杂烩 棋盘问题  POJ - 1321 和经典的八皇后问题一样.  给你一个棋盘,只有#区域可以放棋子,同时同一行和同一列只能有一个棋子. 问你放k个棋子有多少种方案. 很明显,这是搜索题. ...

  7. 邻结矩阵的建立和 BFS,DFS;;

    邻结矩阵比较简单,, 它的BFS,DFS, 两种遍历也比较简单,一个用队列, 一个用数组即可!!!但是邻接矩阵极其浪费空间,尤其是当它是一个稀疏矩阵的时候!!!-------------------- ...

  8. POJ.3172 Scales (DFS)

    POJ.3172 Scales (DFS) 题意分析 一开始没看数据范围,上来直接01背包写的.RE后看数据范围吓死了.然后写了个2^1000的DFS,妥妥的T. 后来想到了预处理前缀和的方法.细节以 ...

  9. Collect More Jewels(hdu1044)(BFS+DFS)

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

随机推荐

  1. 20145127《java程序设计》第九周学习总结

    一.教材学习内容总结 第十六章 整合数据库 16.1 JDBC入门 JDBC(Java DataBase Connectivity) 驱动的四种类型 JDBC-ODBC Bridge Driver N ...

  2. 20145317彭垚 MSF基础应用

    20145317彭垚 MSF基础应用 基础问题回答 用自己的话解释什么是exploit,payload,encode? exploit就相当于是载具,将真正要负责攻击的代码传送到靶机中,我觉得老师上课 ...

  3. Python3基础 ** 幂运算 // 整除运算

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  4. thymeleaf和easyui配合可能出现的错误

    thymeleaf和easyui 在easyui的内页,不再使用th:href引入静态资源文件. 在easyui页面中,script执行easyui自己的方法要加入: <script th:in ...

  5. Nlog、elasticsearch、Kibana以及logstash在项目中的应用(一)

    前言 最近在做文档管理中,需要记录每个管理员以及用户在使用过程中的所有操作记录,本来是通过EF直接将操作数据记录在数据库中,在查询的时候直接从数据库中读取,但是这样太蠢了,于是在网上找到了logsta ...

  6. python 操作浏览器打开指定网页

    #! /usr/bin/env python # encoding=utf8 import webbrowser import time webbrowser.open("http://ww ...

  7. 用java代码将数组元素顺序颠倒

    package test; public class Recover { public int[] reverse(int[] a) { int[] b = new int[a.length]; in ...

  8. Linux忘记root登录密码解决方法

    有时候由于长时间米有登录linux系统,等需要用的时候突然忘记root密码,怎么办?下面简单介绍解决方法. redhat 和 centos 6.5 可以,7.0以上未测 在系统重启后,不停地按”e”键 ...

  9. Linux - 命令重定向

    命令重定向, 就是将目前得到的数据转移到指定的地方.分为以下几种: >>>1>2>1>>2>>< 1. > 与 >>先看一 ...

  10. Android中如何实现EditText的自动换行

    要实现EditText的自动换行需要实现如下设置: <EditText android:id="@+id/function_lifingcost_edit_txtRemark" ...