将内部的O点变成X

input

X X X X
X O O X
X X O X
X O X X

output

X X X X
X X X X
X X X X
X O X X

DFS的基本框架是

 void dfs(int now,int d){
if(终止条件) {
做相应的操作;
return;
}
for(遍历所有now点的相邻点next){
if(!visit[next]) {
访问每个没有访问过的点;
做相应的操作;
dfs(next, d + );
}
}
}

DFS图所有边上的点,将边上的O以及其联通域变为T,然后将内部的O变为X,外部的T变为O,本质是种子填充算法。

此题对于is_in这个函数要注意, 不要用(x < m) && (x >= 0) && (y < n) && (y >= 0) 会堆栈溢出!!原因是会出现边上的点全是O。。。

 class Solution {
public: int m, n;
bool is_in(int x, int y)
{
return (x < m-) && (x >= ) && (y < n-) && (y >= );
} void dfs(std::vector<std::vector<char>> &board,int x,int y)
{
//if (!(is_in(x, y) && board[x][y] == 'O')) return;
//printf("%d %d\n", x, y);
board[x][y] = 'T';
int dir[][] = { { , }, { -, }, { , }, { , - } };
for (int i = ; i < ; ++i){
int tx = x + dir[i][];
int ty = y + dir[i][];
if (is_in(tx, ty) && board[tx][ty] == 'O')
{
dfs(board, tx, ty);
}
}
} void change(std::vector<std::vector<char>> &board)
{
for (int i = ; i < m;++i){
for (int j = ; j < n;++j){
if (board[i][j] == 'T') board[i][j] = 'O';
else if (board[i][j] == 'O') board[i][j] = 'X';
else;
}
}
} void solve(std::vector<std::vector<char>> &board)
{
m = board.size();
if (m == ) return;
n = board[].size();
if (n == ) return;
for (int i = ; i < m;++i){
if (board[i][] == 'O') dfs(board, i, );
if (board[i][n - ] == 'O') dfs(board, i, n-);
}
for (int i = ; i < n; ++i){
if (board[][i] == 'O') dfs(board, , i);
if (board[m-][i] == 'O') dfs(board, m-, i);
}
change(board);
}
};

Leetcode 130 Surrounded Regions DFS的更多相关文章

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

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

  2. leetcode 130 Surrounded Regions(BFS)

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

  3. Leetcode 130. Surrounded Regions

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

  4. Java for LeetCode 130 Surrounded Regions

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

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

  6. 130. Surrounded Regions(M)

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

  7. [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 ...

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

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

  9. 【leetcode】Surrounded Regions

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

随机推荐

  1. linux各种命令

    命令  [选项]  [参数] read  -t  30    -p   "Please input a num: "   num 功能:将键盘输入的数赋予num ps   aux  ...

  2. Java核心知识点学习----多线程 倒计时记数器CountDownLatch和数据交换的Exchanger

    本文将要介绍的内容都是Java5中的新特性,一个是倒计时记数器---CountDownLatch,另一个是用于线程间数据交换的Exchanger. 一.CountDownLatch 1.什么是Coun ...

  3. mysql查询数据返回touple改为字典的方法

    conn = MySQLdb.connect(host='ip',user='root',passwd='123456',db="dbname",charset="utf ...

  4. Java集合之TreeMap

    Map的单元是对键值对的处理,之前分析过的两种Map,HashMap和LinkedHashMap都是用哈希值去寻找我们想要的键值对,优点是由O(1)的查找速度. 那如果我们在一个对查找性能要求不那么高 ...

  5. document.referrer 特性

    最近需要用到document.referrer,但是在测试的时候,总是获取为空,百思不得其解. 于是发动百度,看了大量的文章没有一个说到点子上是为什么,后来偶然看到document.referrer ...

  6. 访问https链接方法

    <a id='___szfw_logo___' href='https://credit.szfw.org/CX20160808028375110138.html' target='_blank ...

  7. tesseract-ocr 出现 错误 Please make sure the TESSDATA_PREFIX environment variable is set to the parent d irectory of your "tessdata" directory.解决方案

    简单就是说把tessdata拷贝到exe的所在目录,或者设置TESSDATA_PREFIX环境变量

  8. Valid Sudoku leetcode

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  9. 第43讲:Scala中类型变量Bounds代码实战及其在Spark中的应用源码解析

    今天学习了scala的界定,先来看看下面这段代码 //class Pair[T] (val first : T,val second : T)class Pair[T <: Comparable ...

  10. Hibernate映射问题之OneToOne【自己整理】

    首先贴上一个MkYong的例子 stock.java package com.mkyong.stock; import javax.persistence.CascadeType; import ja ...