Skip to content

top145-#130 #7

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
LionCoder4ever opened this issue Oct 2, 2021 · 0 comments
Open

top145-#130 #7

LionCoder4ever opened this issue Oct 2, 2021 · 0 comments

Comments

@LionCoder4ever
Copy link
Owner

  • dfs
  • ans vector mark the outter O and the "connected" O
class Solution {
public:
    void solve(vector<vector<char>>& board) {
        int m = board.size();
        int n = board[0].size();
        vector<vector<bool>> ans(0);
        for(int i=0;i<m;i++) {
            vector<bool> item(0);
            for(int j=0;j<n;j++) {
                item.push_back(false);
            }
            ans.push_back(item);
        }

        for(int i =0;i<m;i++) 
            for(int j=0;j<n;j++) 
                if (board[i][j] == 'O' && (i==0 || i == m-1 || j == 0 || j == n-1)) ans[i][j] = true;

        for(int i =0;i<m;i++) {
            for(int j=0;j<n;j++) {
                if (ans[i][j]) {
                    if(i==0) {
                        dfs(ans, board, i+1,j, m,n);
                    }
                    if(i==m-1) {
                        dfs(ans, board, i-1,j, m,n);
                    }
                    if (j==0) {
                        dfs(ans, board, i,j+1, m,n);
                    }
                    if (j==n-1) {
                        dfs(ans, board, i,j-1, m,n);
                    }
                }
            }
        }
       
        for(int i=1;i<m-1;i++) {
            for(int j=1;j<n-1;j++) {
                char x = board[i][j];
                if (x=='O' && !ans[i][j]) {
                    board[i][j] = 'X';
                }
            }
        }
    }
    void dfs(vector<vector<bool>>& ans, vector<vector<char>>& board, int i, int j, int m,int n) {
        // cout << i << "====" << j << endl;
        if (i <= 0 || i >= m-1) return;
        if (j <= 0 || j >= n-1) return;
        if (board[i][j] == 'X') return;
        if (ans[i][j]) return;
        if (board[i][j] == 'O') {
            ans[i][j] = true;
        }
        dfs(ans, board, i-1,j,m,n);
        dfs(ans, board, i+1,j,m,n);
        dfs(ans, board, i,j-1,m,n);
        dfs(ans, board, i,j+1,m,n);
    }
};

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant