【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

用dfs试试,大数据时递归会溢出

 1 class Solution {
 2 public:
 3     void solve(vector<vector<char>> &board) {
 4         int m=board.size();
 5         if(m==0) return;
 6         int n=board[0].size();
 7
 8         vector<vector<bool>> visited(m,vector<bool>(n,false));
 9         for(int i=0;i<m;i++)
10         {
11             if(board[i][0]==‘O‘&&!visited[i][0])
12             {
13                 dfs(board,visited,i,0,m,n);
14             }
15
16             if(board[i][n-1]==‘O‘&&!visited[i][n-1])
17             {
18                 dfs(board,visited,i,n-1,m,n);
19             }
20         }
21
22         for(int j=0;j<n;j++)
23         {
24
25             if(board[0][j]==‘O‘&&!visited[0][j])
26             {
27                 dfs(board,visited,0,j,m,n);
28             }
29
30             if(board[m-1][j]==‘O‘&&!visited[m-1][j])
31             {
32                 dfs(board,visited,m-1,j,m,n);
33             }
34
35         }
36
37
38         for(int i=0;i<m;i++)
39         {
40             for(int j=0;j<n;j++)
41             {
42                 if(board[i][j]==‘O‘&&visited[i][j])
43                 {
44                     board[i][j]=‘X‘;
45                 }
46             }
47         }
48
49         return;
50     }
51
52     void dfs(vector<vector<char>> &board,vector<vector<bool>> &visited,int i,int j,int &m,int &n)
53     {
54         if(board[i][j]==‘O‘)
55         {
56             visited[i][j]=true;
57             if(i+1<m&&visited[i+1][j]==false)dfs(board,visited,i+1,j,m,n);
58             if(j+1<n&&visited[i][j+1]==false)dfs(board,visited,i,j+1,m,n);
59             if(i-1>=0&&visited[i-1][j]==false)dfs(board,visited,i-1,j,m,n);
60             if(j-1>=0&&visited[i][j-1]==false)dfs(board,visited,i,j-1,m,n);
61         }
62         else
63         {
64             return;
65         }
66     }
67 };

利用广度优先搜索,先把边界上的O全部放到队列中,然后搜索

 1 class Solution {
 2 public:
 3     void solve(vector<vector<char>> &board) {
 4         int m=board.size();
 5         if(m==0) return;
 6         int n=board[0].size();
 7
 8         queue<pair<int,int>> q;
 9
10         vector<vector<bool>> visited(m,vector<bool>(n,false));
11         for(int i=0;i<m;i++)
12         {
13             if(board[i][0]==‘O‘) q.push(pair<int,int>(i,0));
14             if(board[i][n-1]==‘O‘)  q.push(pair<int,int>(i,n-1));
15         }
16
17         for(int j=0;j<n;j++)
18         {
19             if(board[0][j]==‘O‘)  q.push(pair<int,int>(0,j));
20             if(board[m-1][j]==‘O‘)  q.push(pair<int,int>(m-1,j));
21         }
22
23         bfs(q,board,visited,m,n);
24
25         for(int i=0;i<m;i++)
26         {
27             for(int j=0;j<n;j++)
28             {
29                 if(!visited[i][j]&&board[i][j]==‘O‘)  board[i][j]=‘X‘;
30             }
31         }
32     }
33
34
35     void bfs(queue<pair<int,int>> &q,vector<vector<char>> &board,vector<vector<bool>> &visited,int &m,int &n)
36     {
37         while(!q.empty())
38         {
39             int ii=q.front().first;
40             int jj=q.front().second;
41             visited[ii][jj]=true;
42
43             q.pop();
44
45             if(ii+1<m&&!visited[ii+1][jj]&&board[ii+1][jj]==‘O‘) q.push(pair<int,int>(ii+1,jj));
46             if(ii-1>=0&&!visited[ii-1][jj]&&board[ii-1][jj]==‘O‘) q.push(pair<int,int>(ii-1,jj));
47             if(jj+1<n&&!visited[ii][jj+1]&&board[ii][jj+1]==‘O‘) q.push(pair<int,int>(ii,jj+1));
48             if(jj-1>=0&&!visited[ii][jj-1]&&board[ii][jj-1]==‘O‘) q.push(pair<int,int>(ii,jj-1));
49         }
50     }
51 };
时间: 2024-10-26 04:02:56

【leetcode】Surrounded Regions的相关文章

【LeetCode】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 sh

【LeetCode】 Surrounded Regions (BFS &amp;&amp; DFS)

题目:Surrounded Regions 广搜和深搜都能解决,但是LeetCode上使用深搜时会栈溢出 DFS: <span style="font-size:18px;">/*LeetCode Surrounded Regions * 题目:给定一个字符数组,由'X'和'O'组成,找到所有被x包围的o并将其替换为x * 思路:只要替换被包围的o就行,如果有一个o是边界或者上下左右中有一个是o且这个o不会被替换,则该点也不会被替换 * 从四条边开始,因为在这4周的一定不是

【leetcode】Surrounded Regions(middle)☆

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

【LeetCode】BFS(共43题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [101]Symmetric Tree 判断一棵树是不是对称. 题解:直接递归判断了,感觉和bfs没有什么强联系,当然如果你一定要用queue改写的话,勉强也能算bfs. // 这个题目的重点是 比较对象是 左子树的左儿子和右子树的右儿子, 左子树的右儿子和右子树的左儿子.不要搞错. // 直接中序遍历的话会有错的情况,最蠢的情况是数字标注改一改.. 1 /** 2

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

链接:https://leetcode.com/tag/union-find/ p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [128]Longest Consecutive Sequence  (2018年11月22日,开始解决hard题) 给了一个无序的数组,问这个数组里面的元素(可以重新排序)能组成的最长的连续子序列是多长.本题的时间复杂度要求是 O(N). 本题 array 专题里面有, 链接:https

【LeetCode】深搜DFS(共85题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [104]Maximum Depth of Binary Tree [105]Construct Binary Tree from Preorder and Inorder

LeetCode OJ - Surrounded Regions

我觉得这道题和传统的用动规或者贪心等算法的题目不同.按照题目的意思,就是将被'X'围绕的'O'区域找出来,然后覆盖成'X'. 那问题就变成两个子问题: 1. 找到'O'区域,可能有多个区域,每个区域'O'都是相连的: 2. 判断'O'区域是否是被'X'包围. 我采用树的宽度遍历的方法,找到每一个'O'区域,并为每个区域设置一个value值,为0或者1,1表示是被'X'包围,0则表示不是.是否被'X'包围就是看'O'区域的边界是否是在2D数组的边界上. 下面是具体的AC代码: class Boar

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n