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(Database)-Employees Earning More Than Their Managers
			The Employee table holds all employees including their managers. Every employee has an Id, and there ... 
- python爬虫系列之爬取多页gif图像
			python爬取多页gif图像 作者:vpoet mail:vpoet_sir@163.com #coding:utf-8 import urllib import ur ... 
- java POI读取excel 2007/2003
			2003版office excel读取 import java.io.FileNotFoundException; import java.io.IOException; import java.io ... 
- 【错误】:MySql Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'
			错误:MySql Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts' 解决 ... 
- C++ Primer 有感(异常处理)
			1.异常是通过抛出对象而引发的.该对象的类型决定应该激活哪个处理代码.被选中的处理代码是调用链中与该对象类型匹配且离抛出异常位置最近的那个. 2.执行throw的时候,不会执行跟在throw后面的语句 ... 
- 每日一发linux命令
			很多用虚拟机的同学在向/tmp目录下进行解压的时候,会发现之前挂载的此目录空间不足,导致下一步无法进行(我在vmwaretools解压的时候就遇到了这个problem)…… 实际上,/tmp是可以进行 ... 
- kafka中处理超大消息的一些考虑
			Kafka设计的初衷是迅速处理短小的消息,一般10K大小的消息吞吐性能最好(可参见LinkedIn的kafka性能测试).但有时候,我们需要处理更大的消息,比如XML文档或JSON内容,一个消息差不多 ... 
- es3中使用es6/7的字符串扩展
			最近在看阮一峰的<ES6标准入门>,在字符串扩展一节中有提到几个新的扩展,觉得挺有意思,想在ES3里面使用,于是就有下面的兼容性写法. repeat 将一个字符串重复n次 String.p ... 
- synchronized 方式实现监控器中数据成员的同步
			要对监控器中的数据成员进行访问,在考虑到多线程的情况下必须使用同步代码块来改变监控器中数据成员的值: synchronized (mAdapterLocking) { if (pEvery == 0) ... 
- 【搜索引擎Jediael开发笔记3】使用HtmlParser提取网页中的链接
			关于HtmpParser的基本内容请见 HtmlParser基础教程 本文示例用于提取HTML文件中的链接 package org.ljh.search.html; import java.util. ... 
