-
Notifications
You must be signed in to change notification settings - Fork 0
/
boj16948.cpp
43 lines (32 loc) · 928 Bytes
/
boj16948.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
#include <bits/stdc++.h>
using namespace std;
int n, r1, c1, r2, c2;
bool visited[201][201];
int bfs(){
queue<tuple<int, int, int>> q;
q.push({0, r1, c1});
if (r1 == r2 && c1 == c2) return 0;
while (!q.empty()){
int depth = get<0>(q.front());
int y = get<1>(q.front());
int x = get<2>(q.front());
q.pop();
vector<pair<int, int>> v = {{y - 2, x - 1}, {y - 2, x + 1}, {y, x - 2}, {y, x + 2}, {y + 2, x - 1}, {y + 2, x + 1}};
for (auto [newy, newx] : v){
if (newy < 0 || newy >= n || newx < 0 || newx >= n) continue;
if (newy == r2 && newx == c2) return depth + 1;
if (!visited[newy][newx]){
q.push({depth + 1, newy, newx});
visited[newy][newx] = true;
}
}
}
return -1;
}
int main(){
ios_base::sync_with_stdio(0); // scanf 사용시 아래 두 문장 삭제
cin.tie(0);
cin >> n >> r1 >> c1 >> r2 >> c2;
cout << bfs();
return 0;
}