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. Mac 删除Openfire

    首先,确保你已经关掉了openfire 打开终端 (在应用程序-->实用工具-->) 输入以下命令 sudo rm -rf /Library/PreferencePanes/Openfir ...

  2. Spring-Junit4

      Spring整合Junit4测试Service 1. 加入依赖包 使用Spring的测试框架需要加入以下依赖包: JUnit 4 (官方下载:https://github.com/KentBeck ...

  3. 水池数目(DFS)

    水池数目 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 南阳理工学院校园里有一些小河和一些湖泊,现在,我们把它们通一看成水池,假设有一张我们学校的某处的地图,这个地 ...

  4. ChromiumFX中js调用C#方法

    server端代码: ChromiumWebBrowser wb; wb.AddGlobalJSFunction("CfxHelloWorld").Execute += CfxHe ...

  5. 矩阵经典题目六:poj 3070 Fibonacci

    http://poj.org/problem?id=3070 按已构造好的矩阵,那么该矩阵的n次方的右上角的数便是f[n]. #include <stdio.h> #include < ...

  6. JAVA HashMap与HashTable 区别

    HashTable和HashMap区别 第一,继承不同. public class Hashtable extends Dictionary implements Mappublic class Ha ...

  7. 淘宝对接API

    最近在忙与淘宝做对接的工作,总体感觉淘宝的api文档做的还不错,不仅有沙箱测试环境,而且对于每一个api都可以通过api测试工具生成想要的代码,你完全可以先在测试工具中测试之后再进行代码的编写,这样就 ...

  8. android——背景颜色渐变(梯度变化)

    首先在drawable文件夹下面新建一个xml文件,起名为bgcolor.xml. 代码如下: <?xml version="1.0" encoding="utf- ...

  9. resolv.conf 是什么

    From Wikipedia, the free encyclopedia This article does not cite any references or sources. Please h ...

  10. 疯狂Android第一章:Android环境配置以及基本概念

    第一章 无关痒痛:Android Studio安装,配置,基本功能介绍! 重点内容:Android应用基本结构分析. 基础概念部分(只需知道作用,原理后见代码): Activity:安卓系统中负责与用 ...