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

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

After capture all regions surrounded by 'X', the board should be:

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

LeetCode上的原题,请参见我之前的博客Surrounded Regions

解法一:

class Solution {
public:
/**
* @param board a 2D board containing 'X' and 'O'
* @return void
*/
void surroundedRegions(vector<vector<char>>& board) {
if (board.empty() || board[].empty()) return;
int m = board.size(), n = board[].size();
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
if (i == || i == m - || j == || j == n - ) {
if (board[i][j] == 'O') dfs(board, i , j);
}
}
}
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
if (board[i][j] == 'O') board[i][j] = 'X';
if (board[i][j] == '$') board[i][j] = 'O';
}
}
}
void dfs(vector<vector<char>> &board, int x, int y) {
int m = board.size(), n = board[].size();
vector<vector<int>> dir{{,-},{-,},{,},{,}};
board[x][y] = '$';
for (int i = ; i < dir.size(); ++i) {
int dx = x + dir[i][], dy = y + dir[i][];
if (dx >= && dx < m && dy >= && dy < n && board[dx][dy] == 'O') {
dfs(board, dx, dy);
}
}
}
};

解法二:

class Solution {
public:
/**
* @param board a 2D board containing 'X' and 'O'
* @return void
*/
void surroundedRegions(vector<vector<char>>& board) {
if (board.empty() || board[].empty()) return;
int m = board.size(), n = board[].size();
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
if (i == || i == m - || j == || j == n - ) {
if (board[i][j] == 'O') dfs(board, i , j);
}
}
}
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
if (board[i][j] == 'O') board[i][j] = 'X';
if (board[i][j] == '$') board[i][j] = 'O';
}
}
}
void dfs(vector<vector<char>> &board, int i, int j) {
int m = board.size(), n = board[].size();
if (i < || i >= m || j < || j >= n || board[i][j] != 'O') return;
board[i][j] = '$';
dfs(board, i + , j);
dfs(board, i - , j);
dfs(board, i, j + );
dfs(board, i, j - );
}
};

[LintCode] 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] 130. Surrounded Regions 包围区域

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

  4. Surrounded Regions 包围区域——dfs

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

  5. 130. Surrounded Regions(周围区域问题 广度优先)(代码未完成!!)

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

  6. Leetcode之深度优先搜索(DFS)专题-130. 被围绕的区域(Surrounded Regions)

    Leetcode之深度优先搜索(DFS)专题-130. 被围绕的区域(Surrounded Regions) 深度优先搜索的解题详细介绍,点击 给定一个二维的矩阵,包含 'X' 和 'O'(字母 O) ...

  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: Surrounded regions

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

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

随机推荐

  1. 配置ogg异构oracle-mysql(1)基础环境配置

    一.环境描述: 192.168.0.164 ( Oracle ) —> 192.168.0.165 (Mysql ) 版本: 操作系统:redhat5.8 Oracle:  11.2.0.3 M ...

  2. Java中synchronized详解

    synchronized 原则: 尽量避免无谓的同步控制,同步需要系统开销,可能造成死锁 尽量减少锁的粒度 同步方法 public synchronized void printVal(int v) ...

  3. 如果我可以重新学习iOS开发(转)

    在过去的几个月里,我一直在学习用Objective-C编写iOS app,最后我开始理清思绪.这比我想象中要难很多,也花了太长时间. 我经常遇到困难.感到沮丧,修复bug比实际写代码要花太多时间.但是 ...

  4. unable to access android sdk add-on list

    在bin\properties里添加disable.android.first.run=true

  5. Pushlet浏览器长连接通讯

    原文链接:http://cuisuqiang.iteye.com/blog/1416771 Pushlet(一种comet 架构的实现)是基于Servlet 机制,数据从server端的Java 对象 ...

  6. 【POI xls】解析xls遇到的问题

    问题1:Package should contain a content type part org.apache.poi.POIXMLException: org.apache.poi.openxm ...

  7. 系统剖析Android中的内存泄漏

    [转发]作为Android开发人员,我们或多或少都听说过内存泄漏.那么何为内存泄漏,Android中的内存泄漏又是什么样子的呢,本文将简单概括的进行一些总结. 关于内存泄露的定义,我可以理解成这样 没 ...

  8. hdu 4000Fruit Ninja 树状数组

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  9. 并查集 + 线段树 LA 4730 Kingdom

    题目传送门 题意:训练指南P248 分析:第一个操作可以用并查集实现,保存某集合的最小高度和最大高度以及城市个数.运用线段树成端更新来统计一个区间高度的个数,此时高度需要离散化.这题两种数据结构一起使 ...

  10. PLSQL看oracle中汉字显示乱码

    首先执行语句 select * from V$NLS_PARAMETERS  查看第一行中PARAMETER项中为NLS_LANGUAGE 对应的VALUE项中是否为SIMPLIFIED CHINES ...