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
class Solution {
struct position
{
int x, y;
position(int a, int b): x(a), y(b) {}
};
public:
void solve(vector<vector<char>>& board) {
int row = board.size();
if(row <= )
return;
int col = board[].size();
if(col <= )
return;
queue<position> q;
int i, j;
for(i = ; i < col; i++)
{
if('O' == board[][i])
q.push(position(, i));
if('O' == board[row-][i])
q.push(position(row-, i));
}
for(i = ; i < row; i++)
{
if('O' == board[i][])
q.push(position(i, ));
if('O' == board[i][col-])
q.push(position(i, col-));
}
while(!q.empty())
{
position p = q.front();
q.pop();
board[p.x][p.y] = 'N';
if(p.x > && 'O' == board[(p.x)-][p.y])
q.push(position((p.x)-, p.y));
if(p.x < row- && 'O' == board[(p.x)+][p.y])
q.push(position((p.x)+, p.y));
if(p.y > && 'O' == board[p.x][(p.y)-])
q.push(position(p.x, (p.y)-));
if(p.y < col- && 'O' == board[p.x][(p.y)+])
q.push(position(p.x, (p.y)+));
}
for(i = ; i < row; i++)
{
for(j = ; j < col; j++)
{
if('O' == board[i][j])
board[i][j] = 'X';
if('N' == board[i][j])
board[i][j] = 'O';
}
cout<<endl;
}
}
};

先找到四条边缘上面的字符,再找和这些字符相邻的字符。

130. Surrounded Regions -- 被某字符包围的区域的更多相关文章

  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 (2 solutions)

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

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

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

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

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

  7. Java for LeetCode 130 Surrounded Regions

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

  8. Leetcode 130. Surrounded Regions

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

  9. 130. Surrounded Regions

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

随机推荐

  1. 【转载】UML类图知识整理

    原文:UML类图知识整理 UML类图 UML,进阶必备专业技能,看不懂UML就会看不懂那些优秀的资料. 这里简单整理 类之间的关系 泛化关系(generalization) 泛化(generalize ...

  2. Using Post-Form Trigger In Oracle Forms

    Post-Form trigger in Oracle Forms fires during the Leave the Form process, when a form is exited.   ...

  3. github 修改fork的代码之后如何提交代码并pull request

    官方的解释还是有点模糊,我是参照这篇文章来的. http://www.linuxidc.com/Linux/2012-12/76922.htm 关于Git的版本管理的原理,我是从这篇文章里面学习的. ...

  4. mysql数据小姿势

    CREATE TABLE `information` (  `NUMBER` bigint(20) NOT NULL AUTO_INCREMENT,//将number设为自增字段  `USER_NAM ...

  5. C#中实现多继承的方法

    有一个类叫老虎,还有一个类叫苍蝇.现在新创一个超级老虎类,一种可以飞的老虎,超级老虎由于同时也继承自苍蝇 namespace Interface { //飞的接口 public interface I ...

  6. C# int.Parse()与int.TryParse()

    int i = -1;bool b = int.TryParse(null, out i);执行完毕后,b等于false,i等于0,而不是等于-1,切记. int i = -1;bool b = in ...

  7. MongoDB资料汇总

    MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. 它的特点是高性能.易部署.易使用,存储数据非常方便.主要功能特性有: 面向集合存 ...

  8. iOS - MVC 架构模式

    1.MVC 从字面意思来理解,MVC 即 Modal View Controller(模型 视图 控制器),是 Xerox PARC 在 20 世纪 80 年代为编程语言 Smalltalk-80 发 ...

  9. mysql概要(六)连接

    内连接 [join on / from 表1,表二 ]效果一样 区别是:可以理解为首先取得笛卡儿积后,再 内连接:取俩表的匹配数据: 左连接:取的俩表匹配数据,并且保留未匹配数据中左表的数据,右表数据 ...

  10. spring之aop概念和配置

    面向切面的一些概念: 简单说: 连接点就一些方法,在这些方法基础上需要额外的一些业务需求处理. 切入点就是方法所代表的功能点组合起来的功能需求. 通知就是那些额外的操作. 织入就是使用代理实现整个切入 ...