DFS——迷宮問題(輸出搜索過程及路徑長度)

題目與上一篇BFS是一樣的,這裏用DFS實現是爲了探究二者遍歷的區別。

DFS代碼:

//
//  main.cpp
//  DFS(迷宮問題自己寫)可輸出搜索過程
//
//  Created by showlo on 2018/4/14.
//  Copyright © 2018年 showlo. All rights reserved.
//

#include <stdio.h>
#include <algorithm>
using namespace std;
#define max_n 102
#define max_m 102
int N,M;
int sx,sy,gx,gy,nx,ny;
char map[max_n][max_m];
int vis[max_n][max_m];
int len,ans,flag;
int dx[4]={1,0,-1,0},dy[4]={0,-1,0,1};

int dfs(int x,int y){
    vis[x][y]=1;
    if (x==gx&&y==gy)
        flag=1;
    else{
        //vis[x][y]=1;
        for (int i=0; i<4; i++) {
            nx=x+dx[i];
            ny=y+dy[i];
            if (nx<0||nx>=N||ny<0||ny>=M||map[nx][ny]=='#'||vis[nx][ny]==1) {
                continue;
            }
            else
            {
                printf("%d %d\n",nx,ny);
                len=len+1;
                vis[nx][ny]=1;
                dfs(nx, ny);
                if(flag==1)
                    break;
                else
                {
                    len--;
                    vis[nx][ny]=0;
                }
            }
        }
    }
    return len;
}
int main() {
    scanf("%d %d",&N,&M);
    memset(map, 0, sizeof(map));
    memset(vis, 0, sizeof(vis));
    for (int i=0; i<N; i++) {
        scanf("%s",map[i]);
    }
    for (int i=0; i<N; i++) {
        for (int j=0; j<M; j++) {
            if (map[i][j]=='S') {
                sx=i;
                sy=j;
            }
            else if (map[i][j]=='G'){
                gx=i;
                gy=j;
            }
        }
    }
    //printf("%d %d\n",gx,gy);
    len=0;
    flag=0;
    //vis[sx][sy]=1;
    ans=dfs(sx,sy);
    printf("%d\n",ans);
    return 0;
}

BFS代碼:

//
//  main.cpp
//  BFS(迷宮問題自己寫)
//
//  Created by showlo on 2018/4/13.
//  Copyright © 2018年 showlo. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <queue>
using namespace std;
typedef pair<int, int> P;
#define max_n 102
#define max_m 102
#define inf 1000000
int N,M;
char map[max_n][max_m];
int direct[max_n][max_m];
int sx,sy,gx,gy;
int ans;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

int bfs(int sx,int sy,int gx,int gy)
{
    int nx,ny;
    queue<P> Q;
    memset(direct, inf, sizeof(direct));
    Q.push(P(sx,sy));
    direct[sx][sy]=0;
    while (Q.size()) {
        P q=Q.front();
        Q.pop();
        if (q.first==gx&&q.second==gy)
            break;
        else{
            for (int i=0; i<=3; i++) {
                nx=q.first+dx[i];
                ny=q.second+dy[i];
                if (nx<0||nx>N||ny<0||ny>M||map[nx][ny]=='#'||direct[nx][ny]<inf)
                    continue;
                else{
                    direct[nx][ny]=direct[q.first][q.second]+1;
                    Q.push(P(nx,ny));
                }
            }
        }
    }
    return direct[gx][gy];
}
int main() {
    scanf("%d %d",&N,&M);
    for (int i=0; i<N; i++) {
        scanf("%s",map[i]);
    }
    for (int i=0; i<N; i++) {
        for (int j=0; j<M; j++) {
            if(map[i][j]=='S')
            {
                sx=i;
                sy=j;
            }
            if(map[i][j]=='G')
            {
                gx=i;
                gy=j;
            }
        }
    }
    ans=bfs(sx, sy, gx, gy);
    printf("%d\n",ans);
    return 0;
}

這裏我是用的測試樣例是

5 5

S#000

0#0#0

00000

0###0

000#G

用上面兩個代碼運行可以得到以下結果:

DFS:

5 5

S#000

0#0#0

00000

0###0

000#G

1 0

2 0

3 0

4 0

4 1

4 2

2 1

2 2

1 2

0 2

0 3

0 4

1 4

2 4

3 4

4 4

12

Program ended with exit code: 0

BFS:

5 5

S#000

0#0#0

00000

0###0

000#G

0 0

1 0

2 0

3 0

2 1

4 0

2 2

5 0

4 1

2 3

1 2

5 1

4 2

2 4

0 2

5 2

3 4

2 5

1 4

0 3

5 3

4 4

8

Program ended with exit code: 0

可以看出BFS找到的是最短路徑,而DFS找到的路徑則與方向向量的設置有緊密關係,而不一定是最短路徑。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章