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

核心思想:只有边界上'O'的位置组成的片区不会被'X'包围。

因此先对边界上的'O'遍历之后暂存为'*'。

非'*'的'O'即被'X'包围了。

解法一:DFS遍历

struct POS
{
int x;
int y;
POS(int newx, int newy): x(newx), y(newy) {}
}; class Solution {
public:
void solve(vector<vector<char>> &board) {
if(board.empty() || board[].empty())
return;
int m = board.size();
int n = board[].size();
for(int i = ; i < m; i ++)
{
for(int j = ; j < n; j ++)
{
if(board[i][j] == 'O')
{
if(i == || i == m- || j == || j == n-)
{// remain 'O' on the boundry
dfs(board, i, j, m, n);
}
}
}
}
for(int i = ; i < m; i ++)
{
for(int j = ; j < n; j ++)
{
if(board[i][j] == 'O')
board[i][j] = 'X';
else if(board[i][j] == '*')
board[i][j] = 'O';
}
}
}
void dfs(vector<vector<char>> &board, int i, int j, int m, int n)
{
stack<POS*> stk;
POS* pos = new POS(i, j);
stk.push(pos);
board[i][j] = '*';
while(!stk.empty())
{
POS* top = stk.top();
if(top->x > && board[top->x-][top->y] == 'O')
{
POS* up = new POS(top->x-, top->y);
stk.push(up);
board[up->x][up->y] = '*';
continue;
}
if(top->x < m- && board[top->x+][top->y] == 'O')
{
POS* down = new POS(top->x+, top->y);
stk.push(down);
board[down->x][down->y] = '*';
continue;
}
if(top->y > && board[top->x][top->y-] == 'O')
{
POS* left = new POS(top->x, top->y-);
stk.push(left);
board[left->x][left->y] = '*';
continue;
}
if(top->y < n- && board[top->x][top->y+] == 'O')
{
POS* right = new POS(top->x, top->y+);
stk.push(right);
board[right->x][right->y] = '*';
continue;
}
stk.pop();
}
}
};

解法二:BFS遍历

struct POS
{
int x;
int y;
POS(int newx, int newy): x(newx), y(newy) {}
}; class Solution {
public:
void solve(vector<vector<char>> &board) {
if(board.empty() || board[].empty())
return;
int m = board.size();
int n = board[].size();
for(int i = ; i < m; i ++)
{
for(int j = ; j < n; j ++)
{
if(board[i][j] == 'O')
{
if(i == || i == m- || j == || j == n-)
{// remain 'O' on the boundry
bfs(board, i, j, m, n);
}
}
}
}
for(int i = ; i < m; i ++)
{
for(int j = ; j < n; j ++)
{
if(board[i][j] == 'O')
board[i][j] = 'X';
else if(board[i][j] == '*')
board[i][j] = 'O';
}
}
}
void bfs(vector<vector<char>> &board, int i, int j, int m, int n)
{
queue<POS*> q;
board[i][j] = '*';
POS* pos = new POS(i, j);
q.push(pos);
while(!q.empty())
{
POS* front = q.front();
q.pop();
if(front->x > && board[front->x-][front->y] == 'O')
{
POS* up = new POS(front->x-, front->y);
q.push(up);
board[up->x][up->y] = '*';
}
if(front->x < m- && board[front->x+][front->y] == 'O')
{
POS* down = new POS(front->x+, front->y);
q.push(down);
board[down->x][down->y] = '*';
}
if(front->y > && board[front->x][front->y-] == 'O')
{
POS* left = new POS(front->x, front->y-);
q.push(left);
board[left->x][left->y] = '*';
}
if(front->y < n- && board[front->x][front->y+] == 'O')
{
POS* right = new POS(front->x, front->y+);
q.push(right);
board[right->x][right->y] = '*';
}
}
}
};

这边再给一种递归实现的dfs供参考,简洁很多,但是在leetcode中由于栈溢出会显示Runtime Error

void dfs(vector<vector<char>> &board, int i, int j, int m, int n)
{
board[i][j] = '*';
if(i > && board[i-][j] == 'O') // up
dfs(board, i-, j, m, n);
if(i < m- && board[i+][j] == 'O') // down
dfs(board, i+, j, m, n);
if(j > && board[i][j-] == 'O') // left
dfs(board, i, j-, m, n);
if(j < n- && board[i][j+] == 'O') // right
dfs(board, i, j+, m, n);
}

【LeetCode】130. Surrounded Regions (2 solutions)的更多相关文章

  1. 【一天一道LeetCode】#130. Surrounded Regions

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  2. 【LeetCode】75. Sort Colors (3 solutions)

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  3. 【LeetCode】90. Subsets II (2 solutions)

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

  4. 【LeetCode】44. Wildcard Matching (2 solutions)

    Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...

  5. 【LeetCode】338. Counting Bits (2 solutions)

    Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num  ...

  6. 【LeetCode】258. Add Digits (2 solutions)

    Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...

  7. 【LeetCode】242. Valid Anagram (2 solutions)

    Valid Anagram Given two strings s and t, write a function to determine if t is an anagram of s. For ...

  8. 【LeetCode】28. Implement strStr() (2 solutions)

    Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haysta ...

  9. 【LeetCode】217. Contains Duplicate (2 solutions)

    Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...

随机推荐

  1. Python3.6学习笔记(四)

    错误.调试和测试 程序运行中,可能会遇到BUG.用户输入异常数据以及其它环境的异常,这些都需要程序猿进行处理.Python提供了一套内置的异常处理机制,供程序猿使用,同时PDB提供了调试代码的功能,除 ...

  2. 安全开发 | 如何让Django框架中的CSRF_Token的值每次请求都不一样

    前言 用过Django 进行开发的同学都知道,Django框架天然支持对CSRF攻击的防护,因为其内置了一个名为CsrfViewMiddleware的中间件,其基于Cookie方式的防护原理,相比基于 ...

  3. 第十三章 springboot + lombok

    lombok作用:消除模板代码. getter.setter.构造器.toString().equals() 便捷的生成比较复杂的代码,例如一个POJO要转化成构建器模式的形式,只需要一个注解. 注意 ...

  4. Java中正则匹配性能测试

    工作中经常会用到在文本中每行检索某种pattern,刚才测试了三种方式,发现实际性能和预想的有区别 方式1: 直接字符串的matches方法,[string.matches("\\d+&qu ...

  5. iframe之onload事件小记

    项目上做了一个具有wizard(向导)功能的菜单导航页面,子页面的引入通过主页面上iframe的src属性切换实现.为了有个良好的交互体验,每次更新iframe的src时,主页面上都显示一个模态的lo ...

  6. Cognos值提示设置小技巧

    针对值提示问题做一个小的总结: 1:显示类问题 如上图,如何让”英文参数名"和"分割线----"不显示,或者说指定中文显示值呢 (1):让”英文参数名"和&qu ...

  7. 如何开机就启动node.js程序

      npm install -g qckwinsvc 定位到安装目录,node_modules/.bin/ 运行如下命令: > qckwinsvc prompt: Service name: [ ...

  8. 【leetcode 桶排序】Maximum Gap

    1.题目 Given an unsorted array, find the maximum difference between the successive elements in its sor ...

  9. Java从零开始学零(Java简介)

    一.Java 简介 Java是由Sun Microsystems公司于1995年5月推出的Java面向对象程序设计语言和Java平台的总称.由James Gosling和同事们共同研发,并在1995年 ...

  10. [转]自定义Drawable实现灵动的红鲤鱼动画(下篇)

      小鱼儿 上篇文章自定义Drawable实现灵动的红鲤鱼动画(上篇)我们绘制了可以摆动身体的小鱼,本篇就分享一下如何让小鱼游到手指点击的位置.用到的主要技术如下: 1).三阶贝塞尔曲线 2).Pat ...