leetcode21 surrounded regions
2024-08-29 23:15:57
题目描述
现在有一个仅包含‘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); } }
};
最新文章
- Jedis的使用
- 委托的N种写法,你喜欢哪种?
- 控制台手动编译Qt5程序
- sql server 日期相关操作
- java的system.arraycopy()方法
- 使用POI读取excel文件内容
- Kakfa揭秘 Day3 Kafka源码概述
- mini-httpd源码分析-mini-httpd.c
- 使用awk和grep做简单的统计
- 【ArcGIS 10.2新特性】ArcGIS 10.2 for Desktop 新特性(二)
- AspNet MVC3中过滤器 + 实例
- Codeforces Round #372 +#373 部分题解
- Windows下配置nginx+FastCgi + Spawn-fcgi
- 关于国际化时报org.springframework.context.NoSuchMessageException错,具体到No message found under code &#39;你的键名&#39; for locale &#39;zh_CN&#39;.的解决方案
- 学习python的第一天
- Redux进阶(像VUEX一样使用Redux)
- 游戏AI
- 利用Webpack+React(antd)+ES6+python(flask)实现代码转换
- JavaScript的Date类的函数特殊处理导致的问题
- emptyDir与hostPath
热门文章
- 启动你的Android应用:运行设备模拟器和调试代码(第3部分)
- 远程触发Jenkins的Pipeline任务的并发问题处理
- fio硬盘测速windows+linux
- 如何免费安装正版Adobe
- vue 组件的封装
- 多测师讲解自动化_rf框架搭建_高级讲师肖sir
- Git的介绍以及安装
- .net c#后台请求接口
- spring boot:使接口返回统一的RESTful格式数据(spring boot 2.3.1)
- spring boot:用redis+lua实现表单接口的幂等性(spring boot 2.2.0)