题目描述

现在有一个仅包含‘X’和‘O’的二维板,请捕获所有的被‘X’包围的区域
捕获一个被包围区域的方法是将被包围区域中的所有‘O’变成‘X’
例如
X X X X↵X O O X↵X X O X↵X O X X

执行完你给出的函数以后,这个二维板应该变成:

X X X X↵X X X X↵X X X X↵X O X X

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
class Solution {
public:
    void solve(vector<vector<char>> &board) {
        if(board.empty())
            return;
        int rows = board.size();
        int cols = board[0].size();
         
        if(rows==0 || cols==0)
            return;
         
        for(int j=0;j<cols;j++)
        {
            DFS(board, 0, j);
            DFS(board, rows-1, j);         }                  for(int i=0;i<rows;i++)         {             DFS(board, i, 0);             DFS(board, i, cols-1);         }                  for(int i=0;i<rows;i++)             for(int j=0;j<cols;j++)                 if(board[i][j] == 'O')                     board[i][j] = 'X';                  for(int i=0;i<rows;i++)             for(int j=0;j<cols;j++)                 if(board[i][j] == '*')                     board[i][j] = 'O';
    }
    void DFS(vector<vector<char> > &board, int r, int c)
    {
        if(board[r][c] == 'O')
        {
            board[r][c] = '*';             int rows = board.size();             int cols = board[0].size();                          if(r > 1)                 DFS(board, r-1, c);             if(r < rows-2)                 DFS(board, r+1, c);             if(c > 1)                 DFS(board, r, c-1);             if(c < cols-2)                 DFS(board, r, c+1);                     }     }
};

最新文章

  1. Jedis的使用
  2. 委托的N种写法,你喜欢哪种?
  3. 控制台手动编译Qt5程序
  4. sql server 日期相关操作
  5. java的system.arraycopy()方法
  6. 使用POI读取excel文件内容
  7. Kakfa揭秘 Day3 Kafka源码概述
  8. mini-httpd源码分析-mini-httpd.c
  9. 使用awk和grep做简单的统计
  10. 【ArcGIS 10.2新特性】ArcGIS 10.2 for Desktop 新特性(二)
  11. AspNet MVC3中过滤器 + 实例
  12. Codeforces Round #372 +#373 部分题解
  13. Windows下配置nginx+FastCgi + Spawn-fcgi
  14. 关于国际化时报org.springframework.context.NoSuchMessageException错,具体到No message found under code &#39;你的键名&#39; for locale &#39;zh_CN&#39;.的解决方案
  15. 学习python的第一天
  16. Redux进阶(像VUEX一样使用Redux)
  17. 游戏AI
  18. 利用Webpack+React(antd)+ES6+python(flask)实现代码转换
  19. JavaScript的Date类的函数特殊处理导致的问题
  20. emptyDir与hostPath

热门文章

  1. 启动你的Android应用:运行设备模拟器和调试代码(第3部分)
  2. 远程触发Jenkins的Pipeline任务的并发问题处理
  3. fio硬盘测速windows+linux
  4. 如何免费安装正版Adobe
  5. vue 组件的封装
  6. 多测师讲解自动化_rf框架搭建_高级讲师肖sir
  7. Git的介绍以及安装
  8. .net c#后台请求接口
  9. spring boot:使接口返回统一的RESTful格式数据(spring boot 2.3.1)
  10. spring boot:用redis+lua实现表单接口的幂等性(spring boot 2.2.0)