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

  solution: 思想是mark 出所有外围的O,里面的不管。

  1. First scan the four edges of the board, if you meet an 'O', call a recursive mark function to mark that region to something else (for example, '+');
  2. scan all the board, if you meet an 'O', flip it to 'X';
  3. scan again the board, if you meet an '+', flip it to 'O';
    class Solution {
    public:
    void BFS(vector<vector<char>> &board, int i , int j){ if(board[i][j] == 'X' || board[i][j] == '#') return;
    board[i][j] = '#'; if( i - >= ) BFS(board, i-, j );
    if( i + < rows ) BFS(board, i+, j );
    if( j - >= ) BFS(board, i, j- );
    if( j + < columns ) BFS(board, i, j+ );
    }
    void solve(vector<vector<char>> &board) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    rows = board.size();
    if( rows == ) return ;
    columns = board[].size();
    if(columns == ) return ; for( int i = ; i < columns - ; i++)
    {
    BFS(board, ,i);//up
    BFS(board, rows - , i );//down
    }
    for( int i = ; i< rows; i++)
    {
    BFS(board, i, ); //left
    BFS(board, i, columns - ); //right
    }
    for(int i = ; i< rows; i++)
    for(int j = ; j < columns ; j++)
    {
    if(board[i][j] == '#')
    board[i][j] = 'O';
    else if (board[i][j] == 'O')
    board[i][j] = 'X';
    }
    }
    private :
    int rows;
    int columns;
    };

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

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

  4. LEETCODE —— Surrounded Regions

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

  5. [REP]AWS Regions and Availability Zones: the simplest explanation you will ever find around

    When it comes to Amazon Web Services, there are two concepts that are extremely important and spanni ...

  6. Leetcode 130. Surrounded Regions

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

  7. 【leetcode】Surrounded Regions

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

  8. 【leetcode】Surrounded Regions(middle)☆

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

  9. ural 1147. Shaping Regions

    1147. Shaping Regions Time limit: 0.5 secondMemory limit: 64 MB N opaque rectangles (1 ≤ N ≤ 1000) o ...

随机推荐

  1. Delphi keydown与keyup、keypress的区别(KeyDown 和KeyUp 通常可以捕获键盘除了PrScrn所有按键)

    Shift 是一个集合变量.type TShiftState = set of (ssShift, ssAlt, ssCtrl, ssLeft, ssRight, ssMiddle, ssDouble ...

  2. netcat

    一.概述 netcat是网络工具中的瑞士军刀,它能通过TCP和UDP在网络中读写数据.通过与其他工具结合和重定向,你可以在脚本中以多种方式使用它.使用netcat命令所能完成的事情令人惊讶.netca ...

  3. spin.js无图片实现loading进度条,支持但非依赖jquery

    特点: 1.无图片,无外部CSS 2.无依赖(支持jQuery,但非必须) 3.高度可配置 4.分辨率无关 5.旧版本IE不支持时,采用VML支持 6.使用关键帧动画,采用setTimeout() 7 ...

  4. 【转】四步完成win7 ubuntu双系统安装(硬盘,无需光驱)

    原文网址:http://ifeiyang.cn/archives/1835.html 适用环境: 理论上win7.vista系统32位或64位均可.ubuntu适用与10.X版本,且ubuntu-10 ...

  5. python学习Processing

    # -*- coding: utf-8 -*-__author__ = 'Administrator'import bisect#排序说明:http://en.wikipedia.org/wiki/i ...

  6. HBase二级索引的设计(案例讲解)

    摘要 最近做的一个项目涉及到了多条件的组合查询,数据存储用的是HBase,恰恰HBase对于这种场景的查询特别不给力,一般HBase的查询都是通过RowKey(要把多条件组合查询的字段都拼接在RowK ...

  7. JAVA单线程以及java多线程的实现方式

    1.java单线程的实现 public class SingletonThread { @SuppressWarnings("static-access") public stat ...

  8. JMeter Building a Database Test Plan

    Building a Database Test Plan In this section, you will learn how to create a basic Test Planto test ...

  9. [AngularJS] Angular 1.5 multiple transclude

    If you know ui-router, multi-transclude should be easy for you also. In previou Angular version < ...

  10. 单片机脚本语言-移植lua到stm32-MDK

    Lua简单介绍 Lua[1]  是一个小巧的脚本语言.作者是巴西人.该语言的设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能. Lua脚本能够非常easy的被C/C++ 代码调用, ...