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
Method1: O(row*col*min(row,col))

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的更多相关文章

  1. [LeetCode] Surrounded Regions 包围区域

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  2. 验证LeetCode Surrounded Regions 包围区域的DFS方法

    在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ ...

  3. 【leetcode】Surrounded Regions

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  4. [LintCode] Surrounded Regions 包围区域

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  5. 22. Surrounded Regions

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  6. Surrounded Regions

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  7. [Swift]LeetCode130. 被围绕的区域 | Surrounded Regions

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  8. 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 用了第一种方式, ...

  9. 130. Surrounded Regions(M)

    130.Add to List 130. Surrounded Regions Given a 2D board containing 'X' and 'O' (the letter O), capt ...

随机推荐

  1. 安全运维之:Linux系统账户和登录安全

    一.合理使用Shell历史命令记录功能 在Linux下可通过history命令查看用户所有的历史操作记录,同时shell命令操作记录默认保存在用户目录下 的.bash_history文件中,通过这个文 ...

  2. [转]ActiveMQ 即时通讯服务 浅析

    一. 概述与介绍 ActiveMQ 是Apache出品,最流行的.功能强大的即时通讯和集成模式的开源服务器.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provide ...

  3. Android专项面试训练题(一)

    1.下面不可以退出Activity的是?(D) A.finish() B.抛异常强制退出 C.System.exit(0) D.onStop() 解析: A, finish() 方法就是退出activ ...

  4. 【剑指Offer学习】【面试题60:把二叉树打印出多行】

    题目:从上到下按层打印二叉树,同一层的结点按从左到右的顺序打印,每一层打印一行. 解题思路 用一个队列来保存将要打印的结点.为了把二叉树的每一行单独打印到一行里,我们须要两个变量:一个变量表示在当前的 ...

  5. 有一种acm题目叫做,奇葩!

    本文全然没有技术含量,纯粹是娱乐. 我事实上想写点东西.可是近期好像做计算几何做得太多了,一种想说说不出东西的感觉,唯有写一下一些奇葩的题目了. HDU3337:Guess the number pi ...

  6. Android应用程序组件Content Provider在应用程序之间共享数据的原理分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6967204 在Android系统中,不同的应用 ...

  7. Gulp及组件安装构建

    Gulp 是一款基于任务的设计模式的自动化工具,通过插件的配合解决全套前端解决方案,如静态页面压缩.图片压缩.JS合并.SASS同步编译并压缩CSS.服务器控制客户端同步刷新. Gulp安装 全局安装 ...

  8. android 数据存储之SharePreference 的几种方式

    1. 常见的 getSharedPreferences(String filename,mode) 指定sp文件的名称,生成的文件名为 filename.xml 2.getPreferences(mo ...

  9. PHP学习笔记十一【数组】

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/h ...

  10. IntelliJ IDEA 14使用笔记

    由eclipse到IDEA IDEA与eclipse的区别: IDEA的project对应eclipse的workspace: IDEA的module对应eclipse的project的. 所以要想在 ...