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大致相似
        //只不过没利用队列而已

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值