-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLv4_게임맵최단거리.cpp
46 lines (35 loc) · 885 Bytes
/
Lv4_게임맵최단거리.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <vector>
#include <queue>
using namespace std;
// BFS
int solution(vector<vector<int> > maps)
{
int answer = 0;
int ey = maps.size();
int ex = maps[0].size();
queue<pair<int, int>> q; // (x,y) , 거리
q.push(make_pair(0,0)); // 시작점
while (!q.empty()) {
answer++;
int size = q.size();
for (int j = 0; j < size; j++) {
int x = q.front().second;
int y = q.front().first;
q.pop();
if (maps[y][x] == 0)
continue;
if (x == ex-1 && y == ey-1) // 도착
return answer;
maps[y][x] = 0;
if (x + 1 < ex && maps[y][x + 1] == 1)
q.push(make_pair(y, x + 1));
if (x - 1 > -1 && maps[y][x - 1] == 1)
q.push(make_pair(y, x - 1));
if (y + 1 < ey && maps[y + 1][x] == 1)
q.push(make_pair(y + 1, x));
if (y - 1 > -1 && maps[y - 1][x] == 1)
q.push(make_pair(y-1, x));
}
}
return -1;
}