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. centos7.4安装rabbitmq服务(3.7.10版本)

    一.需要安装erlang版本依赖,可以使用二进制安装方式,也可以通过rpm安装,但是安装的时候会提示需要erlang版本>=19.3,而且直接默认yum仓库中的版本较低.,为了节省时间,文章中直 ...

  2. 如何在 Linux 中配置基于密钥认证的 SSH

    什么是基于 SSH 密钥的认证? 众所周知,Secure Shell,又称 SSH,是允许你通过无安全网络(例如 Internet)和远程系统之间安全访问/通信的加密网络协议.无论何时使用 SSH 在 ...

  3. 初级React入门

    一.引入Reactjs 方法一:直接下载相关js文件引入网页,其中react.js 是 React 的核心库,react-dom.js 是提供与 DOM 相关的功能,Browser.js 的作用是将 ...

  4. Redis之set类型操作

    接口: package com.net.test.redis.base.dao; /** * @author*** * @Time:2017年8月10日 下午2:32:12 * @version 1. ...

  5. libevent源码分析1 ----evnet相关结构体分析

    位于代码event-internal.h中. event_base类似事件的集合,你创建一个事件,必须将该事件指定一个集合. struct event_base { 50     const stru ...

  6. CodeForces 768E Game of Stones 打表找规律

    题意: 在经典Nim博弈的基础上增加了新的限制:如果从这堆石子中移走\(x\)个石子,那么之后就不能再从这堆移走\(x\)个. 分析: 因为之前的操作会对后面的转移有影响,所以在保存状态时还要记录哪些 ...

  7. “帮你APP”团队冲刺7

    1.整个项目预期的任务量 (任务量 = 所有工作的预期时间)和 目前已经花的时间 (所有记录的 ‘已经花费的时间’),还剩余的时间(所有工作的 ‘剩余时间’) : 所有工作的预期时间:88h 目前已经 ...

  8. Oracle 分析函数--Row_Number()

    row_number() over ([partition by col1] order by col2) ) as 别名 表示根据col1分组,在分组内部根据 col2排序 而这个“别名”的值就表示 ...

  9. php伪随机数漏洞 以及脚本php_mt_seed的使用教程

    前几天在群里看到了一个题目,发现自己没有接触过这个伪随机数这个漏洞,在此记录下. 搜索这两个函数 mt_scrand() mt_rand() mt_scrand(seed)这个函数的意思,是通过分发s ...

  10. git:多个sshkey配置

    克隆项目: 使用git clone +项目.git地址 例如: 创建SSH Key: ssh-keygen -t rsa -C +邮箱地址 sshkey自定义保存:创建后在第二步(enter file ...