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,

  1. X X X X
  2. X O O X
  3. X X O X
  4. X O X X

After running your function, the board should be:

  1. X X X X
  2. X X X X
  3. X X X X
  4. X O X X
 
 
用dfs试试,大数据时递归会溢出
  1. class Solution {
  2. public:
  3. void solve(vector<vector<char>> &board) {
  4. int m=board.size();
  5. if(m==) return;
  6. int n=board[].size();
  7.  
  8. vector<vector<bool>> visited(m,vector<bool>(n,false));
  9. for(int i=;i<m;i++)
  10. {
  11. if(board[i][]=='O'&&!visited[i][])
  12. {
  13. dfs(board,visited,i,,m,n);
  14. }
  15.  
  16. if(board[i][n-]=='O'&&!visited[i][n-])
  17. {
  18. dfs(board,visited,i,n-,m,n);
  19. }
  20. }
  21.  
  22. for(int j=;j<n;j++)
  23. {
  24.  
  25. if(board[][j]=='O'&&!visited[][j])
  26. {
  27. dfs(board,visited,,j,m,n);
  28. }
  29.  
  30. if(board[m-][j]=='O'&&!visited[m-][j])
  31. {
  32. dfs(board,visited,m-,j,m,n);
  33. }
  34.  
  35. }
  36.  
  37. for(int i=;i<m;i++)
  38. {
  39. for(int j=;j<n;j++)
  40. {
  41. if(board[i][j]=='O'&&visited[i][j])
  42. {
  43. board[i][j]='X';
  44. }
  45. }
  46. }
  47.  
  48. return;
  49. }
  50.  
  51. void dfs(vector<vector<char>> &board,vector<vector<bool>> &visited,int i,int j,int &m,int &n)
  52. {
  53. if(board[i][j]=='O')
  54. {
  55. visited[i][j]=true;
  56. if(i+<m&&visited[i+][j]==false)dfs(board,visited,i+,j,m,n);
  57. if(j+<n&&visited[i][j+]==false)dfs(board,visited,i,j+,m,n);
  58. if(i->=&&visited[i-][j]==false)dfs(board,visited,i-,j,m,n);
  59. if(j->=&&visited[i][j-]==false)dfs(board,visited,i,j-,m,n);
  60. }
  61. else
  62. {
  63. return;
  64. }
  65. }
  66. };
利用广度优先搜索,先把边界上的O全部放到队列中,然后搜索
 
  1. class Solution {
  2. public:
  3. void solve(vector<vector<char>> &board) {
  4. int m=board.size();
  5. if(m==) return;
  6. int n=board[].size();
  7.  
  8. queue<pair<int,int>> q;
  9.  
  10. vector<vector<bool>> visited(m,vector<bool>(n,false));
  11. for(int i=;i<m;i++)
  12. {
  13. if(board[i][]=='O') q.push(pair<int,int>(i,));
  14. if(board[i][n-]=='O') q.push(pair<int,int>(i,n-));
  15. }
  16.  
  17. for(int j=;j<n;j++)
  18. {
  19. if(board[][j]=='O') q.push(pair<int,int>(,j));
  20. if(board[m-][j]=='O') q.push(pair<int,int>(m-,j));
  21. }
  22.  
  23. bfs(q,board,visited,m,n);
  24.  
  25. for(int i=;i<m;i++)
  26. {
  27. for(int j=;j<n;j++)
  28. {
  29. if(!visited[i][j]&&board[i][j]=='O') board[i][j]='X';
  30. }
  31. }
  32. }
  33.  
  34. void bfs(queue<pair<int,int>> &q,vector<vector<char>> &board,vector<vector<bool>> &visited,int &m,int &n)
  35. {
  36. while(!q.empty())
  37. {
  38. int ii=q.front().first;
  39. int jj=q.front().second;
  40. visited[ii][jj]=true;
  41.  
  42. q.pop();
  43.  
  44. if(ii+<m&&!visited[ii+][jj]&&board[ii+][jj]=='O') q.push(pair<int,int>(ii+,jj));
  45. if(ii->=&&!visited[ii-][jj]&&board[ii-][jj]=='O') q.push(pair<int,int>(ii-,jj));
  46. if(jj+<n&&!visited[ii][jj+]&&board[ii][jj+]=='O') q.push(pair<int,int>(ii,jj+));
  47. if(jj->=&&!visited[ii][jj-]&&board[ii][jj-]=='O') q.push(pair<int,int>(ii,jj-));
  48. }
  49. }
  50. };

【leetcode】Surrounded Regions的更多相关文章

  1. 【leetcode】Surrounded Regions(middle)☆

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

  2. 【LeetCode】959. Regions Cut By Slashes 由斜杠划分区域(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 日期 题目地址:https://leetcod ...

  3. 【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 ...

  4. 【LeetCode】BFS(共43题)

    [101]Symmetric Tree 判断一棵树是不是对称. 题解:直接递归判断了,感觉和bfs没有什么强联系,当然如果你一定要用queue改写的话,勉强也能算bfs. // 这个题目的重点是 比较 ...

  5. 【LeetCode】并查集 union-find(共16题)

    链接:https://leetcode.com/tag/union-find/ [128]Longest Consecutive Sequence  (2018年11月22日,开始解决hard题) 给 ...

  6. 【LeetCode】深搜DFS(共85题)

    [98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [1 ...

  7. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  8. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  9. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

随机推荐

  1. ASP.NET Padding Oracle Attack EXP

    #!/usr/bin/perl## PadBuster v0.3 - Automated script for performing Padding Oracle attacks# Brian Hol ...

  2. 递归函数解决n到m之间求和问题

    int main() { int n,m; ; scanf("%d %d",&n,&m); result=fun(n,m); printf("%d&quo ...

  3. JSP的原理

    一.什么是JSP JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术. JSP这门技术的最大特点在于,写JSP就行html, ...

  4. 二叉排序树(Binary Sort Tree)

    参考文章:http://blog.csdn.net/ns_code/article/details/19823463 不过博主的使用第一种方法操作后的树已经不是二叉排序树了,值得深思!! #inclu ...

  5. jquery ajax 提交 FormData

    $('form').submit(function(){ var formdata=new FormData(this); $.ajax({ type:'POST', url:'/url/path', ...

  6. CSS 改变文本选中颜色

    改变文字颜色 ::selection {    background: #f88;    text-shadow: none;    color: #000;}::-moz-selection {  ...

  7. iOS-设置UIPageControl 显示图片

    UIPageControl 的默认样式是几个小圆点,系统没有提供属性供我们自定义这几个小圆点的样式,不过我们依然可以使用KVC来自定义PageControl的显示样式 UIPageControl *p ...

  8. [Angularjs]表单验证

    写在前面 在开发中提交表单,并对表单的值进行验证是非常常见的操作,angularjs对表单验证提供了非常好的支持. demo 表单 <form name="myform" n ...

  9. 【帖子】怎么彻底删除kafka的topic,然后重建?

    怎么彻底删除kafka的topic,然后重建? 网上都说用kafka-run-class.shkafka.admin.DeleteTopicCommand 命令删除topic,但是并没有成功,用kaf ...

  10. PHP常用的一些正则表达式

    附一些常用的正则运算: 验证数字:^[0-9]*$验证n位的数字:^\d{n}$验证至少n位数字:^\d{n,}$验证m-n位的数字:^\d{m,n}$验证零和非零开头的数字:^(0|[1-9][0- ...