TLE in my solution


#1

Hi! Can you please check why does it give TLE for solution number 2:
bool issafe(vector<vector > A, int i, int j)
{
if(i>=0 && i<A.size() && j>=0 && j< A[0].size() && A[i][j] ==‘O’)
return true;
return false;
}
void dfs(vector<vector > &A, int i, int j)
{
A[i][j]=‘m’;
if(issafe(A, i+1, j))
{
dfs(A, i+1, j);
}
if(issafe(A, i-1, j))
{
dfs(A, i-1, j);
}
if(issafe(A, i, j+1))
{
dfs(A, i, j+1);
}
if(issafe(A, i, j-1))
{
dfs(A, i, j-1);
}

}
void Solution::solve(vector<vector > &A) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details

int m = A.size();
int n = A[0].size();

for(int i=0;i<m;i++)
{
    if(A[i][0]=='O')
        dfs(A,i,0);
    if(A[i][n-1]=='O')
        dfs(A,i,n-1);
}
for(int i=0;i<n;i++)
{
    if(A[0][i]=='O')
        dfs(A,0,i);
    if(A[m-1][i]=='O')
        dfs(A,m-1,i);
}

// for(int )
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(A[i][j]==‘m’)
A[i][j] = ‘O’;
else if(A[i][j]==‘O’)
A[i][j]= ‘X’;
}
}
}