【leetcode】Surrounded Regions
Surrounded Regions
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) {
- int m=board.size();
- if(m==) return;
- int n=board[].size();
- vector<vector<bool>> visited(m,vector<bool>(n,false));
- for(int i=;i<m;i++)
- {
- if(board[i][]=='O'&&!visited[i][])
- {
- dfs(board,visited,i,,m,n);
- }
- if(board[i][n-]=='O'&&!visited[i][n-])
- {
- dfs(board,visited,i,n-,m,n);
- }
- }
- for(int j=;j<n;j++)
- {
- if(board[][j]=='O'&&!visited[][j])
- {
- dfs(board,visited,,j,m,n);
- }
- if(board[m-][j]=='O'&&!visited[m-][j])
- {
- dfs(board,visited,m-,j,m,n);
- }
- }
- for(int i=;i<m;i++)
- {
- for(int j=;j<n;j++)
- {
- if(board[i][j]=='O'&&visited[i][j])
- {
- board[i][j]='X';
- }
- }
- }
- return;
- }
- void dfs(vector<vector<char>> &board,vector<vector<bool>> &visited,int i,int j,int &m,int &n)
- {
- if(board[i][j]=='O')
- {
- visited[i][j]=true;
- if(i+<m&&visited[i+][j]==false)dfs(board,visited,i+,j,m,n);
- if(j+<n&&visited[i][j+]==false)dfs(board,visited,i,j+,m,n);
- if(i->=&&visited[i-][j]==false)dfs(board,visited,i-,j,m,n);
- if(j->=&&visited[i][j-]==false)dfs(board,visited,i,j-,m,n);
- }
- else
- {
- return;
- }
- }
- };
- class Solution {
- public:
- void solve(vector<vector<char>> &board) {
- int m=board.size();
- if(m==) return;
- int n=board[].size();
- queue<pair<int,int>> q;
- vector<vector<bool>> visited(m,vector<bool>(n,false));
- for(int i=;i<m;i++)
- {
- if(board[i][]=='O') q.push(pair<int,int>(i,));
- if(board[i][n-]=='O') q.push(pair<int,int>(i,n-));
- }
- for(int j=;j<n;j++)
- {
- if(board[][j]=='O') q.push(pair<int,int>(,j));
- if(board[m-][j]=='O') q.push(pair<int,int>(m-,j));
- }
- bfs(q,board,visited,m,n);
- for(int i=;i<m;i++)
- {
- for(int j=;j<n;j++)
- {
- if(!visited[i][j]&&board[i][j]=='O') board[i][j]='X';
- }
- }
- }
- void bfs(queue<pair<int,int>> &q,vector<vector<char>> &board,vector<vector<bool>> &visited,int &m,int &n)
- {
- while(!q.empty())
- {
- int ii=q.front().first;
- int jj=q.front().second;
- visited[ii][jj]=true;
- q.pop();
- if(ii+<m&&!visited[ii+][jj]&&board[ii+][jj]=='O') q.push(pair<int,int>(ii+,jj));
- if(ii->=&&!visited[ii-][jj]&&board[ii-][jj]=='O') q.push(pair<int,int>(ii-,jj));
- if(jj+<n&&!visited[ii][jj+]&&board[ii][jj+]=='O') q.push(pair<int,int>(ii,jj+));
- if(jj->=&&!visited[ii][jj-]&&board[ii][jj-]=='O') q.push(pair<int,int>(ii,jj-));
- }
- }
- };
【leetcode】Surrounded Regions的更多相关文章
- 【leetcode】Surrounded Regions(middle)☆
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- 【LeetCode】959. Regions Cut By Slashes 由斜杠划分区域(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 日期 题目地址:https://leetcod ...
- 【leetcode】959. Regions Cut By Slashes
题目如下: In a N x N grid composed of 1 x 1 squares, each 1 x 1 square consists of a /, \, or blank spac ...
- 【LeetCode】BFS(共43题)
[101]Symmetric Tree 判断一棵树是不是对称. 题解:直接递归判断了,感觉和bfs没有什么强联系,当然如果你一定要用queue改写的话,勉强也能算bfs. // 这个题目的重点是 比较 ...
- 【LeetCode】并查集 union-find(共16题)
链接:https://leetcode.com/tag/union-find/ [128]Longest Consecutive Sequence (2018年11月22日,开始解决hard题) 给 ...
- 【LeetCode】深搜DFS(共85题)
[98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [1 ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
随机推荐
- ASP.NET Padding Oracle Attack EXP
#!/usr/bin/perl## PadBuster v0.3 - Automated script for performing Padding Oracle attacks# Brian Hol ...
- 递归函数解决n到m之间求和问题
int main() { int n,m; ; scanf("%d %d",&n,&m); result=fun(n,m); printf("%d&quo ...
- JSP的原理
一.什么是JSP JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术. JSP这门技术的最大特点在于,写JSP就行html, ...
- 二叉排序树(Binary Sort Tree)
参考文章:http://blog.csdn.net/ns_code/article/details/19823463 不过博主的使用第一种方法操作后的树已经不是二叉排序树了,值得深思!! #inclu ...
- jquery ajax 提交 FormData
$('form').submit(function(){ var formdata=new FormData(this); $.ajax({ type:'POST', url:'/url/path', ...
- CSS 改变文本选中颜色
改变文字颜色 ::selection { background: #f88; text-shadow: none; color: #000;}::-moz-selection { ...
- iOS-设置UIPageControl 显示图片
UIPageControl 的默认样式是几个小圆点,系统没有提供属性供我们自定义这几个小圆点的样式,不过我们依然可以使用KVC来自定义PageControl的显示样式 UIPageControl *p ...
- [Angularjs]表单验证
写在前面 在开发中提交表单,并对表单的值进行验证是非常常见的操作,angularjs对表单验证提供了非常好的支持. demo 表单 <form name="myform" n ...
- 【帖子】怎么彻底删除kafka的topic,然后重建?
怎么彻底删除kafka的topic,然后重建? 网上都说用kafka-run-class.shkafka.admin.DeleteTopicCommand 命令删除topic,但是并没有成功,用kaf ...
- PHP常用的一些正则表达式
附一些常用的正则运算: 验证数字:^[0-9]*$验证n位的数字:^\d{n}$验证至少n位数字:^\d{n,}$验证m-n位的数字:^\d{m,n}$验证零和非零开头的数字:^(0|[1-9][0- ...