130. 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
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 -- 被某字符包围的区域的更多相关文章
- 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 用了第一种方式, ...
- 130. Surrounded Regions(M)
130.Add to List 130. Surrounded Regions Given a 2D board containing 'X' and 'O' (the letter O), capt ...
- [LeetCode] 130. Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...
- 【LeetCode】130. Surrounded Regions (2 solutions)
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- 【一天一道LeetCode】#130. Surrounded Regions
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 130. Surrounded Regions(周围区域问题 广度优先)(代码未完成!!)
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- Java for LeetCode 130 Surrounded Regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- Leetcode 130. Surrounded Regions
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- 130. Surrounded Regions
题目: Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is capt ...
随机推荐
- Creating, Stopping, Re-Starting and Deleting a Timer in Oracle Forms
I have written many posts previously on Timers in Oracle Forms like how to change images randomly wi ...
- DevOps到底是什么?
本篇将讨论DevOps到底包含什么,今后的运维工程师应该朝什么方向努力.
- Beaglebone Black - 控制 BBB 板上的 LED 灯
BBB 的板上有五个 LED 灯,一个电源,四个其他指示灯,usr0 至 usr3 .这次学习是控制 usr0 至 3 让它们亮着,熄灭,闪.算是个 Hello World 实验.非常简单. 需要的材 ...
- kvc kvo(摘录)
概述 由于ObjC主要基于Smalltalk进行设计,因此它有很多类似于Ruby.Python的动态特性,例如动态类型.动态加载.动态绑定等.今天我们着重介绍ObjC中的键值编码(KVC).键值监听( ...
- JMS【二】--ActiveMQ简单介绍以及安装
现实的企业中,对于消息通信的应用一直都非常的火热,而且在J2EE的企业应用中扮演着特殊的角色,所以对于它研究是非常有必要的. 上篇博文JMS[一]--JMS基本概念,我们介绍了消息通信的规范JMS,我 ...
- Eclipse小技巧--快速输入System.out.println();(转)
步骤1:指定“Content Assist”的快捷键,参考:善用 Eclipse 组合键,提高输入效率 步骤2:输入源代码是,先输入sysout,然后输入辅助快捷键:Alt+/,这样就可以自动生成:S ...
- LotteryDrawing
import java.util.*; public class MyTest{ public static void main(String[] args){ Scanner in = new Sc ...
- [转载] tmux的使用tips
原文: http://tangosource.com/blog/a-tmux-crash-course-tips-and-tweaks/
- poj1066Treasure Hunt(线段相交)
链接 很纠结的找到了所有线段的中点,又很纠结的找到了哪些中点可以直接相连,最后bfs一下求出了最短路.. #include <iostream> #include<cstdio> ...
- maven入门
1.1.项目构建 Maven(翻译为"专家","内行")是跨平台的项目管理工具.主要服务于基于Java平台的项目构建,依赖管理和项目信息管理. 项目构建过程包括 ...