美文网首页
BFS与DFS机试模板

BFS与DFS机试模板

作者: CristianoC | 来源:发表于2020-05-08 15:03 被阅读0次

BFS的思想主要就是一层层扩散出去,使用一个辅助队列来实现,简单来讲就是每一步都要走可以走(判定边界,没走过等)的四个方向。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 105;
char mpt[maxn][maxn];
int vis[maxn][maxn];
int dir[4][2] = {0,1,1,0,-1,0,0,-1};
struct node{
    int x,y;
    int step;
};
int bfs(int sx,int sy){
    memset(vis,0, sizeof(vis));
    queue<node> q;
    q.push(node{sx,sy,0});
    vis[sx][sy] = 1;
    int ans = 0;
    while (!q.empty()){
        node now = q.front();
        q.pop();
        if(mpt[now.x][now.y] == 'E'){
            ans = now.step;
            break;
        }
        for(int i = 0;i < 4;i++){
            int nx = now.x + dir[i][0];
            int ny = now.y + dir[i][1];
            if((mpt[nx][ny] == '*'||mpt[nx][ny] == 'E')&&vis[nx][ny] == 0){
                q.push(node{nx,ny,now.step+1});
                vis[nx][ny] = 1;
            }
        }
    }
    return ans;
}
int main(){
    int h,w;
    while (cin>>h>>w){
        int sx,sy;
        for(int i = 1;i <= h;i++){
            cin>>mpt[i]+1;
            for(int j = 1;j <= w;j++){
                if(mpt[i][j] == 'S')
                    sx = i, sy = j;
            }
        }
        int ans = bfs(sx,sy);
        cout<<ans<<endl;
    }
    return 0;
}

DFS,简单来说就是一条路走到底,走不通的时候回溯回来再走另一个方向,要注意回溯的时候要把走过的路标记为没走

#include<bits/stdc++.h>
using namespace std;
const int maxn = 105;
char mpt[maxn][maxn];
int vis[maxn][maxn];
int dir[4][2] = {1,0,0,1,-1,0,0,-1};
int ans;
void dfs(int x,int y,int step){
    if(step >= ans)return;//步数超过之前记录最短的就不用继续
    if(mpt[x][y] == 'E'){
        ans = min(ans,step);
        return;
    }
    for(int i = 0;i < 4;i++){
        int nx = x + dir[i][0];
        int ny = y + dir[i][1];
        if((mpt[nx][ny] == '*' || mpt[nx][ny] == 'E')&& vis[nx][ny] == 0){
            vis[nx][ny] = 1;
            dfs(nx,ny,step+1);
            vis[nx][ny] = 0;//回溯的时候标记为未走
        }
    }
}
int main(){
    int h,w;
    while (cin>>h>>w){
        if(h == 0 && w == 0)
            break;
        int sx = 0,sy = 0;
        memset(mpt,0, sizeof(mpt));
        memset(vis,0, sizeof(vis));
        for(int i = 1;i <= h;i++){
            cin>>mpt[i] + 1;
            for(int j = 1;j <= w;j++){
                if(mpt[i][j] == 'S')
                    sx = i, sy = j;
            }
        }
        ans = 999999;
        dfs(sx,sy,0);
        cout<<ans<<endl;
    }
    return 0;
}

相关文章

  • BFS与DFS机试模板

    BFS的思想主要就是一层层扩散出去,使用一个辅助队列来实现,简单来讲就是每一步都要走可以走(判定边界,没走过等)的...

  • 用Python实现树的BFS与DFS

    一、BFS与DFS简介 在理解动态规划、BFS和DFS一文中,已经集合具体例子,介绍了图的BFS与DFS。但是比较...

  • 海岛问题

    通过这个问题把BFS、DFS、并查集的一些思路和程序主体模板进行一下总结。 其中BFS和DFS属于最基本最容易直接...

  • Binary Tree(2)

    BFS vs DFS for Binary Tree What are BFS and DFS for Binar...

  • BFS/DFS python模板与实现

    BFS BFS在搜索完第k层的节点之前,是不会搜索第k+1层的节点的。 BFS原理 BFS所用的是队列。把每个还没...

  • DFS与BFS LeetCode 刷题小结(一)

    本节我们将汇总一些 LeetCode bfs与dfs相关的题。 关于深度优先搜索(DFS)和广度优先搜索(BFS)...

  • LeetCode 第104题:二叉树的最大深度

    1、前言 2、思路 采用 DFS 或者 BFS 都可以。 3、代码 DFS: BFS:

  • 133. Clone Graph 复制无向图

    bfs dfs

  • BFS和DFS

    DFS BFS

  • BFS与DFS

    广度优先遍历 (BFS) 类似树的层次遍历,首先访问起始顶点v,然后选取与v邻接的全部顶点w1,w2,…wn,进行...

网友评论

      本文标题:BFS与DFS机试模板

      本文链接:https://www.haomeiwen.com/subject/reeightx.html