✔️ C++ solution | BFS
The better approach to this question is using breadth-first-search (BFS) than depth-first-search (DFS)\n\nC++ []\nclass Solution {\npublic:\n  int nearestExit(v
The better approach to this question is using breadth-first-search (BFS) than depth-first-search (DFS)\n\n``` C++ []\nclass Solution {\npublic:\n  int nearestExit(vector<vector<char>>& grid, vector<int>& e) {\n    int y= grid.size() ,yend= grid.size() - 1, x= grid[0].size() ,xend= grid[0].size() - 1, answer = 0;\n    \n        queue<tuple<int,int,int>> q;\n        q.push({e[0], e[1], 0});\n    \n        while(!q.empty()){\n          auto [cy,cx, w] = q.front(); q.pop();\n          if(grid[cy][cx] == \'+\') continue;\n          grid[cy][cx] = \'+\';\n          if(cy == 0 || cy == yend || cx == 0 || cx == xend ){\n            if(cy == e[0] && cx == e[1]);\n            else return w;\n          }\n          w++;\n          if(cy && grid[cy-1][cx] == \'.\') q.push({cy-1,cx,w});\n          if(cy != yend && grid[cy+1][cx] == \'.\') q.push({cy+1,cx,w});\n          if(cx && grid[cy][cx-1] == \'.\') q.push({cy,cx-1,w});\n          if(cx != xend && grid[cy][cx+1] == \'.\') q.push({cy,cx+1,w});\n        }\n      \n    \n    return -1;      \n  }\n};\n```\n\n*Upvote if you liked it*
['Breadth-First Search', 'C++']