leetcode_question_130 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
1、把所有边上的不能被X包围的O换成P---O(row*col*min(row,col)),先从走上角开始换,再从右下角开始换,有的时候里面的O其实是和边上的O连通的,但是因为拐弯一次替换不能完成所以就要至少min(row,col)次替换。如果这个弯拐点太大了,这就完蛋了。。。能过Judge Large纯属幸运。。。
2、把里面的被X包围的O换成X---O(row*col)
3、把P换回O---O(row*col)
void solve(vector<vector<char>> &board) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int row = board.size();
if (row == 0) return;
int col = board[0].size();
if (col == 0) return;
//from left top to right down
for (int j = 0; j < col; ++j)
if (board[0][j] == 'O') board[0][j] = 'P';
for (int i = 0; i < row; ++i)
if (board[i][0] == 'O') board[i][0] = 'P';
for (int i = 1; i < row; ++i)
{
for (int j = 1; j < col; ++j)
{
if ((board[i][j] == 'O') && (board[i][j-1] == 'P' || board[i-1][j] == 'P'))
board[i][j] = 'P';
}
}
//from right down to left top
for (int j = 0; j < col; ++j)
if (board[row-1][j] == 'O') board[row-1][j] = 'P';
for (int i = 0; i < row; ++i)
if (board[i][col-1] == 'O') board[i][col-1] = 'P';
for (int i = row-2; i >= 0; --i)
{
for (int j = col-2; j >= 0; --j)
{
if ((board[i][j] == 'O') && (board[i][j+1] == 'P' || board[i+1][j] == 'P'))
board[i][j] = 'P';
}
}
//ensure
int time = row < col ? row : col;
for (int k = 1; k < time; ++k) {
for (int i = 1; i < row; ++i)
{
for (int j = 1; j < col; ++j)
{
if (board[i][j] == 'O') {
if (board[i][j-1] == 'P' || board[i-1][j] == 'P')
board[i][j] = 'P';
if (j+1 < col && board[i][j+1] =='P')
board[i][j] = 'P';
if (i+1 < row && board[i+1][j] =='P')
board[i][j] = 'P';
}
}
} }
//change O to X
for (int i = 1; i < row; ++i)
{
for (int j = 1; j < col; ++j)
{
if (board[i][j] == 'O')
board[i][j] = 'X';
}
}
//change P to O
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
if (board[i][j] == 'P')
board[i][j] = 'O';
}
}
}
这种方法的缺憾主要在第一步,如果优化的话,就是从矩阵的边界开始找O,只要找到O就从这个O开始BFS搜索把其相邻的O换成P直到相邻的没有O为止。这样就不用这么多次数的O(n^2)了吧。
void changeotop(vector<vector<char>> &board, int i, int j)
{
board[i][j] = 'P';
int row = board.size();
int col = board[0].size();
if(i>0 && board[i-1][j] == 'O')
changeotop(board, i-1, j);
if(j>0 && board[i][j-1] == 'O')
changeotop(board, i, j-1);
if(i+1<row && board[i+1][j] == 'O')
changeotop(board, i+1, j);
if(j+1<col && board[i][j+1] == 'O')
changeotop(board, i, j+1);
} void solve(vector<vector<char>> &board) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int row = board.size();
if(row == 0) return;
int col = board[0].size();
if(col == 0) return; for(int j = 0; j < col; ++j)
if(board[0][j] == 'O')
changeotop(board,0,j);
for(int i = 0; i < row; ++i)
if(board[i][0] == 'O')
changeotop(board,i,0);
for(int j = 0; j < col; ++j)
if(board[row-1][j] == 'O')
changeotop(board,row-1,j);
for(int i = 0; i < row; ++i)
if(board[i][col-1] == 'O')
changeotop(board,i,col);
//change O to X
for(int i = 1; i < row; ++i)
{
for(int j = 1; j < col; ++j)
{
if(board[i][j] == 'O')
board[i][j] = 'X';
}
}
//change P to O
for(int i = 0; i < row; ++i)
{
for(int j = 0; j < col; ++j)
{
if(board[i][j] == 'P')
board[i][j] = 'O';
}
}
}
leetcode_question_130 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
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- [LintCode] Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- 22. Surrounded Regions
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- Surrounded Regions
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- [Swift]LeetCode130. 被围绕的区域 | Surrounded Regions
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions
两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...
- 130. Surrounded Regions(M)
130.Add to List 130. Surrounded Regions Given a 2D board containing 'X' and 'O' (the letter O), capt ...
随机推荐
- 多线程并发 synchronized对象锁的控制与优化
本文针对用户取款时多线程并发情境,进行相关多线程控制与优化的描述. 首先建立用户类UserTest.业务操作类SynchronizedTest.数据存取类DataStore,多线程测试类MultiTh ...
- hdu 4649 Professor Tian 多校联合训练的题
这题起初没读懂题意,悲剧啊,然后看了题解写完就AC了 题意是给一个N,然后给N+1个整数 接着给N个操作符(只有三种操作 即 或 ,与 ,和异或 | & ^ )这样依次把操作符插入整 ...
- 为什么学微信小程序开发
微信小程序是什么? 触手可得,不用安装,不体验过自己是想不到的 ---张小龙 小程序是一种不需要下载安装即可使用的应用,它实现了应用“触手可及”的梦想,用户扫一扫或者搜 一下即可打开应用.也体现了“用 ...
- 自定义视图控制器切换(iOS)
在iOS开发过程中,通常我们会使用UINavigationController,UITabbarController等苹果提供的视图控制器来切换我们的视图.在iOS5之前,如果要自定义容器视图控制器很 ...
- 文件上传利器SWFUpload使用指南(转)
http://www.cnblogs.com/2050/archive/2012/08/29/2662932.html 文件上传利器SWFUpload使用指南 SWFUpload是一个flash和js ...
- centos7命令行与图形界面启动模式修改
1.命令启动 systemctl set-default multi-user.target 2.图形界面模式 systemctl set-default graphical.target
- ASP.net+SQL server2008简单的数据库增删改查 VS2012
工具:VS2012 数据库:SQL server 简单说明:根据老师上课给的代码,进行了简单的改正适用于VS2012环境,包括注册.登录.查询.修改.删除功能,多数参考了网上的代码 百度云源代码连接t ...
- SQL-LINQ-Lambda 语法对照
SQL LINQ Lambda SELECT *FROM Employees from e in Employees select e Employees .Select (e => e) ...
- 对WEB标准以及W3C的理解与认识 - 提高网页加载速度
在写代码的时候应该注意: 1.标签闭合 2.标签小写 3.不能随意嵌套 提高被搜索引擎搜到几率: mate中的name变量[其中keywords和description尤其重要] Meta name= ...
- css基础之 font的简写规则 以及 自定义 CSS3 @font-face详细用法
Part 1 font简写 CSS的命名规则是用英文字母 数字 和下划线(一般用小写)来命名.简写css font的好处有三:一是写起来方便(就像键盘快捷键):二是简化代码:三是帮助你熟悉和深刻理解c ...