1. 程式人生 > >leetcode 130. 被圍繞的區域

leetcode 130. 被圍繞的區域

這道題我用的逆向思維法,用DFS判斷和邊上’O’連線的’O’,然後將他們標記為星號,標記完全部之後,再遍歷一次陣列,將’O’換為‘X’,’*‘換為O,即可完成。
這個演算法可圈點的地方在於,DFS用的是棧實現的,以前實現DFS總是用遞迴,沒想過非遞迴如何實現,如今又是一個進步。不過我很奇怪為什麼我第一次用同樣思路,但是用BFS寫的演算法會超時,有大神幫我看一下嗎?
DFS
20ms

struct POS
{
    int x;
    int y;
    POS(int newx, int newy): x(newx), y(newy) {}
};

class Solution {
public
: void solve(vector<vector<char>> &board) { if(board.empty() || board[0].empty()) return; int m = board.size(); int n = board[0].size(); for(int i = 0; i < m; i ++) { for(int j = 0; j < n; j ++) { if
(board[i][j] == 'O') { if(i == 0 || i == m-1 || j == 0 || j == n-1) {// remain 'O' on the boundry dfs(board, i, j, m, n); } } } } 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'; } } } void dfs(vector<vector<char>> &board, int i, int j, int m, int n) { stack<POS*> stk; POS* pos = new POS(i, j); stk.push(pos); board[i][j] = '*'; while(!stk.empty()) { POS* top = stk.top(); if(top->x > 0 && board[top->x-1][top->y] == 'O') { POS* up = new POS(top->x-1, top->y); stk.push(up); board[up->x][up->y] = '*'; continue; } if(top->x < m-1 && board[top->x+1][top->y] == 'O') { POS* down = new POS(top->x+1, top->y); stk.push(down); board[down->x][down->y] = '*'; continue; } if(top->y > 0 && board[top->x][top->y-1] == 'O') { POS* left = new POS(top->x, top->y-1); stk.push(left); board[left->x][left->y] = '*'; continue; } if(top->y < n-1 && board[top->x][top->y+1] == 'O') { POS* right = new POS(top->x, top->y+1); stk.push(right); board[right->x][right->y] = '*'; continue; } stk.pop(); } } };

BFS
TLE

class Solution {
public:
    void solve(vector<vector<char>>& board) {
        int m = board.size();
        if (m == 0)
            return;
        int n = board[0].size();
        for (int i = 0; i<n; ++i) {
            if (board[0][i] == 'O') {
                process(board, 0, i);
            }
            if (board[m - 1][i] == 'O') {
                process(board, m - 1, i);
            }
        }
        for (int i = 1; i<m - 1; ++i) {
            if (board[i][0] == 'O') {
                process(board, i, 0);
            }
            if (board[i][n - 1] == 'O') {
                process(board, i, n - 1);
            }
        }
        for (auto &k : board) {
            for (auto &t : k) {
                if (t == 'Z')
                    t = 'O';
                else if (t == 'O')
                    t = 'X';
            }
        }
        return;
    }
    void process(vector<vector<char>>& board, int p, int q) {
        vector<int>xaxi{ 1,-1,0,0 };
        vector<int>yaxi{ 0,0,1,-1 };
        deque<pair<int, int>>cur;
        deque<pair<int, int>>next;
        cur.push_back({ p,q });
        while (!cur.empty()) {
            auto tmp = cur.front();
            cur.pop_front();
            board[tmp.first][tmp.second] = 'Z';
            for (int i = 0; i<4; ++i) {
                int x = tmp.first + xaxi[i];
                int y = tmp.second + yaxi[i];
                if (x >= 0 && x<board.size() && y >= 0 && y<board[0].size()) {
                    if (board[x][y] == 'O') {
                        next.push_back({ x,y });
                    }
                }
            }
            if (cur.empty()) {
                swap(cur, next);
            }
        }
    }
};