【LeetCode】 Surrounded Regions (BFS && DFS)

來源:互聯網
上載者:User

標籤:dfs   bfs   leetcode   

題目:Surrounded Regions

廣搜和深搜都能解決,但是LeetCode上使用深搜時會棧溢出

DFS:

<span style="font-size:18px;">/*LeetCode Surrounded Regions * 題目:給定一個字元數組,由'X'和'O'組成,找到所有被x包圍的o並將其替換為x * 思路:只要替換被包圍的o就行,如果有一個o是邊界或者上下左右中有一個是o且這個o不會被替換,則該點也不會被替換 * 從四條邊開始,因為在這4周的一定不是被包圍的所以用他們開始找到廣搜的隊列,如果隊列為空白,那麼就是所有的o都被包圍 */package javaTrain;public class Train25 {public static void solve(char[][] board) {long n = board.length;if(n==0) return;long m = board[0].length; for(long i = 0;i < m;i++){//對第一行和最後一行的字元進行廣搜bfs(board,0,i);bfs(board,n-1,i);}for(long j = 1;j < n-1;j++){ //對第一列和最後一列的字元進行廣搜,去除4條邊重複的字元bfs(board,j,0);bfs(board,j,m-1);}for(int i = 0;i < n;i++){for(int j = 0;j < m;j++){if(board[i][j] == 'O') board[i][j] = 'X';//被包圍的o需取代else if(board[i][j] == '$') board[i][j] = 'O';//標記的不被包圍的o保持原樣}}}private static void bfs(char[][] board,int i,int j){long n = board.length;long m = board[0].length;if(i < 0 || i>=n||j<0||j>=m||board[i][j] != 'O') return; //邊界的點都不被包圍 board[i][j] = '$'; bfs(board,i-1,j);bfs(board,i,j-1);bfs(board,i+1,j);bfs(board,i,j+1); } public static void main(String args[]){char board[][] = {{'O','X','O'},{'X','O','X'},{'O','X','O'}};solve(board); for(int i = 0;i < board.length;i++){for(int j = 0;j < board[0].length;j++){ System.out.print(board[i][j]);}System.out.println();} }}</span>

BFS:

<span style="font-size:18px;">// LeetCode, Surrounded Regions// BFS,時間複雜度O(n),空間複雜度O(n)class Solution {public:    void solve(vector<vector<char>> &board) {        if (board.empty()) return;        const int m = board.size();        const int n = board[0].size();        for (int i = 0; i < n; i++) {            bfs(board, 0, i);            bfs(board, m - 1, i);        }        for (int j = 1; j < m - 1; j++) {            bfs(board, j, 0);            bfs(board, j, n - 1);        }        for (int i = 0; i < m; i++)            for (int j = 0; j < n; j++)                if (board[i][j] == 'O')                    board[i][j] = 'X';                else if (board[i][j] == '+')                    board[i][j] = 'O';    }private:    void bfs(vector<vector<char>> &board, int i, int j) {        typedef pair<int, int> state_t;        queue<state_t> q;        const int m = board.size();        const int n = board[0].size();        auto is_valid = [&](const state_t &s) {            const int x = s.first;            const int y = s.second;            if (x < 0 || x >= m || y < 0 || y >= n || board[x][y] != 'O')                return false;            return true;        };        auto state_extend = [&](const state_t &s) {            vector<state_t> result;            const int x = s.first;            const int y = s.second;            // 上下左右            const state_t new_states[4] = {{x-1,y}, {x+1,y},                    {x,y-1}, {x,y+1}};            for (int k = 0; k < 4;  ++k) {                if (is_valid(new_states[k])) {                    // 既有標記功能又有去重功能                    board[new_states[k].first][new_states[k].second] = '+';                    result.push_back(new_states[k]);                }            }            return result;        };        state_t start = { i, j };        if (is_valid(start)) {            board[i][j] = '+';            q.push(start);        }        while (!q.empty()) {            auto cur = q.front();            q.pop();            auto new_states = state_extend(cur);            for (auto s : new_states) q.push(s);        }    }};</span>


【LeetCode】 Surrounded Regions (BFS && DFS)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.