https://school.programmers.co.kr/learn/courses/30/lessons/1844
#include<vector>
#include <queue>
using namespace std;
bool visited[101][101] = {false, };
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
int result = 987654321;
void bfs(vector<vector<int>> maps){
queue<pair<pair<int,int>,int>>q;
q.push({{0,0},1});
visited[0][0] = true;
while(!q.empty()){
int qy = q.front().first.first;
int qx = q.front().first.second;
int counter = q.front().second;
q.pop();
if(qy == maps.size() - 1&&qx == maps[0].size() - 1){
result = min(result,counter);
}
for(int i = 0;i<4;i++){
int uy = qy + dy[i];
int ux = qx + dx[i];
if(uy < 0 || uy >= maps.size() || ux < 0 || ux >= maps[0].size())
continue;
if(maps[uy][ux] == 0)
continue;
if(!visited[uy][ux]){
q.push({{uy,ux},counter + 1});
visited[uy][ux] = true;
}
}
}
}
int solution(vector<vector<int> > maps)
{
int answer = 0;
bfs(maps);
answer = result;
if(answer == 987654321)
answer = -1;
return answer;
}
'Programmers Review' 카테고리의 다른 글
[Lv 2] 2xn 타일링 (0) | 2024.06.16 |
---|---|
[Lv 2] 124 나라의 숫자 (0) | 2024.06.16 |
[Lv 1] 달리기 경주 (0) | 2024.06.15 |
[Lv 1] 덧칠하기 (0) | 2024.06.13 |
[Lv 1] 대충 만든 자판 (0) | 2024.06.13 |