/*** https://oj.leetcode.com/problems/surrounded-regions/* 从四个边界的'O'出发,它们所能到达的'O'就是没有被包围的'O'。* 所以,该题能够用BFS遍历或者DFS遍历。
*/class Solution {
public:void solve(vector<vector<char>> &board) {int row = board.size();if(row <= 2){return;}int col = board[0].size();if(col <=2){return;}int i = 0;int j = 0;if(row > 2 && col > 2){for(i = 0; i < row; i++){if(board[i][0] == 'O'){board[i][0] = 'P';if(board[i][1] == 'O'){search(i, 1, row, col, board);}}if(board[i][col - 1] == 'O'){board[i][col - 1] = 'P';if(board[i][col - 2] == 'O'){search(i, col - 2, row, col, board);  }}}//end of for(i = 0; i < row; i++)for(j = 0; j < col; j++){if(board[0][j] == 'O'){board[0][j] = 'P';if(board[1][j] == 'O'){search(1, j, row, col, board);}}if(board[row - 1][j] == 'O'){board[row - 1][j] = 'P';if(board[row - 2][j] == 'O'){search(row - 2, j, row, col, board);  }}} //end of for(j = 0; j < col; j++)}//end of if(row >2 && col > 2)for(i = 0; i < row; i++){for(j = 0; j < col; j++){if(board[i][j] == 'P'){board[i][j] = 'O';} else {board[i][j] = 'X';} }}}//search(1, j, row, col, board); void search(int i, int j, const int row, const int col, vector<vector<char>> &board){board[i][j]='P';if(i - 1 > 0 && board[i - 1][j] == 'O'){search(i - 1, j, row, col, board);}if(i + 1 < row - 1 && board[i + 1][j] == 'O'){search(i+1, j, row, col, board);}if(j - 1 > 0  && board[i][j - 1] == 'O'){search(i, j - 1, row, col, board);}if(j + 1 < col  && board[i][j + 1] == 'O'){search(i, j + 1, row, col, board);}}
};

Surrounded Regions相关推荐

  1. 【BFS】LeetCode 130. Surrounded Regions

    LeetCode 130. Surrounded Regions Solution1:我的答案 利用bfs解决,超过98.7%的答案,还不错.关键是简单易懂! class Solution { pub ...

  2. [LeetCode] 130. Surrounded Regions Java

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

  3. [leetcode]Surrounded Regions @ Python

    原题地址:https://oj.leetcode.com/problems/surrounded-regions/ 题意: Given a 2D board containing 'X' and 'O ...

  4. LeetCode: Surrounded Regions [130]

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

  5. leetcode @python 130. Surrounded Regions

    题目链接 https://leetcode.com/problems/surrounded-regions/ 题目原文 Given a 2D board containing 'X' and 'O', ...

  6. LeetCode OJ - Surrounded Regions

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

  7. 130 Surrounded Regions

    题目链接:https://leetcode.com/problems/surrounded-regions/ 题目: Given a 2D board containing 'X' and 'O', ...

  8. LeetCode Surrounded Regions(floodfill算法)

    问题:一个由X,O字符填充的二维数组,问由O包围起来的X,其中O与边界的不计算在内. 思路:floodfill算法 ,先求出O构成的连通,将这些剔除后,就是由O包围起来的X 具体代码参考: https ...

  9. 利用BFS和DFS解决 LeetCode 130: Surrounded Regions

    问题来源 此题来源于LEETCODE,具体问题详见下面的链接 https://leetcode.com/problems/surrounded-regions/description/ 问题简述 给定 ...

  10. [LeetCode] Surrounded Regions, Solution

    http://fisherlei.blogspot.com/search?q=Surrounded+Region 特别说一下,里面实现bfs的code挺有意思的 int k =0; 20: while ...

最新文章

  1. 100%的程序员都想挑战的算法趣题!| 码书
  2. Intelij IDEA注册码生成代码
  3. Symbian的内存管理机制
  4. windows/Linux 安装 MongoDB 3.6.5
  5. boost::spirit模块演示了 AST 的生成,然后将其转储为人类可读的格式
  6. Google ARCore SDK
  7. mysql 8.0用doc修改密码_MYSQL8.0修改密码流程
  8. mysql快速上手3
  9. 四川大学计算机专业贵州分数线,四川大学2016年在贵州省高考各专业录取分数线...
  10. Federated Machine Learning: Concept and Applications
  11. elsevier模板_英文论文双栏模板
  12. TensorFlow模型保存和提取方法(含滑动平均模型)
  13. 清理300多台MySQL数据库的过期binlog日志
  14. 深入理解java:线程本地变量 java.lang.ThreadLocal类
  15. Windows下配置安装Git(一)
  16. 什么是电容式传感器?其工作原理是怎样的?
  17. 文具行业调研报告 - 市场现状分析与发展前景预测
  18. Java Web调用Matlab实现在线绘图
  19. tecplot无法处理高版本fluent导出的Ensight格式
  20. 万字长文:用Python轻轻松松操作Excel、Word、CSV!

热门文章

  1. 1、ShardingSphere基本概念
  2. python爬虫requests源码链家_python的爬虫项目(链家买二手房)
  3. 数据分析师可不是啥好工作
  4. Java中动态获取项目根目录和tomcat的绝对路径
  5. 搭建iscsi存储系统
  6. mysql一些操作个人备忘(持续更新)
  7. 手动整合ssh框架时的各种错误
  8. ViewPager用法
  9. PHP函数之CURL: 常见问题总结
  10. Apache Log4j 学习笔记