【LeetCode 934】Shortest Bridge

Subject description:

01 to a matrix, water represents 0, 1 represents island connected.
There are two islands.
Q. How many zeros to fill at least, be able to connect the two islands.

Ideas:

To find a random island, and the setting value with bfs of the island and the step values ​​0 and -1. Then at any point on the island began to bfs, this time to find the minimum value of 1 record, or join the queue. 1 or later found to continue the search, and finally returns the minimum value.

Code:

class Solution {
public:
    int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    
    int shortestBridge(vector<vector<int>>& A) {
        int n = A.size();
        int m = A[0].size();
        
        vector<vector<int> > step(n, vector<int>(m, INT_MAX));
        int stx, sty;
        
        bool init = false; 
        for (int i=0; i<n; ++i) {
            for (int j=0; j<m; ++j) {
                if (A[i][j] == 1) {
                    stx = i;
                    sty = j;
                    init_func(i, j, A, step);
                    init = true;
                    break;
                }
            }
            if (init) break;
        }
        
        int ans = bridge(stx, sty, A, step);
        return ans-1;
    }
    
    int bridge(int stx, int sty, vector<vector<int>>& A, vector<vector<int>>& step) {
        int n = A.size();
        int m = A[0].size();
        vector<vector<bool>> vis(n, vector<bool>(m, false));
        vis[stx][sty] = 1;
        queue<pair<int, int>> que;
        que.push(make_pair(stx, sty));
        int ans = INT_MAX;
        
        while(!que.empty()) {
            int curx = que.front().first;
            int cury = que.front().second;
            que.pop();
            
            for (int i=0; i<4; ++i) {
                int nxtx = curx + dir[i][0];
                int nxty = cury + dir[i][1];
                if (nxtx < 0 || nxtx >= n || nxty < 0 || nxty >= m) continue;
                
                if (A[nxtx][nxty] == -1 && !vis[nxtx][nxty]) {
                    que.push(make_pair(nxtx, nxty));
                    vis[nxtx][nxty] = 1;
                }
                if (A[nxtx][nxty] == 0 && step[nxtx][nxty] > step[curx][cury] + 1) {
                    step[nxtx][nxty] = step[curx][cury] + 1;
                    que.push(make_pair(nxtx, nxty));
                }
                if (A[nxtx][nxty] == 1) {
                    step[nxtx][nxty] = step[curx][cury] + 1;
                    ans = min(ans, step[nxtx][nxty]);
                }
            }
        }
        return ans;
    }
    
    
    void init_func(int x, int y, vector<vector<int>>& A, vector<vector<int>>& step) {
        int n = A.size();
        int m = A[0].size();
        queue<pair<int, int>> que;
        que.push(make_pair(x, y));
        step[x][y] = 0;
        A[x][y] = -1;
        
        while(!que.empty()) {
            int curx = que.front().first;
            int cury = que.front().second;
            que.pop();
            
            for (int i=0; i<4; ++i) {
                int nxtx = curx + dir[i][0];
                int nxty = cury + dir[i][1];
                if (nxtx < 0 || nxtx >= n || nxty < 0 || nxty >= m) continue;
                if (step[nxtx][nxty] && A[nxtx][nxty] == 1) {
                    step[nxtx][nxty] = 0;
                    A[nxtx][nxty] = -1;
                    que.push(make_pair(nxtx, nxty));
                } 
            }
        }
    }
};

End condition here is that the queue is empty.
When bfs first find of most value to the value of it.
Just saw the same table to the same floor of the English sister to send fruit.
He fall in love.
Lemon flavor.

Guess you like

Origin blog.csdn.net/iCode_girl/article/details/92794544