图论 day1

class Solution {
    List<List<Integer>> result;
    List<Integer> path;
    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
        path = new LinkedList<>();
        result = new LinkedList<>();
        path.add(0);
        dfs(graph,0);
        return result;
    }
    public void dfs(int[][] graph, int node){
        //当前节点是最后一个节点
        if (node==graph.length-1){
            result.add(new LinkedList<>(path));
            return;
        }
        // 循环该节点的各条路径
        for (int index=0;index<graph[node].length;index++){
            int nextNode = graph[node][index];
            path.add(nextNode);
            dfs(graph,nextNode);
            path.removeLast();
        }
    }
}

class Solution {
    public int numIslands(char[][] grid) {
        int result = 0;
        //遍历网格,找到岛屿(即:‘1’)
        for (int i=0;i<grid.length;i++){
            for (int j=0;j<grid[0].length;j++){
                if (grid[i][j]=='1'){
                    result++;
                    dfs(grid,i,j);
                }
            }
        }
        return result;
    }
    // 将找到的一个岛屿“淹没”,同时对其相连的岛屿淹没,因为这属于同一片岛屿
    public void dfs(char[][] grid, int i, int j){
        if(i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == '0') return;
        grid[i][j] = '0';
        dfs(grid,i-1,j);
        dfs(grid,i+1,j);
        dfs(grid,i,j-1);
        dfs(grid,i,j+1);
    }
}
class Solution {
    public int numIslands(char[][] grid) {
        if (grid == null || grid.length == 0) {
            return 0;
        }

        int nr = grid.length;
        int nc = grid[0].length;
        int num_islands = 0;

        for (int r = 0; r < nr; ++r) {
            for (int c = 0; c < nc; ++c) {
                if (grid[r][c] == '1') {
                    ++num_islands;
                    grid[r][c] = '0';
                    Queue<Integer> neighbors = new LinkedList<>();
                    neighbors.add(r * nc + c);
                    while (!neighbors.isEmpty()) {
                        int id = neighbors.remove();
                        int row = id / nc;
                        int col = id % nc;
                        if (row - 1 >= 0 && grid[row-1][col] == '1') {
                            neighbors.add((row-1) * nc + col);
                            grid[row-1][col] = '0';
                        }
                        if (row + 1 < nr && grid[row+1][col] == '1') {
                            neighbors.add((row+1) * nc + col);
                            grid[row+1][col] = '0';
                        }
                        if (col - 1 >= 0 && grid[row][col-1] == '1') {
                            neighbors.add(row * nc + col-1);
                            grid[row][col-1] = '0';
                        }
                        if (col + 1 < nc && grid[row][col+1] == '1') {
                            neighbors.add(row * nc + col+1);
                            grid[row][col+1] = '0';
                        }
                    }
                }
            }
        }

        return num_islands;
    }
}

上一篇:智慧工业园区的物联网解决方案


下一篇:Go使用Terraform 库