leetcode-surrounded regions-ZZ

Problem Statement (link):

Given a 2D board containing ‘X‘ and ‘O‘, capture all regions surrounded by ‘X‘.
A region is captured by flipping all ‘O‘s into ‘X‘s in that surrounded region.
For example,

X X X X
X O O X
X X O X
X O X X

After running your function, the board should be:

X X X X
X X X X
X X X X
X O X X

Analysis:
Rather than recording the 2D positions for any scanned ‘O‘, a trick is to substitute any border ‘O‘s with another character - here in the Code I use ‘Y‘. And scan the board again to change any rest ‘O‘s to ‘X‘s, and change ‘Y‘s back to ‘O‘s.

We start searching ‘O‘ from the four borders. I tried DFS first, the OJ gives Runtime error on the 250x250 large board; In the Sol 2 below, I implement BFS instead, and passed all tests.

The time complexity is O(n^2), as in the worst case, we may need to scan the entire board.

Code:
1, DFS

 1 class Solution {
 2 public:
 3     // dfs - Runtime error on large board 250x250
 4     void dfs(vector<vector<char>> &board, int r, int c) {
 5         if (r<0||r>board.size()-1||c<0||c>board[0].size()-1||board[r][c]!=‘O‘)
 6             return;
 7         board[r][c]=‘Y‘;
 8         dfs(board, r-1, c);
 9         dfs(board, r+1, c);
10         dfs(board, r, c-1);
11         dfs(board, r, c+1);
12     }
13     void solve(vector<vector<char>> &board) {
14         if (board.empty() || board.size()<3 || board[0].size()<3)
15             return;
16         int r=board.size();
17         int c=board[0].size();
18         // dfs from boundary to inside
19         for (int i=0; i<c; i++) {
20             if (board[0][i]==‘O‘)
21                 dfs(board, 0, i);   // first row
22             if (board[c-1][i]==‘O‘)
23                 dfs(board, c-1, i); // last row
24         }
25         for (int i=0; i<board.size(); i++) {
26             if (board[i][0]==‘O‘)
27                 dfs(board, i, 0);   // first col
28             if (board[i][c-1])
29                 dfs(board, i, c-1); // last col
30         }
31         // scan entire matrix and set values
32         for (int i=0; i<board.size(); i++) {
33             for (int j=0; j<board[0].size(); j++) {
34                 if (board[i][j]==‘O‘)
35                     board[i][j]=‘X‘;
36                 else if (board[i][j]==‘Y‘)
37                     board[i][j]=‘O‘;
38             }
39         }
40     }
41 };

2, BFS

 1 class Solution {
 2 public:
 3     void solve(vector<vector<char>> &board) {
 4         if (board.empty() || board.size()<3 || board[0].size()<3)
 5             return;
 6         int r=board.size();
 7         int c=board[0].size();
 8         // queues to store row and col indices
 9         queue<int> qr;
10         queue<int> qc;
11         // start from boundary
12         for (int i=0; i<c; i++) {
13             if (board[0][i]==‘O‘) { qr.push(0); qc.push(i); }
14             if (board[r-1][i]==‘O‘) { qr.push(r-1); qc.push(i); }
15         }
16         for (int i=0; i<r; i++) {
17             if (board[i][0]==‘O‘) { qr.push(i); qc.push(0); }
18             if (board[i][c-1]==‘O‘) { qr.push(i); qc.push(c-1); }
19         }
20         // BFS
21         while (!qr.empty()) {
22             int rt=qr.front(); qr.pop();
23             int ct=qc.front(); qc.pop();
24             board[rt][ct]=‘Y‘;
25             if (rt-1>=0 && board[rt-1][ct]==‘O‘) { qr.push(rt-1); qc.push(ct); }  //go up
26             if (rt+1<r && board[rt+1][ct]==‘O‘) { qr.push(rt+1); qc.push(ct); } // go down
27             if (ct-1>=0 && board[rt][ct-1]==‘O‘) { qr.push(rt); qc.push(ct-1); } // go left
28             if (ct+1<c && board[rt][ct+1]==‘O‘) { qr.push(rt); qc.push(ct+1); } // go right
29         }
30
31         // scan entire matrix and set values
32         for (int i=0; i<board.size(); i++) {
33             for (int j=0; j<board[0].size(); j++) {
34                 if (board[i][j]==‘O‘) board[i][j]=‘X‘;
35                 else if (board[i][j]==‘Y‘) board[i][j]=‘O‘;
36             }
37         }
38     }
39 };

http://justcodings.blogspot.com/2014/07/leetcode-surrounded-regions.html

时间: 2024-10-05 23:26:56

leetcode-surrounded regions-ZZ的相关文章

LeetCode: Surrounded Regions [130]

[题目] Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X X X O O X X X O X X O X X After running your function, the board sh

[LeetCode] Surrounded Regions(DFS、BFS)

Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X X X O O X X X O X X O X X After running your function, the board should

验证LeetCode Surrounded Regions 包围区域的DFS方法

在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ,一直百思不得其解其中的原因,直到有网友告诉我说他验证了最后一个大集合在本地机子上可以通过,那么我也来验证看看吧. class Solution { public: void solve(vector<vector<char> >& board) { for (int i =

LeetCode: Surrounded Regions 解题报告

Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example,X X X XX O O XX X O XX O X XAfter running your function, the

[leetcode]Surrounded Regions @ Python

原题地址:https://oj.leetcode.com/problems/surrounded-regions/ 题意: Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X X X O O X

LEETCODE —— Surrounded Regions

Total Accepted: 43584 Total Submissions: 284350 Difficulty: Medium Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X X X O

[LeetCode] Surrounded Regions 包围区域

Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X X X O O X X X O X X O X X After running your function, the board should

[LeetCode] Surrounded Regions 广度搜索

Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X X X O O X X X O X X O X X After running your function, the board should

【LeetCode】 Surrounded Regions (BFS &amp;&amp; DFS)

题目:Surrounded Regions 广搜和深搜都能解决,但是LeetCode上使用深搜时会栈溢出 DFS: <span style="font-size:18px;">/*LeetCode Surrounded Regions * 题目:给定一个字符数组,由'X'和'O'组成,找到所有被x包围的o并将其替换为x * 思路:只要替换被包围的o就行,如果有一个o是边界或者上下左右中有一个是o且这个o不会被替换,则该点也不会被替换 * 从四条边开始,因为在这4周的一定不是

LeetCode OJ - Surrounded Regions

我觉得这道题和传统的用动规或者贪心等算法的题目不同.按照题目的意思,就是将被'X'围绕的'O'区域找出来,然后覆盖成'X'. 那问题就变成两个子问题: 1. 找到'O'区域,可能有多个区域,每个区域'O'都是相连的: 2. 判断'O'区域是否是被'X'包围. 我采用树的宽度遍历的方法,找到每一个'O'区域,并为每个区域设置一个value值,为0或者1,1表示是被'X'包围,0则表示不是.是否被'X'包围就是看'O'区域的边界是否是在2D数组的边界上. 下面是具体的AC代码: class Boar