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 ...
随机推荐
- linux留下后门的技巧
在团队内部的wiki上已经写出 http://drops.wooyun.org/tips/1951 http://www.freebuf.com/sectool/10474.html 还有一种方法是写 ...
- kaili 2.0 metasploit连接postgres数据库
第一步:使用命令 db_init 初始化数据库
- C语言--指针问题_1
#include <stdio.h> #include <string.h> main() { int *a,*b,*c; a=b=c=(int *)malloc(sizeof ...
- poj 1265 Area (Pick定理+求面积)
链接:http://poj.org/problem?id=1265 Area Time Limit: 1000MS Memory Limit: 10000K Total Submissions: ...
- hdu 1217 (Floyd变形)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 Arbitrage Time Limit: 2000/1000 MS (Java/Others) ...
- 3.mybatis注解
在上篇2.mybatis入门实例(一) 连接数据库进行查询的基础上 1.添加Mapper接口:UserMapper接口,并使用mybatis的注解 import java.util.List; imp ...
- VS生成事件
源自:http://www.cnblogs.com/FreeDong/p/3406737.html 如果说磨刀不误砍柴工,同样用好Visual Studio,会大大增加咱.NET程序猿效率.本文说的就 ...
- 作用域内优先级及this指针
函数声明>变量声明 作用域:顶部----------------------> 函数声明会覆盖变量声明,但不会覆盖变量赋值 this指针不是变量.当调用括号的左边不是引用类型而是其它类型, ...
- iOS - Block 代码块
1.Block Block 是一段预先准备好的代码,可以在需要的时候执行,可以当作参数传递.Block 可以作为函数参数或者函数的返回值,而其本身又可以带输入参数或返回值.Block 是 C 语言的, ...
- Python学习(8)字符串
目录 Python 字符串 Python 访问字符串中的值 Python 字符串更新 Python 转义字符 Python 字符串运算符 Python 字符串格式化 Python 三引号 Unicod ...