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. 自动化运维工具——ansible剧本playbook(三)

    一.Playbook--Ansible剧本 playbook是由一个或多个 "play"组成的列表 play的主要功能在于将事先归并为一组的主机装扮成事先通过ansible中的ta ...

  2. 503. Next Greater Element II

    https://leetcode.com/problems/next-greater-element-ii/description/ class Solution { public: vector&l ...

  3. ARM linux中断总结

    Linux异常处理体系结构 Linux异常体系之vector_stub宏解析 Linux异常体系之stubs_offset Linux中断体系结构 ARM系统调用

  4. Codeforces Round #464 (Div. 2) B. Hamster Farm

    B. Hamster Farm time limit per test2 seconds memory limit per test256 megabytes Problem Description ...

  5. Java多线程-join方法

    thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程.比如在线程B中调用了线程A的Join()方法,直到线程A执行完毕后,才会继续执行线程B. 具体例子看链接 ...

  6. border-color与color

    1.border-color就是color,即border-color的默认颜色就是color 当没有指定border-color的时候,会使用color作为边框的颜色! 类似的还有text-shad ...

  7. [oldboy-django][2深入django]班级管理(Form)--编辑

    1.需求:编辑班级,难点:点击编辑按钮后,页面如何显示 待编辑 班级的信息(用Form实现), 以及Url 设置 2 url url(r'^edit_class/nid=(?P<nid>\ ...

  8. MQ、JMS以及ActiveMQ的了解和认识

    新加入的公司中,架构用到了activeMq,对于以前只了解nginx.tomcat的我有点懵逼,所以在网上找点资料看看,了解下什么是MQ,activemq.具体作用是什么 MQ MQ简介: MQ全称为 ...

  9. ubuntu下安装JDK(复制)

    ubuntu 安装jdk 的两种方式:(本来jdk应该安装到/usr/lib/jvm下,但我安装到了/usr/local/lib/jvm下了) 1:通过ppa(源) 方式安装. 2:通过官网下载安装包 ...

  10. PAT1034

    本题要求编写程序,计算2个有理数的和.差.积.商. 输入格式: # include<iostream> # include<algorithm> # include<st ...