Surrounded Regions
/*** 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相关推荐
- 【BFS】LeetCode 130. Surrounded Regions
LeetCode 130. Surrounded Regions Solution1:我的答案 利用bfs解决,超过98.7%的答案,还不错.关键是简单易懂! class Solution { pub ...
- [LeetCode] 130. Surrounded Regions Java
题目:Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A ...
- [leetcode]Surrounded Regions @ Python
原题地址:https://oj.leetcode.com/problems/surrounded-regions/ 题意: Given a 2D board containing 'X' and 'O ...
- LeetCode: Surrounded Regions [130]
[题目] Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is cap ...
- leetcode @python 130. Surrounded Regions
题目链接 https://leetcode.com/problems/surrounded-regions/ 题目原文 Given a 2D board containing 'X' and 'O', ...
- LeetCode OJ - Surrounded Regions
题目: Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is capt ...
- 130 Surrounded Regions
题目链接:https://leetcode.com/problems/surrounded-regions/ 题目: Given a 2D board containing 'X' and 'O', ...
- LeetCode Surrounded Regions(floodfill算法)
问题:一个由X,O字符填充的二维数组,问由O包围起来的X,其中O与边界的不计算在内. 思路:floodfill算法 ,先求出O构成的连通,将这些剔除后,就是由O包围起来的X 具体代码参考: https ...
- 利用BFS和DFS解决 LeetCode 130: Surrounded Regions
问题来源 此题来源于LEETCODE,具体问题详见下面的链接 https://leetcode.com/problems/surrounded-regions/description/ 问题简述 给定 ...
- [LeetCode] Surrounded Regions, Solution
http://fisherlei.blogspot.com/search?q=Surrounded+Region 特别说一下,里面实现bfs的code挺有意思的 int k =0; 20: while ...
最新文章
- 100%的程序员都想挑战的算法趣题!| 码书
- Intelij IDEA注册码生成代码
- Symbian的内存管理机制
- windows/Linux 安装 MongoDB 3.6.5
- boost::spirit模块演示了 AST 的生成,然后将其转储为人类可读的格式
- Google ARCore SDK
- mysql 8.0用doc修改密码_MYSQL8.0修改密码流程
- mysql快速上手3
- 四川大学计算机专业贵州分数线,四川大学2016年在贵州省高考各专业录取分数线...
- Federated Machine Learning: Concept and Applications
- elsevier模板_英文论文双栏模板
- TensorFlow模型保存和提取方法(含滑动平均模型)
- 清理300多台MySQL数据库的过期binlog日志
- 深入理解java:线程本地变量 java.lang.ThreadLocal类
- Windows下配置安装Git(一)
- 什么是电容式传感器?其工作原理是怎样的?
- 文具行业调研报告 - 市场现状分析与发展前景预测
- Java Web调用Matlab实现在线绘图
- tecplot无法处理高版本fluent导出的Ensight格式
- 万字长文:用Python轻轻松松操作Excel、Word、CSV!