zoukankan      html  css  js  c++  java
  • BFS和DFS记录路径

     DFS记录路径的意义好像不大,因为不一定是最短的,但是实现起来却很简单。

     1 #include<math.h>
     2 #include<stdio.h>
     3 #include<queue>
     4 #include<string.h>
     5 #include<iostream>
     6 #include<algorithm>
     7 using namespace std;
     8 #define N 1234
     9 
    10 
    11 int n,m,step;
    12 int dx[]={0,0,1,-1};
    13 int dy[]={1,-1,0,0};
    14 char mat[N][N];
    15 int  vis[N][N];
    16 int    a[2][N];
    17 
    18 void dfs(int x,int y,int t)
    19 {
    20     if(step!=-1)return;
    21     if(x<1||x>n||y<1||y>m)return;
    22     if(mat[x][y]=='#'||vis[x][y])return;
    23     if(mat[x][y]=='r')
    24     {
    25         step=t;
    26         a[0][t]=x;
    27         a[1][t]=y;
    28         return ;
    29 
    30     }
    31     vis[x][y]=1;
    32 //    printf("(%d,%d)
    ",x,y);
    33     a[0][t]=x;
    34     a[1][t]=y;
    35     for(int i=0;i<4;i++)
    36         dfs(x+dx[i],y+dy[i],t+1);
    37 }
    38 int main()
    39 {
    40     while(~scanf("%d%d",&n,&m))
    41     {
    42         int stx,sty;
    43         step=-1;
    44         memset(vis,0,sizeof(vis));
    45         for(int i=1;i<=n;i++)
    46             for(int j=1;j<=m;j++)
    47             {
    48                 scanf(" %c",&mat[i][j]);
    49                 if(mat[i][j]=='a')
    50                     stx=i,sty=j;
    51             }
    52         dfs(stx,sty,0);
    53         if(step==-1)puts("Poor ANGEL has to stay in the prison all his life.");
    54         else
    55         {
    56             cout<<step<<endl;
    57             puts("Path");
    58             for(int i=0;i<=step;i++)
    59                 printf("%d,%d
    ",a[0][i],a[1][i]);
    60         }
    61 
    62     }
    63     return 0;
    64 }
    65 
    66 
    67 /*
    68 
    69 7 8
    70 #.#####.
    71 #.a#..r.
    72 #..#....
    73 ..#..#.#
    74 #...##..
    75 .#......
    76 ........
    77 */

    BFS记录路径:我的方法是每一个点都保存一下它从哪个方向走过来的(也就是那个i),这样由最后一个点,就可以倒推出倒数第二个点,倒数第二个点再可以推出倒数第三个点,最后推到起点,再反过来,就是路径了。

      1 #include<math.h>
      2 #include<stdio.h>
      3 #include<queue>
      4 #include<string.h>
      5 #include<iostream>
      6 #include<algorithm>
      7 using namespace std;
      8 #define N 1234
      9 struct point
     10 {
     11     int x,y,t,dir;
     12 }st;
     13 
     14 int n,m,step;
     15 int dx[]={0,0,1,-1};
     16 int dy[]={1,-1,0,0};
     17 char mat[N][N];
     18 int  vis[N][N];
     19 int  xx[N];
     20 int  yy[N];
     21 int  d[N][N];
     22 
     23 int bfs()
     24 {
     25     queue<point>q;
     26     q.push(st);vis[st.x][st.y]=1;
     27     while(!q.empty())
     28     {
     29         point cur=q.front();
     30         q.pop();
     31         for(int i=0;i<4;i++)
     32         {
     33             point next=cur;
     34             next.x+=dx[i];next.y+=dy[i];
     35 
     36             if(next.x<1||next.x>n||next.y<1||next.y>m)continue;
     37             if(mat[next.x][next.y]=='#'||vis[next.x][next.y]==1)continue;
     38 
     39             d[next.x][next.y]=i;
     40 
     41             if(mat[next.x][next.y]=='.')next.t=next.t+1;
     42             if(mat[next.x][next.y]=='r')
     43             {
     44                 xx[0]=next.x;
     45                 yy[0]=next.y;
     46                 step=next.t+1;
     47                 return step;
     48             }
     49             q.push(next);vis[next.x][next.y]=1;
     50         }
     51     }
     52     return -1;
     53 }
     54 int main()
     55 {
     56     while(~scanf("%d%d",&n,&m))
     57     {
     58         step=0;
     59         memset(vis,0,sizeof(vis));
     60         for(int i=1;i<=n;i++)
     61             for(int j=1;j<=m;j++)
     62             {
     63                 scanf(" %c",&mat[i][j]);
     64                 if(mat[i][j]=='a')
     65                     st.x=i,st.y=j,st.t=0;
     66             }
     67         int ans=bfs();
     68         if(ans==-1)puts("Poor ANGEL has to stay in the prison all his life.");
     69         else
     70         {
     71             cout<<ans<<endl;
     72 
     73 
     74             for(int i=1;i<=step;i++)
     75             {
     76                 xx[i]=xx[i-1] - dx[ d[ xx[i-1] ][ yy[i-1] ] ];
     77                 yy[i]=yy[i-1] - dy[ d[ xx[i-1] ][ yy[i-1] ] ];
     78             }
     79             puts("Path");
     80             for(int i=step;i>=0;i--)
     81             {
     82                 printf("%d,%d
    ",xx[i],yy[i]);
     83             }
     84         }
     85 
     86     }
     87     return 0;
     88 }
     89 
     90 
     91 /*
     92 
     93 7 8
     94 #.#####.
     95 #.a#..r.
     96 #..#....
     97 ..#..#.#
     98 #...##..
     99 .#......
    100 ........
    101 
    104 */
  • 相关阅读:
    Java-Class-C:com.alibaba.fastjosn.JSON.java
    兆芯笔试题(2015)找反复数最多的字母的数量以及位置
    获得文件属性的函数调用实例
    错了:用流量能够放肆,有wifi则要节制
    为什么会找不到D层文件?
    Cocos2d-x3.0游戏实例之《别救我》第六篇——从代码中获取UI控件
    Add Binary
    【iOS开发之Objective-C】书签管理器项目
    UVa 11732 strcmp() Anyone?
    【打CF,学算法——二星级】Codeforces Round #313 (Div. 2) B. Gerald is into Art(水题)
  • 原文地址:https://www.cnblogs.com/wmxl/p/4695222.html
Copyright © 2011-2022 走看看