Can Anyone pls Explain


#1

Pls anyone explain how the DFS will give me TLE but BFS is working fine.
Wrote this beauty but alas it doesnt work

int dx[]= {1,0,-1,0};
int dy[]= {0,1,0,-1};

void helper(int i, int j, int p, int q ,vector<string> &C ,vector<vector<bool>> vis,int step, int &ans)
{
    if(step>ans) return;
    if(i==p && j==q)
    {
        cout<<"the value of step: " << step << endl;
        ans = min(ans,step);
        return;
    }
    cout<< "the value of i and j: " << i << " " << j << " value of C[i][j] " << C[i][j] << endl; 
    vis[i][j]= true;
    for(int k=0;k<4;k++)
    {
        int x = i+dx[k];
        int y = j+dy[k];
        if(x >=0 && x<=p && y>=0 && y<=q && vis[x][y]==false && step < ans)
        {
            if((k==0 && C[i][j]=='D') || (k==1 && C[i][j]=='R') || (k==2 && C[i][j]=='U') || (k==3 && C[i][j]=='L') ) 
            { 
                helper(x,y,p,q,C,vis,step,ans); 
            }
            else
            {
                helper(x,y,p,q,C,vis,step+1,ans);
            }
        }
    }
    vis[i][j] = false;
}


int Solution::solve(int A, int B, vector<string> &C) {
    vector<vector<bool>> vis(A,vector<bool>(B,false));
    int ans = INT_MAX;
    helper(0,0,A-1,B-1,C,vis,0,ans);
    return ans;
}