-
Notifications
You must be signed in to change notification settings - Fork 1
/
07562.cpp
47 lines (40 loc) · 1 KB
/
07562.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
47
#include <iostream>
#include <utility>
#include <vector>
#include <queue>
using namespace std;
int dx[] = {-2, -2, -1, -1, 1, 1, 2, 2};
int dy[] = {-1, 1, -2, 2, -2, 2, -1, 1};
void solve(void) {
int l;
pair<int, int> start, end;
cin >> l;
cin >> start.first >> start.second;
cin >> end.first >> end.second;
vector<vector<int> > ans(l, vector<int>(l, -1));
ans[start.first][start.second] = 0;
queue<pair<int, int> > q;
q.push(start);
while (!q.empty()) {
int x = q.front().first, y = q.front().second;
q.pop();
if (x == end.first && y == end.second) break;
for (int i=0; i<8; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (nx < 0 || nx >= l || ny < 0 || ny >= l || ans[nx][ny] != -1) continue;
ans[nx][ny] = ans[x][y] + 1;
q.push(make_pair(nx, ny));
}
}
cout << ans[end.first][end.second] << "\n";
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}