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

思路:从四条边上的'0'入手,与其邻接的'O'都不被改,记为'D'。逆向思维!从不被改的'O'入手,而非寻找被改得'O'

class Solution {
public:
void dfs(int x, int y){
if(x< || x>=m || y< || y>=n || board[x][y]!='O') return;
board[x][y]='D'; //图的四个方向遍历
dfs(x-,y);
dfs(x+,y);
dfs(x,y-);
dfs(x,y+);
} void solve(vector<vector<char>> &board){
if (board.empty()) return;
this->board = board;
m=board.size();
n=board[].size();
if(n<= || m<=) return; for(int j=;j<n;j++){
dfs(,j);
dfs(m-,j);
} for(int i=;i<m;i++){
dfs(i,);
dfs(i,n-);
} for(int i=;i<m;i++)
for(int j=;j<n;j++){
if(this->board[i][j]=='O') this->board[i][j]='X';
else if(this->board[i][j]=='D') this->board[i][j]='O';
}
board = this->board;
}
private:
int m,n;
vector<vector<char>> board;
};

130. Surrounded Regions (Graph; DFS)的更多相关文章

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

  2. 130. Surrounded Regions(M)

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

  3. [LeetCode] 130. Surrounded Regions 包围区域

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

  4. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

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

  5. 【LeetCode】130. Surrounded Regions (2 solutions)

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

  6. Leetcode 130 Surrounded Regions DFS

    将内部的O点变成X input X X X XX O O X X X O XX O X X output X X X XX X X XX X X XX O X X DFS的基本框架是 void dfs ...

  7. 130. Surrounded Regions

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

  8. leetcode 130 Surrounded Regions(BFS)

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

  9. Leetcode 130. Surrounded Regions

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

随机推荐

  1. 用于调试的printf函数和自定义log函数

    1. 用宏定义调试用的DPRINT #define DEBUG_ENABLE #ifdef DEBUG_ENABLE #define DPRINT(fmt, args...) fprintf(stde ...

  2. New Concept English Two 21 55

    $课文53  触电的蛇 544. At last firemen have put out a big forest fire in California. 消防队员们终于扑灭了加利福尼亚的一场森林大 ...

  3. C++ int转string(stringstream可转更多类型)

    一.使用atoi 说明: itoa(   int   value,   char   *string,   int   radix   );      第一个参数:你要转化的int;      第二个 ...

  4. PAT 1012 数字分类 C语言

    给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字: A1 = 能被5整除的数字中所有偶数的和: A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4...: ...

  5. Django中MySQL读写分离技术

    最近需要用到Django的MySQL读写分离技术,查了一些资料,把方法整理了下来. 在Django里实现对MySQL的读写分离,实际上就是将不同的读写请求按一定的规则路由到不同的数据库上(可以是不同类 ...

  6. 冒泡排序算法-Python实现

    #-*- coding: UTF-8 -*- import numpy as np def BubbleSort(a): for i in xrange(0, a.size): for j in xr ...

  7. XSS获取cookie并利用

    获取cookie利用代码cookie.asp <html> <title>xx</title> <body> <%testfile = Serve ...

  8. C++ 函数特性_函数重载

    函数重载 在相同作用域内 用同一函数名定义的多个函数: 参数个数和参数类型不同 int getMax(int x,int y,int z) // 函数名相同都是 getMax() { // to do ...

  9. thinkPHP使用函数时字符串中不能含有管道符”|“,否则报错;

    如 {$data.name|str_repeat="|",###}报错!!!

  10. oracle to_date函数和mysql DATE_FORMAT函数用法

    一.在Oracle中,当想把字符串为‘2011-09-20 08:30:45’的格式转化为日期格式,我们可以使用oracle提供的to_date函数. sql语句为: SELECT to_date(' ...