"Leetcode" surrounded regions (BFS && DFS)

Source: Internet
Author: User

Title: Surrounded regions

Wide search and deep search can be solved, but leetcode on the use of deep search will overflow the stack

Dfs:

<span style= "FONT-SIZE:18PX;" >/*leetcode surrounded Regions * Title: Given an array of characters, consisting of ' X ' and ' O ', find all o surrounded by x and replace it with X-ideas: Just replace the surrounded O-line, If there is an O that is the boundary or if one of the upper and lower left is O and this o is not replaced, then the point will not be replaced * starting from four sides, because in this 4 weeks must not be surrounded so use them to start to find the wide search queue, if the queue is empty, then all O is surrounded */package Javatrain;public class Train25 {public static void solve (char[][] board) {Long n = board.length;if (n==0) Return;long m = b Oard[0].length; for (Long i = 0;i < m;i++) {//The first and last line characters are extensively searched BFS (board,0,i); BFS (board,n-1,i);} for (Long j = 1;j < n-1;j++) {//wide search for characters in the first and last columns, removing 4-edged duplicate characters BFS (board,j,0); BFS (board,j,m-1); for (int i = 0;i < n;i++) {if (int j = 0;j < m;j++) {if (board[i][j] = = ' O ') board[i][j] = ' X ';//The enclosing O is required to replace else if (board[i ][J] = = ' $ ') board[i][j] = ' o ';//marked non-enclosed O remains as-is}}}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; The points of the boundary are not surrounded by 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 &L T 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, time complexity O (n), Space complexity O (N) class Solution {Public:void solve (vector<vector<ch        Ar>> &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> STA        te_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;            Up or down 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])) {//both tagged and re-function board[new_state                    S[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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.