CPP | Faster than O(n^2) | Easy to Read
1926. Nearest Exit from Entrance in Maze\n#\n# CPP Code\n\nclass Solution {\npublic:\n    \n    vector<int> dx = {-1, 0, 1, 0};\n    vector<int> dy = {0, 1, 0, 
# 1926. Nearest Exit from Entrance in Maze\n#\n# CPP Code\n```\nclass Solution {\npublic:\n    \n    vector<int> dx = {-1, 0, 1, 0};\n    vector<int> dy = {0, 1, 0, -1};\n    \n    int nearestExit(vector<vector<char>>& a, vector<int>& st) {\n        queue<vector<int>> q;\n        st.push_back(0);\n        q.push(st);\n        a[st[0]][st[1]] = \'x\';\n        while (!q.empty()) {\n            int x = q.front()[0], y = q.front()[1], stp = q.front()[2];\n            q.pop();\n            for (int dir=0; dir<4; ++dir) {\n                int tx=x+dx[dir], ty=y+dy[dir];\n                if (tx>=0 && tx<a.size() && ty>=0 && ty<a[0].size() && a[tx][ty]==\'.\') {\n                    if (tx==0 || tx==a.size()-1 || ty==0 || ty==a[0].size()-1)\n                        return stp+1;\n                    q.push({tx, ty, stp+1});\n                    a[tx][ty] = \'x\';\n                }\n            }\n        }\n        return -1;\n    }\n};\n```
['C++']