Issues with DFS code identified!!! | C++ | Working + Not Working Solution
The solution below passes all the test cases.\n\n\nclass Solution {\npublic:   \n    typedef long long ll;\n    ll dp[105][105];\n    bool seen[105][105];\n    
The solution below passes all the test cases.\n\n```\nclass Solution {\npublic:   \n    typedef long long ll;\n    ll dp[105][105];\n    bool seen[105][105];\n    int x[4] = {1, -1, 0, 0};\n    int y[4] = {0, 0, 1, -1};\n    int srcx, srcy;\n    ll rec(vector<vector<char>>& arr, int row, int col){\n        \n        if((row != srcx or col != srcy) and (row == 0 or col == 0 or row == arr.size()-1 or col == arr[0].size()-1)) return 0;\n        \n        if(dp[row][col] != -1) return dp[row][col];\n        \n        seen[row][col] = true;\n        \n        ll ans = INT_MAX;\n        for(int i=0; i<4; i++){\n            int xx = row + x[i], yy = col + y[i];\n            if(xx < 0 or yy < 0 or xx >= arr.size() or yy >= arr[0].size() or arr[xx][yy] == \'+\' or seen[xx][yy]) continue;\n            ans = min(ans, 1 + rec(arr, row + x[i], col + y[i]));\n        }\n        \n        \n        seen[row][col] = false;\n        \n        \n        return dp[row][col] = ans;\n    }\n    \n    int nearestExit(vector<vector<char>>& arr, vector<int>& src) {\n        memset(dp, -1, sizeof(dp));\n        memset(seen, false, sizeof(seen));\n        srcx = src[0], srcy = src[1];\n        ll ans = rec(arr, src[0], src[1]);\n\n        if(ans >= INT_MAX) return -1;\n        return ans;\n    }\n};\n```\n\n**Twist**\nJust modify the below mentioned code fragment, which simply swaps the ```x[]``` and ```y[]``` array but the logic behind is intact. BANGG!!! The code no longer works !\n\n* Earlier:\n\n```\nint x[4] = {1, -1, 0, 0};\nint y[4] = {0, 0, 1, -1};\n```\n\n* Modified (Code not working):\n```\nint x[4] = {0, 0, 1, -1};\nint y[4] = {1, -1, 0, 0};\n```\n\nAlthough only 1 test case fails, with the above modification but still it gives us an intuition that DFS is not suitable for the forementioned problem, like it may work for most of the cases but it will fail in other cases, just like Greedy algorithm fails in some cases of Knapsack problem. So, the bottom line is to use BFS instead to get a correct answer always!
[]