[LeetCode] Surrounded Regions

简介: This problem is not quite difficult (a typical BFS traversal of graph), though, its aceptance rate is relatively low.

This problem is not quite difficult (a typical BFS traversal of graph), though, its aceptance rate is relatively low. In fact, the key obstacle in passing this problem is how to reduce the number of checks and avoid the annoying TLE.

There is a nice idea in this link. In fact, all the surrounded regions will not contain an O on the boundary. This idea takes advantage of this observation and visit the board from O's on the boundary and mark them using another character, say, #. Finally, all the remaining O's are the surrounded regions and should be captured to X. The # regions simply need to be recovered to O.

I adopt the idea from the above link. And I include some minor optimizations. For example, I pass the queue toVisit to the update function to check and update the four neighbors together during the BFS. This turns out to reduce the running time from 28ms to 16ms.

The code is as follows.

 1 class Solution {
 2 public:
 3     void solve(vector<vector<char>>& board) {
 4         if (board.empty()) return;
 5         int m = board.size(), n = board[0].size();
 6         for (int i = 0; i < m; i++) {
 7             if (board[i][0] == 'O')
 8                 mark(board, i, 0);
 9             if (board[i][n - 1] == 'O')
10                 mark(board, i, n - 1);
11         }
12         for (int j = 0; j < n; j++) {
13             if (board[0][j] == 'O')
14                 mark(board, 0, j);
15             if (board[m - 1][j] == 'O')
16                 mark(board, m - 1, j);
17         }
18         capture(board);
19     }
20 private:
21     // Update neighbors
22     void update(vector<vector<char>>& board, queue<pair<int, int>>& toVisit, int r, int c) {
23         int m = board.size(), n = board[0].size();
24         if (r - 1 >= 0 && board[r - 1][c] == 'O') {
25             board[r - 1][c] = '#';
26             toVisit.push(make_pair(r - 1, c));
27         }
28         if (r + 1 < m && board[r + 1][c] == 'O') {
29             board[r + 1][c] = '#';
30             toVisit.push(make_pair(r + 1, c));
31         }
32         if (c - 1 >= 0 && board[r][c - 1] == 'O') {
33             board[r][c - 1] = '#';
34             toVisit.push(make_pair(r, c - 1));
35         }
36         if (c + 1 < n && board[r][c + 1] == 'O') {
37             board[r][c + 1] = '#';
38             toVisit.push(make_pair(r, c + 1));
39         }
40     }
41     // Mark non-surrounded regions
42     void mark(vector<vector<char>>& board, int r, int c) {
43         queue<pair<int, int> > toVisit;
44         toVisit.push(make_pair(r, c));
45         board[r][c] = '#';
46         while (!toVisit.empty()) {
47             int num = toVisit.size();
48             for (int i = 0; i < num; i++) {
49                 pair<int, int> cur = toVisit.front();
50                 toVisit.pop();
51                 update(board, toVisit, cur.first, cur.second);
52             }
53         }
54     }
55     // Capture surrounded regions
56     void capture(vector<vector<char>>& board) {
57         int m = board.size(), n = board[0].size();
58         for (int i = 0; i < m; i++) {
59             for (int j = 0; j < n; j++) {
60                 if (board[i][j] == '#')
61                     board[i][j] = 'O';
62                 else board[i][j] = 'X';
63             }
64         }
65     }
66 };

 

目录
相关文章
LeetCode 130. Surrounded Regions
给定一个二维的矩阵,包含 'X' 和 'O'(字母 O)。 找到所有被 'X' 围绕的区域,并将这些区域里所有的 'O' 用 'X' 填充。
58 0
LeetCode 130. Surrounded Regions
|
28天前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
50 6
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 26. 树的子结构
这篇文章提供了解决LeetCode上"剑指Offer 26. 树的子结构"问题的Python代码实现和解析,判断一棵树B是否是另一棵树A的子结构。
43 4
|
2月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
99 2
|
28天前
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
|
2月前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
47 7