LeetCode--Surrounded Regions

 1 class Solution {
 2 public:
 3     void solve(vector<vector<char>> &board) {
 4         int m = board.size();
 5         if(m == 0)
 6             return;
 7         int n = board[0].size();
 8         int i,j;
 9         for(i=0;i<m;i++)
10         {
11             if (board[i][0]==‘O‘)
12                 bfs(i,0,board);
13             if (board[i][n-1]==‘O‘)
14                 bfs(i,n-1,board);
15         }
16         for(j=0;j<n;j++)
17         {
18             if (board[0][j]==‘O‘)
19                 bfs(0,j,board);
20             if (board[m-1][j]==‘O‘)
21                 bfs(m-1,j,board);
22         }
23         for(i=0;i<m;i++)
24         {
25             for(j=0;j<n;j++)
26             {
27                 if(board[i][j]==‘O‘)
28                     board[i][j]=‘X‘;
29                 else if (board[i][j]==‘E‘)
30                     board[i][j]=‘O‘;
31             }
32         }
33     }
34     void bfs(int i,int j,vector<vector<char>> &board){
35         int m = board.size();
36         int n = board[0].size();
37
38         queue<pair<int,int> > Q;
39         Q.push(make_pair(i,j));
40         board[i][j] = ‘E‘;
41         int dx[4] = {-1,0,0,1};
42         int dy[4] = {0,-1,1,0};
43         while(!Q.empty()){
44             pair<int,int> p = Q.front();
45             Q.pop();
46
47             int iidx = p.first;
48             int jidx = p.second;
49
50             for(int i = 0 ; i < 4 ; ++i){
51                 int itmp = iidx+dx[i];
52                 int jtmp = jidx+dy[i];
53
54                 if((itmp < m && itmp >= 0)&&(jtmp < n && jtmp >= 0) && board[itmp][jtmp] == ‘O‘){
55                     Q.push(make_pair(itmp,jtmp));
56                     board[itmp][jtmp] = ‘E‘;
57                 }
58             }
59         }
60     }
61 };

dfs和bfs都可以

时间: 2024-10-14 01:34:39

LeetCode--Surrounded Regions的相关文章

LeetCode: Surrounded Regions [130]

[题目] 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(DFS、BFS)

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 Surrounded Regions 包围区域的DFS方法

在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ,一直百思不得其解其中的原因,直到有网友告诉我说他验证了最后一个大集合在本地机子上可以通过,那么我也来验证看看吧. class Solution { public: void solve(vector<vector<char> >& board) { for (int i =

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 XX O O XX X O XX O X XAfter running your function, the

[leetcode]Surrounded Regions @ Python

原题地址:https://oj.leetcode.com/problems/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

LEETCODE —— Surrounded Regions

Total Accepted: 43584 Total Submissions: 284350 Difficulty: Medium 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

[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 should

[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 should

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