Notice~An interestring question, why the first one can ac, while the other one can not?


  • 0
    1
    just change the if(dx>=0&&dy>=0&&dx<m-1&&dy<n-1&&board[dx][dy]=='O')  
    to  if(dx>=0&&dy>=0&&dx<m&&dy<n&&board[dx][dy]=='O')
    
    
    
    class Solution {
    public:
        void find_pool(int x,int y,vector<vector<char>>& board,int m,int n)
        {
            board[x][y]='A';
            int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
            for(int i=0;i<4;i++)
            {
                int dx=x+dir[i][0];
                int dy=y+dir[i][1];
                if(dx>=0&&dy>=0&&dx<m-1&&dy<n-1&&board[dx][dy]=='O') find_pool(dx,dy,board,m,n);
            }
        }
        void solve(vector<vector<char>>& board) {
            if(board.size()==0) return ;
            int m=board.size(),n=board[0].size();
            for(int i=0;i<n;i++)
            {
                if(board[0][i]=='O') find_pool(0,i,board,m,n);
                if(board[m-1][i]=='O') find_pool(m-1,i,board,m,n);
            }
            for(int i=1;i<m-1;i++)
            {
                if(board[i][0]=='O') find_pool(i,0,board,m,n);
                if(board[i][n-1]=='O') find_pool(i,n-1,board,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';
                    if(board[i][j]=='A') board[i][j]='O';
                }
            }
        }
    };
    
    
    
    
    class Solution {
    public:
        void find_pool(int x,int y,vector<vector<char>>& board,int m,int n)
        {
            board[x][y]='A';
            int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
            for(int i=0;i<4;i++)
            {
                int dx=x+dir[i][0];
                int dy=y+dir[i][1];
                if(dx>=0&&dy>=0&&dx<m&&dy<n&&board[dx][dy]=='O') find_pool(dx,dy,board,m,n);
            }
        }
        void solve(vector<vector<char>>& board) {
            if(board.size()==0) return ;
            int m=board.size(),n=board[0].size();
            for(int i=0;i<n;i++)
            {
                if(board[0][i]=='O') find_pool(0,i,board,m,n);
                if(board[m-1][i]=='O') find_pool(m-1,i,board,m,n);
            }
            for(int i=1;i<m-1;i++)
            {
                if(board[i][0]=='O') find_pool(i,0,board,m,n);
                if(board[i][n-1]=='O') find_pool(i,n-1,board,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';
                    if(board[i][j]=='A') board[i][j]='O';
                }
            }
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.