博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode]Surrounded Regions
阅读量:4705 次
发布时间:2019-06-10

本文共 1660 字,大约阅读时间需要 5 分钟。

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 XX O O XX X O XX O X X

 

After running your function, the board should be:

X X X XX X X XX X X XX O X X
class Solution {public:    int dir[4][2] = {
{0,1},{0,-1},{1,0},{-1,0}}; void solveUtil(vector
>& board, vector
>& visited, int ix, int iy, vector
>& curPath, bool& isSurrounded) { int n = board.size(); int m = board[0].size(); queue
> posQ; posQ.push(pair
(ix, iy)); while(!posQ.empty()) { int curX = posQ.front().first; int curY = posQ.front().second; posQ.pop(); if(board[curX][curY] == 'O' && !visited[curX][curY]) { curPath.push_back(pair
(curX, curY)); visited[curX][curY] = true; for(int k = 0; k < 4; ++k) { int newX = curX+dir[k][0]; int newY = curY+dir[k][1]; if(newX < 0 || newX >= n || newY < 0 || newY >= m) isSurrounded = false; else if (!visited[newX][newY]) posQ.push(pair
(newX, newY)); } } } } void solve(vector
> &board) { // Start typing your C/C++ solution below // DO NOT write int main() function int n = board.size(); if(n == 0) return; int m = board[0].size(); vector
> visited(n, vector
(m, false)); for(int i = 0; i < n; ++i) { for(int j = 0; j < m; ++j) { vector
> curPath; bool isSurrounded = true; if(!visited[i][j]) solveUtil(board, visited, i, j, curPath, isSurrounded); if(isSurrounded) { for(int i = 0; i < curPath.size(); ++i) board[curPath[i].first][curPath[i].second] = 'X'; } } } }};

  

转载于:https://www.cnblogs.com/Rosanna/p/3598407.html

你可能感兴趣的文章
webform(四)简单控件
查看>>
验证码
查看>>
敏捷开发入门教程
查看>>
C#发现之旅(收藏)
查看>>
POJ1125 Stockbroker Grapevine 多源最短路
查看>>
HDU 2126 Buy the souvenirs
查看>>
顺序容器的insert使用方法
查看>>
Markdown的使用
查看>>
销售系统学习.mdl
查看>>
触发器
查看>>
mysql配置默认字符集为UTF8mb4
查看>>
WPF实现3D翻转的动画效果
查看>>
自定义圆环进度条
查看>>
UILayer
查看>>
复杂对象写入文件
查看>>
k8s-高级调度方式-二十一
查看>>
[HDU3555]Bomb
查看>>
基于dubbo的分布式系统(一)安装docker
查看>>
Recursion
查看>>
66. Plus One
查看>>