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

 

你可能感兴趣的:(LeetCode)