I got WA on the last case?


  • 0
    T
    int dx[]={1,0,-1,0};
    int dy[]={0,1,0,-1};
    class Solution {
    public:
    void solve(vector<vector<char>> &board) {
        n=board.size();
        if(n)
            m=board[0].size();
        else
            return ;
        queue<int> q;
        bool *vis =new bool[n*m]; 
        memset(vis,0,sizeof(vis));
        for(int i=0;i<m;i++)
            if(board[0][i]=='O')
                q.push(i),vis[i]=1;
        for(int i=0;i<m;i++)
            if(board[n-1][i]=='O')
                q.push((n-1)*m+i),vis[(n-1)*m+i]=1;
        for(int i=1;i<n-1;i++){
            if(board[i][0]=='O')
                q.push(i*m),vis[i*m]=1;
            if(board[i][m-1]=='O')
                q.push(i*m+m-1),vis[i*m+m-1]=1;
        }
        while(!q.empty())
        {
            int x=q.front();
            q.pop();
            int y=x%m;
            x/=m;
            for(int i=0;i<4;i++)
            {
                int tx=x+dx[i];
                int ty=y+dy[i];
                if(Judge(tx,ty)&&board[tx][ty]=='O')
                {
                    if(!vis[tx*m+ty])
                        q.push(tx*m+ty),vis[tx*m+ty]=1;
                }
            } 
        }
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                if(board[i][j]!='X'&&!vis[i*m+j])
                    board[i][j]='X';
        return ;
    }
    private:
    int n,m;
    bool Judge(int x,int y)
    {
        if(x<0||x>=n||y<0||y>=m)
            return false;
        return true;
    }
    

    };

    i use the dfs?but i got WA on the last case.who can help me to solve this problem?


Log in to reply
 

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