[LeetCode] 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
#include <vector>
#include <iostream>
#include <iterator>
using namespace std; class Solution {
public:
void solve(vector<vector<char> > &board) {
int m=board.size();
if(m<) return;
int n=board[].size();
if(n<) return;
vector<vector<bool> > flag;
for(int i=;i<m;i++) flag.push_back(vector<bool>(n,false));
for(int i=;i<m;i++){
for(int j=;j<n;j++){
if(board[i][j]=='X') flag[i][j]=true;
if(board[i][j]=='O'&&flag[i][j]==false)
help_f(i,j,board,flag);
}
}
return ;
}
void help_f(int i,int j,vector<vector<char> > &board,vector<vector<bool> > &flag)
{
// cout<<"Azhu"<<endl;
vector<pair<int,int> > tmp;
tmp.push_back({i,j});
flag[i][j]=true;
int now_idx=,m=board.size(),n=board[].size();
while(now_idx<tmp.size()){
int now_i = tmp[now_idx].first,now_j = tmp[now_idx].second;
if(now_i->=&&board[now_i-][now_j]=='O'&&flag[now_i-][now_j]==false){
tmp.push_back({now_i-,now_j});
flag[now_i-][now_j]=true;
}
if(now_i+<m&&board[now_i+][now_j]=='O'&&flag[now_i+][now_j]==false){
tmp.push_back({now_i+,now_j});
flag[now_i+][now_j]=true;
}
if(now_j->=&&board[now_i][now_j-]=='O'&&flag[now_i][now_j-]==false){
tmp.push_back({now_i,now_j-});
flag[now_i][now_j-]=true;
}
if(now_j+<n&&board[now_i][now_j+]=='O'&&flag[now_i][now_j+]==false){
tmp.push_back({now_i,now_j+});
flag[now_i][now_j+]=true;
}
now_idx++;
}
bool canCapture=true;
now_idx=;
while(canCapture&&now_idx<tmp.size()){
int now_i = tmp[now_idx].first,now_j = tmp[now_idx].second;
if(now_i==||now_i==m-||now_j==||now_j==n-) canCapture=false;
now_idx++;
}
if(canCapture){
now_idx=;
while(now_idx<tmp.size()){
int now_i = tmp[now_idx].first,now_j = tmp[now_idx].second;
board[now_i][now_j]='X';
now_idx++;
}
}
return ;
}
}; int main()
{
vector<vector<char> > board{
{{'X','X','X','O'}},
{{'X','O','O','X'}},
{{'X','X','X','X'}},
{{'X','O','O','X'}}
};
Solution sol;
sol.solve(board);
for(int i=;i<board.size();i++){
copy(board[i].begin(),board[i].end(),ostream_iterator<char>(cout," "));
cout<<endl;
} return ;
}
[LeetCode] Surrounded Regions 广度搜索的更多相关文章
- [LeetCode] Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- 验证LeetCode Surrounded Regions 包围区域的DFS方法
在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ ...
- LeetCode: Surrounded Regions 解题报告
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- [leetcode]Surrounded Regions @ Python
原题地址:https://oj.leetcode.com/problems/surrounded-regions/ 题意: Given a 2D board containing 'X' and 'O ...
- Leetcode: Surrounded regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- LEETCODE —— Surrounded Regions
Total Accepted: 43584 Total Submissions: 284350 Difficulty: Medium Given a 2D board containing 'X' a ...
- LeetCode: Surrounded Regions [130]
[题目] Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is cap ...
- [LeetCode] 130. Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...
- Leetcode之深度优先搜索(DFS)专题-130. 被围绕的区域(Surrounded Regions)
Leetcode之深度优先搜索(DFS)专题-130. 被围绕的区域(Surrounded Regions) 深度优先搜索的解题详细介绍,点击 给定一个二维的矩阵,包含 'X' 和 'O'(字母 O) ...
随机推荐
- vs对某些网络错误的拦截
在编写代码的过程中发现如果在写好网页中的文本框内写入js代码(以<script>1</script>输入为例) vs会自动拦截并报错,如图(密码中我也输入了<script ...
- python笔记-tuple元组的方法
#!/usr/bin/env python #-*- coding:utf-8 -*- # 创建空元组 tuple1 = () print(tuple) # 创建带有元素的元组 # 元组中的类型可以不 ...
- Flask初学者:Python虚拟环境,Flask安装,helloworld,run方法
一.Python虚拟环境: 作用:使Python框架的不同版本可以在同一台电脑上运行.如果在电脑上全局(C盘或者其他目录)安装Flask(或其他Python框架),当你使用其他版本的Flask(比如有 ...
- 809. Expressive Words
https://leetcode.com/problems/expressive-words/description/ class Solution { public: int expressiveW ...
- Sliding Window POJ - 2823
Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is m ...
- IntentService和Service执行子线程对比
1.为何要用子程序 服务是在主线程中执行的,直接在服务中执行耗时操作明显不可取,于是安卓官方增加了IntentService类来方便使用 在Service中执行子程序代码如下 @Override pu ...
- 2 Model层-模型成员
1 类的属性 objects:是Manager类型的对象,用于与数据库进行交互 当定义模型类时没有指定管理器,则Django会为模型类提供一个名为objects的管理器 支持明确指定模型类的管理器 c ...
- Python中函数参数类型和参数绑定
参数类型 Python函数的参数类型一共有五种,分别是: POSITIONAL_OR_KEYWORD(位置参数或关键字参数) VAR_POSITIONAL(可变参数) KEYWORD_ONLY(关键字 ...
- 【ELK】ELK安装与配置
一.ELK体系结构 二.系统环境变量 [主机信息] IP 主机名 操作系统版本 10.10.10.102 console CentOS7.5 10.10.10.103 log1 CentOS7.510 ...
- "帮你"-用户模板和用户场景
场景/故事/story 典型用户: 用户性质 典型用户介绍 姓名 小李 年龄 20岁 职业 学生 代表的用户在市场上的比例和重要性 代表学校内广大普通学生,因此有很大的重要性. 使用本软件的典型场景 ...