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. C++ lambda 表达式 简介

    自己根据对lambda表达式的理解,做了一套ppt简单介绍

  2. mybatis的环境搭建

    mybatis是一个持久层框架,其主要思想就是想将程序中大量的SQL语句剥离出来,配置在配置文件中,实现SQL的灵活配置. 使得SQL与程序代码分离,即在不修改程序代码的情况下,直接在配置文件中修改S ...

  3. JavaScript 基础语法

    1 谈谈 JavaScript JavaScript,通常会简称为'JS', 是一种浏览器脚本语言 1.1 JavaScript 编程语言特点 JavaScript是一种脚本编程语言 JavaScri ...

  4. .NET 执行命令行乱码

    Process可以运行命令行内容儿不用担心会弹出命令行窗口 需要读取命令行结果时,如果不注意内容编码,就会出现读取的结果出现乱码 读取StandardOutput结果时需要指定StandardOutp ...

  5. 学习Spring框架系列(一):通过Demo阐述IoC和DI的优势所在

    Spring框架最核心东西便是大名鼎鼎的IoC容器,主要通过DI技术实现.下面我通过Demo的演变过程,对比学习耦合性代码,以及解耦和的过程,并深入理解面向接口编程的真正内涵. 这个例子包括如下几个类 ...

  6. Python9-MySQL-Homework-day43

    表结构 SET NAMES utf8; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure f ...

  7. 【4Sum】cpp

    题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  8. leetcode 【 Search in Rotated Sorted Array II 】python 实现

    题目: 与上一道题几乎相同:不同之处在于array中允许有重复元素:但题目要求也简单了,只要返回true or false http://www.cnblogs.com/xbf9xbf/p/42545 ...

  9. 课堂笔记III

  10. linux配置Hadoop伪分布安装模式

    1)关闭禁用防火墙: /etc/init.d/iptables status 会得到一系列信息,说明防火墙开着. /etc/rc.d/init.d/iptables stop 关闭防火墙 2)禁用SE ...