-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7562.cpp
More file actions
49 lines (47 loc) · 1.32 KB
/
7562.cpp
File metadata and controls
49 lines (47 loc) · 1.32 KB
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
48
49
#include <bits/stdc++.h>
using namespace std;
const int dy[8] = {-2, -1, 1, 2, -2, -1, 1, 2};
const int dx[8] = {-1, -2, -2, -1, 1, 2, 2, 1};
int visited[300][300];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
for (int t = 0; t < T; t++) {
int I;
cin >> I;
int sy, sx, ey, ex;
cin >> sy >> sx >> ey >> ex;
queue<int> Q;
Q.push(sy * I + sx);
visited[sy][sx] = true;
int cnt = 0;
while (1) {
int find = 0;
for (int i = 0; i < Q.size(); i++) {
int cury = Q.front() / I;
int curx = Q.front() % I;
Q.pop();
if (cury == ey && curx == ex) {
cout << cnt;
find = 0;
break;
}
for (int dir = 0; dir < 8; dir++) {
int nr = cury + dy[dir];
int nc = curx + dx[dir];
if (nr < 0 || nr >= I || nc < 0 || nc >= I)
continue;
if (visited[nr][nc])
continue;
visited[nr][nc] = 1;
Q.push(nr * I + nc);
}
}
if (find)
break;
cnt++;
}
}
}