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 ...
随机推荐
- LeeCode-Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- linux多线程socket编程一些心得
http://hi.baidu.com/netpet/blog/item/2cc79216d9012b54f2de32b9.html 前段时间将新的web模型办到linux上来,用epoll代替了IO ...
- 谱聚类--SpectralClustering
谱聚类通常会先对两两样本间求相似度. 然后依据相似度矩阵求出拉普拉斯矩阵,然后将每一个样本映射到拉普拉斯矩阵特诊向量中,最后使用k-means聚类. scikit-learn开源包中已经有现成的接口能 ...
- docker 实战---使用oracle xe作为开发数据库(六)
oracle作为oltp的大佬,非常多行业应用都会用到它.那么在开发的过程中就不可避免的要使用oracle数据库,oracle数据库的版本号有好多,当中express版本号是免费的开发版.它的主要限制 ...
- Java 强引用,软引用,弱引用
1.强引用 public void handleMessage(Message msg) { case FAIL: GoplayException mException = new GoplayExc ...
- Java获取客户端真实IP地址的两种方法
在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实I ...
- ISO9126 质量模型
功能性 适合性:当软件在指定条件下使用,其满足明确和隐含要求功能的能力. 准确性:软件提供给用户功能的精确度是否符合目标. 互操作性:软件与其它系统进行交互的能力. 安全性:软件保护信息和数据的安全能 ...
- DevExpress中SearchLookUpEdit用法总结
在前一个项目中用到了DevExpress,需要搜索某一个字段,来拉取出对应的相关信息,比来比去,发现SearchLookUpEdit的用户体验更好,但自己是个不折不扣的C#和DevExpress的初学 ...
- ASP.NET通用权限验证组件实现
沙发(SF)通用权限验证组件 开篇 本篇介绍通用权限验证的实现代码思路,总共分为导入参数.解析XML.根据XML配置进行处理.返回结果. 代码架构图 1. 类介绍 1.SFWebPermissio ...
- React-Native 获取node.js提供的接口
一个简单的React-Native 获取node.js提供的接口的实现 一.node.js var http = require("http"); var url = requir ...