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
Hide Tags

Breadth-first Search

 

  题目是给定一个char 矩阵,将被包括的O 标记为X。
思路:
  遍历整个矩阵,如果遇到O 那么便找出全部与其相连的O,然后判断这个是否需要变换。
 
  这样的实现就是判断麻烦一些,而且会有重复判断,因为需要先找出全部的相连O,再遍历一次判断,然后遍历修改,操作有重复了,下面是我写的代码:
 
#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 ;
}
 
改进:
  注意到O 不能修改的是与边相连的时候,那么便改变下过程,从边开始,找出全部不能够变化的O,然后其余的O 变化,这样时间快很多。这个就不写了。
 
discuss 中提到并查集,看了一下,也就是讲全部的O 找出来,相连的划分成一个集合中,集合内的元素链表表示,一个接一个,这样避免了重复,而我实现是通过后台标记,并查集的时间其实更长,因为是单项链表的实现。或许有更好的实现,例如1-n 树表示,不过选用那个作为根节点,好像这题目不是很好用。
 
 

[LeetCode] Surrounded Regions 广度搜索的更多相关文章

  1. [LeetCode] Surrounded Regions 包围区域

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

  2. 验证LeetCode Surrounded Regions 包围区域的DFS方法

    在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ ...

  3. LeetCode: Surrounded Regions 解题报告

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

  4. [leetcode]Surrounded Regions @ Python

    原题地址:https://oj.leetcode.com/problems/surrounded-regions/ 题意: Given a 2D board containing 'X' and 'O ...

  5. Leetcode: Surrounded regions

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

  6. LEETCODE —— Surrounded Regions

    Total Accepted: 43584 Total Submissions: 284350 Difficulty: Medium Given a 2D board containing 'X' a ...

  7. LeetCode: Surrounded Regions [130]

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

  8. [LeetCode] 130. Surrounded Regions 包围区域

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

  9. Leetcode之深度优先搜索(DFS)专题-130. 被围绕的区域(Surrounded Regions)

    Leetcode之深度优先搜索(DFS)专题-130. 被围绕的区域(Surrounded Regions) 深度优先搜索的解题详细介绍,点击 给定一个二维的矩阵,包含 'X' 和 'O'(字母 O) ...

随机推荐

  1. vs对某些网络错误的拦截

    在编写代码的过程中发现如果在写好网页中的文本框内写入js代码(以<script>1</script>输入为例) vs会自动拦截并报错,如图(密码中我也输入了<script ...

  2. python笔记-tuple元组的方法

    #!/usr/bin/env python #-*- coding:utf-8 -*- # 创建空元组 tuple1 = () print(tuple) # 创建带有元素的元组 # 元组中的类型可以不 ...

  3. Flask初学者:Python虚拟环境,Flask安装,helloworld,run方法

    一.Python虚拟环境: 作用:使Python框架的不同版本可以在同一台电脑上运行.如果在电脑上全局(C盘或者其他目录)安装Flask(或其他Python框架),当你使用其他版本的Flask(比如有 ...

  4. 809. Expressive Words

    https://leetcode.com/problems/expressive-words/description/ class Solution { public: int expressiveW ...

  5. 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 ...

  6. IntentService和Service执行子线程对比

    1.为何要用子程序 服务是在主线程中执行的,直接在服务中执行耗时操作明显不可取,于是安卓官方增加了IntentService类来方便使用 在Service中执行子程序代码如下 @Override pu ...

  7. 2 Model层-模型成员

    1 类的属性 objects:是Manager类型的对象,用于与数据库进行交互 当定义模型类时没有指定管理器,则Django会为模型类提供一个名为objects的管理器 支持明确指定模型类的管理器 c ...

  8. Python中函数参数类型和参数绑定

    参数类型 Python函数的参数类型一共有五种,分别是: POSITIONAL_OR_KEYWORD(位置参数或关键字参数) VAR_POSITIONAL(可变参数) KEYWORD_ONLY(关键字 ...

  9. 【ELK】ELK安装与配置

    一.ELK体系结构 二.系统环境变量 [主机信息] IP 主机名 操作系统版本 10.10.10.102 console CentOS7.5 10.10.10.103 log1 CentOS7.510 ...

  10. "帮你"-用户模板和用户场景

    场景/故事/story 典型用户: 用户性质 典型用户介绍 姓名 小李 年龄 20岁 职业 学生 代表的用户在市场上的比例和重要性 代表学校内广大普通学生,因此有很大的重要性. 使用本软件的典型场景 ...