题目链接

https://leetcode.com/problems/surrounded-regions/

注意点

  • 边缘不算包围‘O’

解法

解法一:dfs。找处在边缘上的O然后dfs将与之相邻的O都改为#。处理完之后再把这时候的O改为X,#改为O即可

class Solution {
public:
void solve(vector<vector<char>>& board) {
int n = board.size();
for(int i = 0;i < n;i++)
{
for(int j = 0;j < board[i].size();j++)
{
if((i == 0 || i == n-1 || j == 0 ||j == board[i].size()-1) && board[i][j] == 'O') dfs(board,i,j);
}
}
for(int i = 0;i < n;i++)
{
for(int j = 0;j < board[i].size();j++)
{
if(board[i][j] == 'O') board[i][j] = 'X';
if(board[i][j] == '#') board[i][j] = 'O';
}
}
}
void dfs(vector<vector<char>>& board,int i,int j)
{
if (board[i][j] == 'O')
{
board[i][j] = '#';
if (i > 0 && board[i - 1][j] == 'O')
dfs(board, i - 1, j);
if (j < board[i].size() - 1 && board[i][j + 1] == 'O')
dfs(board, i, j + 1);
if (i < board.size() - 1 && board[i + 1][j] == 'O')
dfs(board, i + 1, j);
if (j > 0 && board[i][j - 1] == 'O')
dfs(board, i, j - 1);
}
}
};

解法二:bfs。基本上一样的思路,还是找处在边缘上的O然后bfs将与之相邻的O都改为#。处理完之后再把这时候的O改为X,#改为O即可

class Solution {
public:
void solve(vector<vector<char>>& board) {
int n = board.size();
for(int i = 0;i < n;i++)
{
for(int j = 0;j < board[i].size();j++)
{
if((i == 0 || i == n-1 || j == 0 ||j == board[i].size()-1) && board[i][j] == 'O') dfs(board,i,j);
}
}
for(int i = 0;i < n;i++)
{
for(int j = 0;j < board[i].size();j++)
{
if(board[i][j] == 'O') board[i][j] = 'X';
if(board[i][j] == '#') board[i][j] = 'O';
}
}
}
void dfs(vector<vector<char>>& board,int i,int j)
{
if (board[i][j] == 'O')
{
board[i][j] = '#';
if (i > 0 && board[i - 1][j] == 'O')
dfs(board, i - 1, j);
if (j < board[i].size() - 1 && board[i][j + 1] == 'O')
dfs(board, i, j + 1);
if (i < board.size() - 1 && board[i + 1][j] == 'O')
dfs(board, i + 1, j);
if (j > 0 && board[i][j - 1] == 'O')
dfs(board, i, j - 1);
}
}
};

小结

  • 这道题dfs和bfs效率都差不多

Surrounded Regions - LeetCode的更多相关文章

  1. Surrounded Regions——LeetCode

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

  2. Surrounded Regions leetcode java

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

  3. [LeetCode] Surrounded Regions 包围区域

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

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

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

  5. 【LeetCode】130. Surrounded Regions (2 solutions)

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

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

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

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

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

  8. 【leetcode】Surrounded Regions

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

  9. leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions

    两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...

随机推荐

  1. Ionic app升级插件开发

    终于走到了写插件的这个地方了,插件的过程: 1.安装plugman插件,管理我们的程序 npm install -g plugman 2.创建插件项目appUpgrade,cd 到你的目标目录下,执行 ...

  2. PCB之PASTE助焊层和SOLDER阻焊层

    1.PASTE为焊接层,用于SMT贴片元件的焊接,对应的图形为钢网(钢网上的小孔): 2.SOLDER为阻焊层,它代表的是绿油的涂抹区域,且为负片输出(负片输出指的是图形以外的区域为有效区域): PA ...

  3. 蓝牙baseband概述

    从蓝牙specispecification中看,基带协议主要分为8个部分来介绍的,分别是概述.物理信道.物理连接.逻辑传输.逻辑连接.封包.比特流的处理.组网行为.这里面会涉及到很多的概念,主要是在概 ...

  4. 【javascript】详解javaScript的深拷贝

        前言: 最开始意识到深拷贝的重要性是在我使用redux的时候(react + redux), redux的机制要求在reducer中必须返回一个新的对象,而不能对原来的对象做改动,事实上,当时 ...

  5. CEPH Object Gateway

    参考文档: CEPH OBJECT GATEWAY:http://docs.ceph.com/docs/master/radosgw/ 一.环境准备 1. Ceph Object Gateway框架 ...

  6. centos6下redis cluster集群部署过程

    一般来说,redis主从和mysql主从目的差不多,但redis主从配置很简单,主要在从节点配置文件指定主节点ip和端口,比如:slaveof 192.168.10.10 6379,然后启动主从,主从 ...

  7. C_数据结构_链式二叉树

    # include <stdio.h> # include <malloc.h> struct BTNode { int data; struct BTNode * pLchi ...

  8. BugPhobia休息篇章:Beta阶段第IX次Scrum Meeting前奏

    特别说明:此次Scrum Meeting不计入正式的Scrum Meeting,因此此次工作仅为第IX次Scrum Meeting的前奏,而笔者也首次采用休息篇章作为子命题   0x01 :Scrum ...

  9. <<浪潮之巅>>阅读笔记一

    第一次的阅读就想读这本书的,却因为很多愿意一直拖到现在,因为听说这本书在李开复先生 的微博上有推荐,更是增加了我的阅读兴趣.可能是因为在网上找的电子版的原因,通篇阅读的速度很快,但是没有纸质数那种细嚼 ...

  10. Distances to Zero CodeForces - 803B (二分)

    题目链接:https://vjudge.net/problem/CodeForces-803B#author=0 题意: 给你一个数组,其中至少包括一个0,求每一个元素距离最近一个0的距离是多少. 样 ...