代码随想录——图论一刷day01

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、力扣797. 所有可能的路径
  • 二、力扣200. 岛屿数量<深搜版>
  • 三、力扣200. 岛屿数量<广搜版>


前言


一、力扣797. 所有可能的路径

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
        fun(graph,0);
        return res;
    }
    public void fun(int[][] graph, int cur){
        if(cur == graph.length -1){
                path.add(cur);
                res.add(new ArrayList<>(path));
                path.remove(path.size()-1);
                return;
            }
        for(int i = 0; i < graph[cur].length; i ++){
            path.add(cur);
            fun(graph, graph[cur][i]);
            path.remove(path.size()-1);
        }
    }
}

二、力扣200. 岛屿数量<深搜版>

版本一

class Solution {
    public int numIslands(char[][] grid) {
        int res = 0;
        for(int i = 0; i < grid.length; i ++){
            for(int j = 0; j < grid[i].length; j ++){
                if(grid[i][j] == '1'){
                    res ++;
                    dfs(grid, i, j);
                }
            }
        }
        return res;
    }
    public void dfs(char[][] grid, int i, int j){
        if(i < 0 || i >= grid.length || j < 0 || j >= grid[i].length || grid[i][j] == '0'){
            return;
        }
        grid[i][j] = '0';
        dfs(grid, i, j + 1);
        dfs(grid, i , j - 1);
        dfs(grid, i - 1, j);
        dfs(grid, i + 1, j);
    }
}

版本二

class Solution {
    boolean[][] flag;
    int[][] dir = {
        {0,1},
        {0,-1},
        {-1,0},
        {1,0}
    };
    public int numIslands(char[][] grid) {
        flag = new boolean[grid.length][grid[0].length];
        int res = 0;
        for(int i = 0; i < grid.length; i ++){
            for(int j = 0; j < grid[i].length; j ++){
                if(flag[i][j] == false && grid[i][j] == '1'){
                    res ++;
                    dfs(grid, i, j);
                }
            }
        }
        return res;
    }
    public void dfs(char[][] grid, int x, int y){
        if(flag[x][y] == true || grid[x][y] == '0'){
            return ;
        }
        flag[x][y] = true;
        for(int i = 0; i < 4; i ++){
            int nextX = x + dir[i][0];
            int nextY = y + dir[i][1];
            if(nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[x].length){
                continue ;
            }
            dfs(grid, nextX, nextY);
        }
    }
}

三、力扣200. 岛屿数量<广搜版>

class Solution {
    boolean[][] flag;
    int[][] move = {
        {0,1},
        {0,-1},
        {-1,0},
        {1,0}
    };
    public int numIslands(char[][] grid) {
        flag = new boolean[grid.length][grid[0].length];
        int count = 0;
        for(int i = 0; i < grid.length; i ++){
            for(int j = 0; j < grid[i].length; j ++){
                if(flag[i][j] == false && grid[i][j] == '1'){
                    count ++;
                    bfs(grid, i, j);
                }
            }
        }
        return count;
    }
    public void bfs(char[][] grid, int x, int y){
        Deque<int[]> deq = new ArrayDeque<>();
        deq.offerLast(new int[]{x,y});
        flag[x][y] = true;
        while(!deq.isEmpty()){
            int[] cur = deq.pollFirst();
            for(int i = 0; i < 4; i ++){
                int nextX = cur[0] + move[i][0];
                int nextY = cur[1] + move[i][1];
                if(nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[x].length ||                     grid[nextX][nextY] == '0' || flag[nextX][nextY] == true){
                    continue;
                }
                deq.offerLast(new int[]{nextX,nextY});
                flag[nextX][nextY] = true;
            }
        }
    }
}

你可能感兴趣的:(图论,深度优先,算法,数据结构,leetcode,java,广度优先)