Showing posts with label BFS. Show all posts
Showing posts with label BFS. Show all posts

Monday, December 14, 2015

LeetCode [317] Shortest Distance from All Buildings

317. Shortest Distance from All Buildings
Hard

You want to build a house on an empty land which reaches all buildings in the shortest amount of distance. You can only move up, down, left and right. You are given a 2D grid of values 01 or 2, where:

  • Each 0 marks an empty land which you can pass by freely.
  • Each 1 marks a building which you cannot pass through.
  • Each 2 marks an obstacle which you cannot pass through.

Example:

Input: [[1,0,2,0,1],[0,0,0,0,0],[0,0,1,0,0]]

1 - 0 - 2 - 0 - 1
|   |   |   |   |
0 - 0 - 0 - 0 - 0
|   |   |   |   |
0 - 0 - 1 - 0 - 0

Output: 7 

Explanation: Given three buildings at (0,0), (0,4), (2,2), and an obstacle at (0,2),
             the point (1,2) is an ideal empty land to build a house, as the total 
             travel distance of 3+3+1=7 is minimal. So return 7.

Note:
There will be at least one building. If it is not possible to build such house according to the above rules, return -1.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#define N 100
class Solution {
public:
    int shortestDistance(vector<vector<int>>& grid) {
        int m = grid.size();
        if(m==0) return 0;
        int n = grid[0].size();
        if(n==0) return 0;

        int nb = 0;//count the number of buildings
        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                if(grid[i][j]==1){
                    nb++;
                }
            }
        }

        int ret = INT_MAX;
        bool found = false;//is true of there exists a valid place which can reach all buildings

        //check every cell if it is not an obstacle, grid[i][j]==0 or 1
        //if grid[i][j]==0, this is an potential valid place 
        //if grid[i][j]==1, check if we can reach all other buildings from [i][j]. If not, ie., there exists another buildings [i', j'] 
        //such that grid[i'][j']==1 and [i, j] cannot reach [i', j'], thus, there mush not exist an valid place which can reach all buildings. 
        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                if(grid[i][j]==2) continue;//cannot pass through an obstacle
                bool visited[N][N] = {false};
                queue<vector<int>> que;
                int cnt = 0, dist = 0;
                vector<int> cor={i, j, 0};//row id, column id, current steps
                que.push(cor);
                visited[i][j] = true;
                while(!que.empty()){
                    int x = que.front()[0];
                    int y = que.front()[1];
                    int step = que.front()[2];
                    que.pop();
                    if(grid[x][y]==1){//found an buildings which can be reached from [i, j] in "step" steps
                        cnt++;
                        dist += step;
                    }

                    //continue bfs if 1) current place is empty; 2) current place is the original place
                    if(grid[x][y]==0||(i==x && j==y)){
                        if(x-1>=0 && grid[x-1][y]<2 && !visited[x-1][y]){vector<int> v = {x-1, y, step+1}; que.push(v); visited[x-1][y] = true;};
                        if(x+1<m  && grid[x+1][y]<2 && !visited[x+1][y]){vector<int> v = {x+1, y, step+1}; que.push(v); visited[x+1][y] = true;};
                        if(y-1>=0 && grid[x][y-1]<2 && !visited[x][y-1]){vector<int> v = {x, y-1, step+1}; que.push(v); visited[x][y-1] = true;};
                        if(y+1<n  && grid[x][y+1]<2 && !visited[x][y+1]){vector<int> v = {x, y+1, step+1}; que.push(v); visited[x][y+1] = true;};
                    }
                }

                //[i, j] is a buildings and it cannot reach all other buildings. Return -1.
                if(grid[i][j]==1 && cnt<nb) return -1;

                //[i, j] is an empty place and it can reach all buildings.
                if(cnt==nb && grid[i][j]==0){
                    found = true;
                    ret = min(ret, dist);
                }
            }
        }

        return found?ret:-1;
    }
};

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
public class Solution {
    public int shortestDistance(int[][] grid) {
        if (grid == null || grid[0].length == 0) return 0;
        final int[] shift = new int[] {0, 1, 0, -1, 0};
        
        int row  = grid.length, col = grid[0].length;
        int[][] distance = new int[row][col];
        int[][] reach = new int[row][col];
        int buildingNum = 0;
        
        for (int i = 0; i < row; i++) {
            for (int j =0; j < col; j++) {
                if (grid[i][j] == 1) {
                    buildingNum++;
                    Queue<int[]> myQueue = new LinkedList<int[]>();
                    myQueue.offer(new int[] {i,j});

                    boolean[][] isVisited = new boolean[row][col];
                    int level = 1;
                    
                    while (!myQueue.isEmpty()) {
                        int qSize = myQueue.size();
                        for (int q = 0; q < qSize; q++) {
                            int[] curr = myQueue.poll();
                            
                            for (int k = 0; k < 4; k++) {
                                int nextRow = curr[0] + shift[k];
                                int nextCol = curr[1] + shift[k + 1];
                                
                                if (nextRow >= 0 && nextRow < row && nextCol >= 0 && nextCol < col
                                    && grid[nextRow][nextCol] == 0 && !isVisited[nextRow][nextCol]) {
                                        //The shortest distance from [nextRow][nextCol] to thic building
                                        // is 'level'.
                                        distance[nextRow][nextCol] += level;
                                        reach[nextRow][nextCol]++;
                                        
                                        isVisited[nextRow][nextCol] = true;
                                        myQueue.offer(new int[] {nextRow, nextCol});
                                    }
                            }
                        }
                        level++;
                    }
                }
            }
        }
        
        int shortest = Integer.MAX_VALUE;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (grid[i][j] == 0 && reach[i][j] == buildingNum) {
                    shortest = Math.min(shortest, distance[i][j]);
                }
            }
        }
        
        return shortest == Integer.MAX_VALUE ? -1 : shortest;
        
        
    }
}

Saturday, November 28, 2015

LeetCode [310] Minimum Height Trees

Ref
[1] https://leetcode.com/problems/minimum-height-trees/
OJ
[2] https://leetcode.com/discuss/71656/c-solution-o-n-time-o-n-space

Sunday, November 8, 2015

LeetCode [302] Smallest Rectangle Enclosing Black Pixels

Ref
[1] https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/
[2] https://leetcode.com/discuss/68246/c-java-python-binary-search-solution-with-explanation

Thursday, November 5, 2015

LeetCode [301] Remove Invalid Parentheses

301. Remove Invalid Parentheses
Hard
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.
Note: The input string may contain letters other than the parentheses ( and ).
Example 1:
Input: "()())()"
Output: ["()()()", "(())()"]
Example 2:
Input: "(a)())()"
Output: ["(a)()()", "(a())()"]
Example 3:
Input: ")("
Output: [""]

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution {
    bool isvalid(string s){
        int cnt = 0;
        for(auto c:s){
            cnt += c=='(';
            cnt -= c==')';
            if(cnt<0) return false;
        }
        return cnt==0;
    }
public:
    vector<string> removeInvalidParentheses(string s) {
        int l = 0, r = 0;
        for(auto c:s){
            if(c=='(') l++;
            else{
                if(l==0) r += (c==')');
                else l -= (c==')');                
            }
        }      
        vector<string> ret;
        dfs(s, ret, 0, l, r);
        return ret;
    }
    
    void dfs(string s, vector<string>&ret, int pos, int l, int r){
        if(l==0 && r==0 && isvalid(s)){
            ret.push_back(s);
            return;
        }
        
        for(int i=pos; i<s.size(); ++i){
            if(i>pos && s[i]==s[i-1]) continue;
            if(!(s[i]=='(' && l>0 || s[i]==')' && r>0)) continue;
            string s1 = s;
            
            s1.erase(i, 1);
            if(s[i]=='(' && l>0) dfs(s1, ret, i, l-1, r);
            else if(s[i]==')' && r>0) dfs(s1, ret, i, l, r-1);
        }
    }
};

Thursday, September 24, 2015

LeetCode [286] Walls and Gates


286. Walls and Gates
Medium

You are given a m x n 2D grid initialized with these three possible values.

  1. -1 - A wall or an obstacle.
  2. 0 - A gate.
  3. INF - Infinity means an empty room. We use the value 231 - 1 = 2147483647 to represent INF as you may assume that the distance to a gate is less than 2147483647.

Fill each empty room with the distance to its nearest gate. If it is impossible to reach a gate, it should be filled with INF.

Example: 

Given the 2D grid:

INF  -1  0  INF
INF INF INF  -1
INF  -1 INF  -1
  0  -1 INF INF

After running your function, the 2D grid should be:

  3  -1   0   1
  2   2   1  -1
  1  -1   2  -1
  0  -1   3   4
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public:
    void wallsAndGates(vector<vector<int>>& rooms) {
        int m = rooms.size();
        if(m==0) return;
        int n = rooms[0].size();
        if(n==0) return;
        
        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                stack<pair<int, int>> stk;
                if(rooms[i][j]==0){
                    stk.push(pair<int, int>(i,j));
                    while(!stk.empty()){
                        int x = stk.top().first, y = stk.top().second;
                        stk.pop();
                        if(x-1>=0 && rooms[x-1][y]>rooms[x][y]+1){rooms[x-1][y] = rooms[x][y]+1; stk.push(pair<int, int>(x-1,y));}
                        if(x+1<m  && rooms[x+1][y]>rooms[x][y]+1){rooms[x+1][y] = rooms[x][y]+1; stk.push(pair<int, int>(x+1,y));}
                        if(y-1>=0 && rooms[x][y-1]>rooms[x][y]+1){rooms[x][y-1] = rooms[x][y]+1; stk.push(pair<int, int>(x,y-1));}
                        if(y+1<n  && rooms[x][y+1]>rooms[x][y]+1){rooms[x][y+1] = rooms[x][y]+1; stk.push(pair<int, int>(x,y+1));}
                    }
                }
            }
        }
    }
};

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
    int[][] dirs = new int[][]{{-1,0},{1,0},{0,-1},{0,1}};
    int m, n;
    public void wallsAndGates(int[][] rooms) {
        m = rooms.length;
        if(m==0) return;
        n = rooms[0].length;

        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                if(rooms[i][j]==0){
                    dfs(rooms, i, j);
                }
            }
        }
    }

    void dfs(int[][] rooms, int i0, int j0){
        for(int[] d : dirs){
            int i = i0+d[0], j = j0+d[1];
            if(i>=0 && i<m && j>=0 && j<n && rooms[i][j]!=-1){
                if(rooms[i][j]>rooms[i0][j0]+1){
                    rooms[i][j] = rooms[i0][j0]+1;
                    dfs(rooms, i, j);
                }
            }
        }
    }
}

Monday, August 17, 2015

LeetCode [261] Graph Valid Tree

Ref
[1] https://leetcode.com/problems/graph-valid-tree/
OJ

Saturday, April 11, 2015

LeetCode [200] Number of Islands

 200. Number of Islands

Medium

Given an m x n 2d grid map of '1's (land) and '0's (water), return the number of islands.

An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

 

Example 1:

Input: grid = [
  ["1","1","1","1","0"],
  ["1","1","0","1","0"],
  ["1","1","0","0","0"],
  ["0","0","0","0","0"]
]
Output: 1

Example 2:

Input: grid = [
  ["1","1","0","0","0"],
  ["1","1","0","0","0"],
  ["0","0","1","0","0"],
  ["0","0","0","1","1"]
]
Output: 3

 

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 300
  • grid[i][j] is '0' or '1'.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
vector<vector<int>> dir = {{-1,0},{1,0},{0,-1},{0,1}};

class Solution_dfs {
public:
    int numIslands(vector<vector<char>>& grid) {
        int m = grid.size();
        if(m==0) return 0;
        int n = grid[0].size();
        if(n==0) return 0;

        int cnt = 0;
        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                if(grid[i][j]=='1'){
                    cnt++;
                    dfs(grid, i, j, m, n);
                }
            }
        }
        return cnt;
    }

    void dfs(vector<vector<char>>& grid, int i, int j, int m, int n){
        grid[i][j] = '0';
        for(int d=0; d<4; ++d){
            int ii = i + dir[d][0];
            int jj = j + dir[d][1];
            if(ii>=0 && ii<m && jj>=0 && jj<n && grid[ii][jj]=='1'){
                dfs(grid, ii, jj, m, n);
            }
        }
    }
};

/****/
class Solution_bfs {
public:
    int numIslands(vector<vector<char>>& grid) {
        int m = grid.size();
        if(m==0) return 0;
        int n = grid[0].size();
        if(n==0) return 0;

        int cnt = 0;
        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                if(grid[i][j]=='1'){
                    cnt++;
                    queue<pair<int, int>> que;
                    que.push(pair<int, int>(i, j));
                    grid[i][j] = '0';
                    while(que.size()){
                        int x = que.front().first;
                        int y = que.front().second;
                        que.pop();
                        for(int d=0; d<4; ++d){
                            int x1 = x+dir[d][0];
                            int y1 = y+dir[d][1];
                            if(x1>=0 && x1<m && y1>=0 && y1<n && grid[x1][y1]=='1'){
                                que.push(pair<int, int>(x1, y1));
                                grid[x1][y1] = '0';
                            }
                        }
                    }
                }
            }
        }
        return cnt;
    }
};

/****/
class Solution_union {
    vector<int> id;
    int find(int i){
        while(id[i]!=i){
            i = id[i];
        }
        return i;
    }
    void myUnion(int i, int j){
        int ri = find(i);
        int rj = find(j);
        if(ri<=rj){
            id[rj] = ri;
        }else{
            id[ri] = rj;
        }
    }
public:
    int numIslands(vector<vector<char>>& grid) {
        int m = grid.size();
        if(m==0) return 0;
        int n = grid[0].size();
        if(n==0) return 0;
        int N = m*n;
        id.resize(N);

        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                id[i*n+j] = i*n+j;
            }
        }

        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                if(grid[i][j]=='1'){
                    for(int d=0; d<4; ++d){
                        int ii = i+dir[d][0];
                        int jj = j+dir[d][1];
                        if(ii>=0 && ii<m && jj>=0 & jj<n && grid[ii][jj]=='1'){
                            myUnion(i*n+j, ii*n+jj);
                        }
                    }
                }
            }
        }

        int cnt = 0;

        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                if(grid[i][j]=='1' && id[i*n+j]==i*n+j){
                    cnt++;
                }
            }
        }
        return cnt;
    }
};

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
    int[][] dirs = new int[][]{{-1,0},{1,0},{0,-1},{0,1}};
    int m, n;
    public int numIslands(char[][] grid) {
        m = grid.length;
        n = grid[0].length;
        int ret = 0;
        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                if(grid[i][j]=='1'){
                    ret++;
                    dfs(grid, i, j);
                }
            }
        }
        return ret;
    }
    
    void dfs(char[][] grid, int i, int j){
        for(int[] d : dirs){
            int ii = i+d[0], jj = j+d[1];
            if(ii>=0 && ii<m && jj>=0 && jj<n && grid[ii][jj]=='1'){
                grid[ii][jj]='0';
                dfs(grid, ii, jj);
            }
        }
    }
}

Follow up1: count rank 2 islands, where a rank 2 island is an island inside a lake located on a continent. A continent is a piece of land located in the ocean; the ocean is any body of water that touches the edges of the map. 

 

Example:

000000000

000001100

001111100

011000100

001010100

001000100

001111100

000000000

It should return 1.

 

=============

Follow up2: If the input 2d array is too large to fit in memory, how to handle?


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "head.h"

class Solution{
public:
    int rank2island(vector<vector<char> > &board){
        int m = board.size();
        if(!m) return 0;
        int n = board[0].size();
        if(!n) return 0;

 //       PrintVV(board, "board: the original board");
        queue<pair<int ,int>> que;
        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                if(board[i][j]=='0' && (i==0 || j==0 || i==m-1 || j==n-1)){
                    board[i][j] = '2';
                    que.push(pair<int, int>(i, j));
                }
            }
        }
        while(!que.empty()){
            int i = que.front().first, j = que.front().second;
            que.pop();
            bfs(board, i, j, m, n, que, '0', '2');
        }
 //       PrintVV(board, "\nboard:set ocean to 2");//2 is ocean
        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                if(board[i][j]=='2'){
                    bfs(board, i, j, m, n, que, '1', '3');
                }
            }
        }
        while(!que.empty()){
            int i = que.front().first, j = que.front().second;
            que.pop();
            bfs(board, i, j, m, n, que, '1', '3');
        }
//        PrintVV(board, "\nboard:set continent to 3");

        int cnt = 0;
        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                if(board[i][j]=='1') cnt++;
            }
        }

//        cout<<"\nthe number of rank 2 islands is "<<cnt<<endl;
        return cnt;
    }

    void bfs(vector<vector<char>> &board, int i, int j, int m, int n, queue<pair<int, int>> &que, char a, char b){
        if(i-1>=0 && board[i-1][j]==a){board[i-1][j]=b; que.push(pair<int, int>(i-1, j));}
        if(i+1<m && board[i+1][j]==a){board[i+1][j]=b; que.push(pair<int, int>(i+1, j));}
        if(j-1>=0 && board[i][j-1]==a){board[i][j-1]=b; que.push(pair<int, int>(i, j-1));}
        if(j+1<n && board[i][j+1]==a){board[i][j+1]=b; que.push(pair<int, int>(i, j+1));}
        if(i-1>=0 && j-1>=0 && board[i-1][j-1]==a){board[i-1][j-1]=b; que.push(pair<int, int>(i-1, j-1));}
        if(i-1>=0 && j+1<n && board[i-1][j+1]==a){board[i-1][j+1]=b; que.push(pair<int, int>(i-1, j+1));}
        if(i+1<m && j-1>=0 && board[i+1][j-1]==a){board[i+1][j-1]=b; que.push(pair<int, int>(i+1, j-1));}
        if(i+1<m && j+1<n && board[i+1][j+1]==a){board[i+1][j+1]=b; que.push(pair<int, int>(i+1, j+1));}
    }

};

int main(){
  vector<vector<char> > board1{{'0','0','0','0','0','0','0','0','0'},\
                               {'0','0','0','0','0','1','1','0','0'},\
                               {'0','0','1','1','1','1','1','0','0'},\
                               {'0','1','1','0','0','0','1','0','0'},\
                               {'0','0','1','0','1','0','1','0','0'},\
                               {'0','0','1','0','0','0','1','0','0'},\
                               {'0','0','1','1','1','1','1','0','0'},\
                               {'0','0','0','0','0','0','0','0','0'}};

  vector<vector<char> > board2{{'0','0','1','0','0','0','0','0','0'},\
                               {'0','1','1','1','1','1','0','0','0'},\
                               {'0','1','0','0','0','1','0','0','0'},\
                               {'0','1','0','1','0','1','0','0','0'},\
                               {'0','1','0','0','0','1','0','0','0'},\
                               {'0','1','1','1','1','1','0','0','0'},\
                               {'0','0','0','0','0','0','0','0','0'},\
                               {'0','0','0','0','0','0','0','0','0'},\
                               {'0','1','1','1','1','1','0','0','0'},\
                               {'0','1','0','0','0','1','0','0','0'},\
                               {'0','1','0','1','0','1','0','0','0'},\
                               {'0','1','0','0','0','1','0','0','0'},\
                               {'0','1','1','1','1','1','0','0','0'},\
                               {'0','0','0','0','0','0','0','0','0'}};
  Solution sol;
  sol.rank2island(board2);
  return 0;
}

YMSF

follow up: 返回每个island的面积。当有被陆地包围的湖泊时,湖泊面积也算在island内。

e.g.

[0,0,0,0,1]

[0,1,1,1,0]

[0,1,0,1,0]

[0,1,1,1,0]

[0,0,0,0,0]

两个island,第一个面积为1,第二个为9.


请问第一题有啥思路吗?我的做法是先遍历一遍,然后把所有的和边界相连的'0‘ 变成 ’*‘, 第二次遍历的时候剩下的'0' 和 '1' 就是岛屿面积。

不知道还有什么更好的方法


我给的也是这个办法。感觉复杂度应该很难提高了。


想问一下楼主第一题下面这三种算不算包围呢? 谢谢


[0,1,0]

[1,0,1]

[0,1,0]


[0,1,1]

[1,0,1]

[1,1,0]


[0,1,1]

[1,0,1]

[1,1,1]

算的,相邻按四个方向的来算。

题目问每个岛屿面积分别多少,没想明白被环绕的湖泊面积(环绕的0)怎么算,比如

0 0 0 0 1 0

1 1 1 0 1

1 0 1 0 1

1 1 1 1 0

0 0 0 0 0 0


难道是标完*以后第二次遍历的时候,不管是1还是0 都算同时分别计数0和1的数量?是我想简单了么...

求指教


YMSF

后头的followup貌似是正题,因为前头那个写的还挺快,但确确实实是正题,scenario不重要,但建议大家要review一下Dijkstra Algorithm。。。回头看了下刷题网竟然没有这个算法在矩阵上头应用的题。。。sooooo good luck