Rat chases its cheese

1 test case shows tle

Arjun, here you need to maintain a visited array as , and follow the approach as , :
bool visited[1001][1001]

ans[i][j]=1;
visited[i][j]=true;
bool rightSuccess,downSuccess,topSuccess,leftSuccess;

if(visited[i+1][j]==false && ans[i+1][j]==0)
{
    downSuccess=sol(a,ans,i+1,j,n,m);
}
if(visited[i][j+1]==false && ans[i][j+1]==0)
{
    rightSuccess=sol(a,ans,i,j+1,n,m);
}

if(ans[i-1][j]==0)
{
    topSuccess=sol(a,ans,i-1,j,n,m);
}
if(ans[i][j-1]==0)
{
    leftSuccess=sol(a,ans,i,j-1,n,m);
}
if(leftSuccess || rightSuccess || topSuccess || downSuccess)

{
return true;
}
ans[i][j]=0;//backtracking
return false;
}

but all test case are passing except one which is showing tle where is the error in the code

In the question, you are supposed to print a single path… So for large test case, you code is showing TLE, so to prevent that … I have told you the approach you need to follow up in the code along with wht you have exactly done… Kindly follow up that…