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 be: X X X X
X X X X
X X X X
X O X X
solution: 思想是mark 出所有外围的O,里面的不管。
- First scan the four edges of the board, if you meet an 'O', call a recursive mark function to mark that region to something else (for example, '+');
- scan all the board, if you meet an 'O', flip it to 'X';
- scan again the board, if you meet an '+', flip it to 'O';
class Solution {
public:
void BFS(vector<vector<char>> &board, int i , int j){ if(board[i][j] == 'X' || board[i][j] == '#') return;
board[i][j] = '#'; if( i - >= ) BFS(board, i-, j );
if( i + < rows ) BFS(board, i+, j );
if( j - >= ) BFS(board, i, j- );
if( j + < columns ) BFS(board, i, j+ );
}
void solve(vector<vector<char>> &board) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
rows = board.size();
if( rows == ) return ;
columns = board[].size();
if(columns == ) return ; for( int i = ; i < columns - ; i++)
{
BFS(board, ,i);//up
BFS(board, rows - , i );//down
}
for( int i = ; i< rows; i++)
{
BFS(board, i, ); //left
BFS(board, i, columns - ); //right
}
for(int i = ; i< rows; i++)
for(int j = ; j < columns ; j++)
{
if(board[i][j] == '#')
board[i][j] = 'O';
else if (board[i][j] == 'O')
board[i][j] = 'X';
}
}
private :
int rows;
int columns;
};
LeetCode_Surrounded Regions的更多相关文章
- [LeetCode] Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- 验证LeetCode Surrounded Regions 包围区域的DFS方法
在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ ...
- Leetcode: Surrounded regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- LEETCODE —— Surrounded Regions
Total Accepted: 43584 Total Submissions: 284350 Difficulty: Medium Given a 2D board containing 'X' a ...
- [REP]AWS Regions and Availability Zones: the simplest explanation you will ever find around
When it comes to Amazon Web Services, there are two concepts that are extremely important and spanni ...
- Leetcode 130. Surrounded Regions
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- 【leetcode】Surrounded Regions
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- 【leetcode】Surrounded Regions(middle)☆
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- ural 1147. Shaping Regions
1147. Shaping Regions Time limit: 0.5 secondMemory limit: 64 MB N opaque rectangles (1 ≤ N ≤ 1000) o ...
随机推荐
- RazorPad中的ModelProvider
在RazorPad的右侧 我们可以提供模型的结构,Json数据结构体 当提供多个的时候 是Json中的数组 [{ Name: "NI" }, { Name: &qu ...
- 构建WDK驱动出现fatal error U1087: cannot have : and :: dependents for same target
原因:WDK在编译驱动时,是不允许源文件所在的路径(全路径)中包含空格的,如果你包含了空格,就会出现上述错误. 解决方法:把源文件放在一个没有空格的路径下. reference: http://blo ...
- VMware虚拟机相关文件问题
.vmx VM的配置文件 .vmdk VM的虚拟硬盘 .vmsd VM快照和相关联的vmdk的字典文件 .vswap 虚拟交换文件 .nvram 虚拟机的BIOS信息.VM会生成VMX, VMDK, ...
- codility上的问题 (23)Chi 2012
这个题也比较有意思.意思是给定一个数组A,长度为M,里面都是正整数,代表每块地形的高度.现在要测试一种加农炮,给定一个炮弹的高度H, 如果存在最小的I,满足0 < I < M,满足A[I ...
- hdu3308--LCIS 最大连续递增序列长度
这个是动态的,所以要用线段树维护.代码里有注释因为ls敲成lsum,rs敲成rsum查错查了好久.. #include <set> #include <map> #includ ...
- 今天弄了整整一天DCloud
开发一个新闻移动客户端 起先用appcan.apiCloud,最后选择了DCloud: MUI文档看了n久,js代码一上来不很适应编码风格,一堆括号弄得头大: 数据库由爬虫程序将新闻页面数据爬出来: ...
- poj 1286 Necklace of Beads (polya(旋转+翻转)+模板)
Description Beads of red, blue or green colors are connected together into a circular necklace of ...
- Mysql 时间操作
Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度) 1 . 查看当天日期 select current_date(); 2. 查看当天时间 select current_time(); ...
- 使用面向 iOS 的本机插件扩展
本文细致探讨了 Xcode(以 iOS 设备为目标)中的 PhoneGap(也称为 Apache Cordova)应用程序本机插件.如果您刚开始接触 PhoneGap 或者需要回顾 PhoneGap ...
- encodeURI与encodeURIComponent的区别
webservice输出时选择的格式与Content-Type报文头有关 encodeURI与encodeURIComponent的区别:后者会将URI进行编码(包括"://")