'+'와 '.'으로 이루어진 2차원 벡터와 시작 지점이 담긴 벡터를 받는다.
'+'는 갈 수 없는 벽을 나타내며, '.'은 이동할 수 있는 지점을 의미한다.
그리고 시작 지점으로부터 상하좌우 1칸씩 이동할 수 있으며,
시작 지점에서 시작하여 2차원 벡터의 가장자리까지 가는 가장 작은 스텝을 구하는 문제
단 시작 지점은 가장자리여도 출구가 아니라는 조건이 있다.
class Solution {
public:
int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {
const int DIRECTION = 4;
const int DIRECTION_X[DIRECTION]{0, 0, -1, 1};
const int DIRECTION_Y[DIRECTION]{-1, 1, 0, 0};
int maxY = maze.size();
int maxX = maze.back().size();
queue<pair<int, int>> booklist{};
bool visited[101][101]{};
pair<int, int> position{entrance[0], entrance[1]};
booklist.push(position);
visited[entrance[0]][entrance[1]] = true;
int result{};
while(booklist.empty() == false)
{
int booklistSize = booklist.size();
for (int i = 0; i < booklistSize; i++)
{
position = booklist.front();
booklist.pop();
for (int j = 0; j < DIRECTION; j++)
{
int nextX = position.second + DIRECTION_X[j];
int nextY = position.first + DIRECTION_Y[j];
if (nextX < 0 || maxX <= nextX || nextY < 0 || maxY <= nextY
|| maze[nextY][nextX] != '.')
{
continue;
}
if (visited[nextY][nextX])
{
continue;
}
visited[nextY][nextX] = true;
if (nextX == 0 || nextY == 0 || nextY == maxY - 1 || nextX == maxX - 1)
{
return result + 1;
}
booklist.push({nextY, nextX});
}
}
result++;
}
return -1;
}
};