leetcode130

C++实现,使用BFS:

struct POS
{
    int x;
    int y;
    POS(int newx, int newy): x(newx), y(newy) {}
};

class Solution {
public:
    void solve(vector<vector<char>> &board) {
        if(board.empty() || board[0].empty())
            return;
        int m = board.size();
        int n = board[0].size();
        for(int i = 0; i < m; i ++)
        {
            for(int j = 0; j < n; j ++)
            {
                if(board[i][j] == 'O')
                {
                    if(i == 0 || i == m-1 || j == 0 || j == n-1)
                    {// remain 'O' on the boundry
                        bfs(board, i, j, m, n);
                    }
                }
            }
        }
        for(int i = 0; i < m; i ++)
        {
            for(int j = 0; j < n; j ++)
            {
                if(board[i][j] == 'O')
                    board[i][j] = 'X';
                else if(board[i][j] == '*')
                    board[i][j] = 'O';
            }
        }
    }
    void bfs(vector<vector<char>> &board, int i, int j, int m, int n)
    {
        stack<POS*> stk;
        POS* pos = new POS(i, j);
        stk.push(pos);
        board[i][j] = '*';
        while(!stk.empty())
        {
            POS* top = stk.top();
            if(top->x > 0 && board[top->x-1][top->y] == 'O')
            {
                POS* up = new POS(top->x-1, top->y);
                stk.push(up);
                board[up->x][up->y] = '*';
                continue;
            }
            if(top->x < m-1 && board[top->x+1][top->y] == 'O')
            {
                POS* down = new POS(top->x+1, top->y);
                stk.push(down);
                board[down->x][down->y] = '*';
                continue;
            }
            if(top->y > 0 && board[top->x][top->y-1] == 'O')
            {
                POS* left = new POS(top->x, top->y-1);
                stk.push(left);
                board[left->x][left->y] = '*';
                continue;
            }
            if(top->y < n-1 && board[top->x][top->y+1] == 'O')
            {
                POS* right = new POS(top->x, top->y+1);
                stk.push(right);
                board[right->x][right->y] = '*';
                continue;
            }
            stk.pop();
        }
    }
};

补充一个python的实现,使用DFS:

 1 class Solution:
 2     def dfs(self,board,i,j,m,n,visited,direction):
 3         if i < 0 or i >= m or j < 0 or j >= n or visited[i][j] == 1:
 4             return
 5         if board[i][j] == 'O':
 6             board[i][j] = '*'
 7             visited[i][j] = 1
 8 
 9             for dirt in direction:
10                 if dirt[0] == 0 and dirt[1] == 0:
11                     continue
12                 x = i + dirt[0]
13                 y = j + dirt[1]
14                 self.dfs(board,x,y,m,n,visited,direction)
15         
16         
17     def solve(self, board: 'List[List[str]]') -> None:
18         m = len(board)#
19         if m == 0:
20             return
21         n = len(board[0])#
22         if n == 0:
23             return
24         visited = [[0 for _ in range(n)]for _ in range(m)]
25         direction = [[-1,0],[1,0],[0,-1],[0,1]]
26         for i in range(m):
27             for j in range(n):
28                 if board[i][j] == 'O' and (i == 0 or i == m-1 or j == 0 or j == n-1):
29                     self.dfs(board,i,j,m,n,visited,direction)
30         
31         for i in range(m):
32             for j in range(n):
33                 if board[i][j] == 'O':
34                     board[i][j] = 'X'
35                 elif board[i][j] == '*':
36                     board[i][j] = 'O'
原文地址:https://www.cnblogs.com/asenyang/p/9826824.html