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
class Solution { public: void solve(vector<vector<char>> &board) { if(board.size() == 0) return; int m = board.size(), n = board[0].size(); for(int i = 0; i < m; ++i) { bfs(i, 0, board); bfs(i, n-1, board); } for(int j = 1; j < n-1; ++j) { bfs(0, j, board); bfs(m-1, j, board); } for(int i = 0; i < n; ++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 bfs(int i, int j, vector<vector<char> > &board) { queue<int> q; visit(i, j, q, board); int n = board[0].size(); while(!q.empty()) { int f = q.front(); q.pop(); int r = f / n; int l = f % n; visit(r-1, l, q, board); visit(r+1, l, q, board); visit(r, l-1, q, board); visit(r, l+1, q, board); } } void visit(int i, int j, queue<int> &q, vector<vector<char> > &board) { int m = board.size(), n = board[0].size(); if(i < 0 || i >= m || j < 0 || j >= n || board[i][j] != 'O') return; board[i][j] = '+'; q.push(i * n + j); } };
欢迎关注微信公众号——计算机视觉:
相关推荐
Surrounded Regions 总结 深度优先搜索 Additive Number Palindrome Partitioning Unique Paths Unique Paths II N-Queens N-Queens II Restore IP Addresses Combination Sum Combination Sum II Combination Sum ...
leetcode做过的LeetCode的题目记录一下。对一些比较经典的题型进行分类总结。数据结构 数组 字符串 队列 链表 双指针 栈 堆 树 二叉搜索... Surrounded Regions【2020-01-09】114. Flatten Binary Tree to Linked List
Surrounded - 创建对象的封装系统并专注于他们的交互
rsa加密解密算法的JAVA代码实现,可以参考,
包围Surrounded 类允许开发人员从较长的字符串中提取部分文本。 提取的部分基于特定字符周围的单词数。 输出类似于 Google 返回的结果:以特定单词为中心的文本片段。 这个类有一个公共方法: public static String ...
Nonlinear total variation based noise removal algorithms 非线性TV去噪的程序
自动检测遥感影像上的阴影区域,并将阴影区域去掉。
easy xml for delphi, great product v4
将您的问题名称用作类,并使用Surrounded::Context对其进行扩展它可能看起来像这样: class Employment extend Surrounded :: Context role :boss role :employeeend 在您的应用程序中,您将使用对象初始化此类以...
气压传感器FBM320基于C语言的参考代码
Python入门教程每天一小时,100天轻松入门,利用闲暇时间行动起来
GD32F130C8T6移植FreeRTOS
立方晶系各向异性地层井孔声多极导波频散分析,张丽,王克协,本文用摄动方法研究井内充液,井外为立方晶系各向异性地层声多极导波频散问题。这种地层做井的问题时无对称轴,井内外声场不能严
concise](https://leetcode.com/problems/surrounded-regions/discuss/453448/java-concise-bfs) 137这种 deep copy, HashMap通吃不要太爽 Day 3 - Dec 17 12:28 199 Finished. 佛了 做完第一题就不想做了 12:54...
Surrounded_Regions 由四周‘O’开始向内检索,利用第三个字符对可以变换的‘O’进行暂存 Word_Lader BFS,避免DFS记录以遍历的节点错误,且保证优先找到最短的路径, 按照图的方法判断任意两个节点是否差一个字母,...
SP-6939 : Code analysis no longer reports non-scalar false positives on subqueries surrounded by ANY, ALL and SOME logical operators (EI003). SP-6957 : SELECT NULL produces a scalar value, so it ...
或背景上出现阴影。Subject must be surrounded by a plain, light-coloredbackground with no
shell cgi list directory contents Surrounded by a Tags. shell-cgi输出被a标签包围的目录文件
Machine description operations are to be surrounded by grab and release calls. The mdesc_handle returned the grab is the first argument to all of the operational calls that work on mdescs.
myself sitting in an oak paneled room surrounded by books and attentive students listening to me pontificating on the latest criticism of 19th Century novels. It didn’t take me long to realize though...