【LeetCode】 Surrounded Regions (BFS && DFS)

HIT_微笑前進發表於2015-03-28

題目: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>


相關文章