why this wrong? C++ DFS


  • 0
    O

    void dfs(vector<string>& board,int i, int j)
    {
    if (i < 0 || i >= board.size()) return;
    if (j < 0 || j >= board[0].size()) return;

    if (board[i][j] == 'X'||board[i][j] == '$') return;
    board[i][j] = '$';
    
    dfs(board,  i - 1, j);
    dfs(board,  i + 1, j);
    dfs(board,  i, j - 1);
    dfs(board,  i, j + 1);
    

    }

    void solve(vector<string>& board)
    {
    if (board.empty() || board[0].empty()) return;
    int m = board.size(), n = board[0].size();

    for (int i = 0; i < m; i++)
    {
    	for (int j = 0; j < n; j++)
    	{
    		if (i==0||j==0||i==m-1||j==n-1)
    		{
    			if (board[i][j] == 'O') dfs(board, i, j);
    		}
    	}
    }
    cout << board[0][0] << endl;
    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';
    	}
    }
    

    }


Log in to reply
 

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