DFS BFS的典型案例

https://github.com/AbitGo/Algorithm/blob/main/Master%20algorithm/src/Algorithm/DFS_BFS/BFS_Maze.java

BFS类型

package Algorithm.DFS_BFS;

import DataStructure.BaseDataStructureUtil.LinerList.queue.LinkSqeue;

public class BFS_Maze {
    static class node {
        int x;
        int y;
        int step=0;
        public node(int x, int y,int step) {
            this.x = x;
            this.y = y;
            this.step= step;
        }
    }

    static int[] X = {0, 0, 1, -1};
    static int[] Y = {1, -1, 0, 0};
    static char[][] matix_mn = {
            {'.', '.', '.', '.', '.'},
            {'.', '*', '.', '*', '.'},
            {'.', '*', 'S', '*', '.'},
            {'.', '*', '*', '*', '.'},
            {'.', '.', '.', 'T', '*'}
    };
    static int n = matix_mn[0].length;
    static int m = matix_mn.length;
    static int count = 0;


    //C语言为bool inq[m][n] = {false}
    static boolean inq[][] = new boolean[m][n];

    //判断该点是否需要访问
    static boolean judge(int x, int y) {
        if (x >= m || x < 0 || y < 0 || y >= n) {
            return false;
        }
        //墙壁* 以及入队的话
        if (matix_mn[x][y] == '*' || inq[x][y] == true) {
            return false;
        }
        return true;
    }

    static int BFS(int x, int y) {
        //queue<int> q;
        //q.push(x,y);
        //while(!p.empty()){
        //取出队首元素top
        //访问队首元素top
        //将队首元素出队
        //将队首的下一层元素中未曾访问的结点全部入队
        //并设置为已入队即可
        LinkSqeue q = new LinkSqeue();
        //将头节点入队即可
        node s = new node(x, y,0);
        q.offer(s);

        //该节点已经被入队了
        inq[x][y] = true;
        while (!q.isEmpty()) {
            node top = (node) q.peek();
            q.poll();
            if(matix_mn[top.x][top.y]=='T'){
                return top.step;
            }
            for (int i = 0; i < 4; i++) {
                int newX = top.x + X[i];
                int newY = top.y + Y[i];
                //符合条件即入队
                if (judge(newX, newY)) {
                    q.offer(new node(newX, newY,top.step+1));
                    inq[newX][newY] = true;
                }
            }

        }
        return -1;

    }

    public static void main(String[] args) {
        //从matix[2][2]作为开始起点
        count = BFS(2,2);

        System.out.println("count:" + count);
    }
}

DFS类型

package Algorithm.DFS_BFS;

public class DFS_Maze {
    int[] X = {0,0,1,-1};
    int[] Y = {-1,1,0,0};
    static char[][] matix_mn = {
            {'.', '.', '.', '.', '.'},
            {'.', '*', '.', '*', '.'},
            {'.', '*', 'S', '*', '.'},
            {'.', '*', '*', '*', '.'},
            {'.', '.', '.', 'T', '*'}
    };
    static int[][] visit=new int[10][10];

    public static void main(String[] args) {
        BFS(3,3,4,4);
    }
    public static void BFS(int startX,int startY,int endX,int endY){
        //答题思路其实差不多
        //通过DFS进行层层递归
        //其实代码和BFS大致相似
        //只不过没利用队列而已

    }

}

### 关于DFS深度优先搜索)BFS(广度优先搜索)算法的参考资料 对于希望深入了解DFS深度优先搜索)BFS(广度优先搜索)这两种重要算法的人来说,有多种资源可供查阅。 #### 学术论文与书籍推荐 学术界有许多经典著作探讨了这些主题。例如,《Introduction to Algorithms》由Thomas H. Cormen等人编著的一本书籍,在计算机科学领域享有盛誉,其中包含了对这两类算法详尽而系统的介绍[^1]。 #### 在线教程及课程材料 网络上也存在大量优质的在线教育资源可以帮助理解这两个概念及其应用方式。像Coursera、edX这样的平台提供了来自顶尖大学开设的数据结构与算法专项课程;此外还有许多个人博客技术社区分享着实用的学习指南以及实战案例研究[^2]。 #### 开源项目文档 参与开源软件开发也是一种极佳的方式去实践所学理论知识并加深印象。GitHub等平台上众多活跃维护中的项目往往附带详细的README文件说明其内部工作流程——这当中不乏涉及到了如何运用DFS/BFS解决问题的具体例子[^3]。 ```python def dfs(graph, start): visited = set() def helper(node): if node not in visited: print(f'Visiting {node}') visited.add(node) for neighbor in graph[node]: helper(neighbor) helper(start) from collections import deque def bfs(graph, start): visited = set([start]) queue = deque([start]) while queue: vertex = queue.popleft() print(f'Visiting {vertex}') for neighbour in graph[vertex]: if neighbour not in visited: visited.add(neighbour) queue.append(neighbour) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值