Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Testcase:
Input
[["X","O","X"],["X","O","X"],["X","O","X"]]
My Output:
[["X","O","X"],["O","O","O"],["X","O","X"]]
Expected Output:
[["X","O","X"],["X","O","X"],["X","O","X"]]


class Solution {
public:
    void dfs(int x,int y,vector<vector<char>>& board,int n,int m, vector<vector<int>> &vis,int &flag){
         if(board[x][y]=='X' || vis[x][y]){
            return;
        }
        if(x==0 || x==n-1 || y==0 || y==m-1 ){
            flag=0;
            return;
        }
        vis[x][y]=1;
        board[x][y]='X';
        dfs(x-1,y,board,n,m,vis,flag);
        if(!flag) board[x-1][y]='O';
        dfs(x+1,y,board,n,m,vis,flag);
        if(!flag) board[x+1][y]='O';
        dfs(x,y-1,board,n,m,vis,flag);
        if(!flag) board[x][y-1]='O';
        dfs(x,y+1,board,n,m,vis,flag);
        if(!flag) board[x][y+1]='O';
    }
    void solve(vector<vector<char>>& board) {
        int n=board.size();
        int m=board[0].size();
        int flag=1;
        vector<vector<int>> vis(n,vector<int>(m,0));
        for(int i=0;i<board.size();i++){
            for(int j=0;j<board[0].size();j++){
                if(board[i][j]=='O'){
                    if(!vis[i][j]){
                        dfs(i,j,board,n,m,vis,flag);
                        if(!flag) board[i][j]='O';
                        flag=1;
                    }
                }
            }
        }
    }
};


What I have tried:

Tried to dry run my code but couldn't find the error.
Posted
Updated 30-Jul-22 8:49am
Comments
merano99 29-Jul-22 15:39pm    
Neither the rules were explained nor is it clear how to call solve(), because the function main() is missing. It is not reasonable that we should find out the missing parts ourselves for testing.
Rick York 29-Jul-22 15:59pm    
My advice is to use a debugger. We could try to help with that but there is not enough code here to build an executable image.

Advice: give autonomous sample code, with a main and samp)le data without manual user input. So we can run your code directly, without any change by us.

Quote:
Why my code fails for the testcase discussed below? (Leetcode problem number 130)

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v2
I think your logic with the flag is wrong. In addition, the field should only be overwritten if all neighboring fields agree beforehand and no false is returned.
Note: The flag should be of type bool.

C++
// check if x is on the edge 
if (x == 0 || x == m_n - 1 || y == 0 || y == m_m - 1) {
  flag = false;       // do NOT overwite
  return;
}

m_vis[x][y] = 1;      // mark visited

dfs(x - 1, y, flag);
dfs(x + 1, y, flag);
dfs(x, y - 1, flag);
dfs(x, y + 1, flag);

if (flag)
  m_board[x][y] = 'X';  // overwrite

The variables board, vis, m, n should actually all be defined in the class and not always passed as parameters. This doesn't change the result, but what could be better readable, more maintainable and more performant.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900