-
Notifications
You must be signed in to change notification settings - Fork 1
/
7562.cpp
44 lines (41 loc) · 1.18 KB
/
7562.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
#include<iostream>
#include<queue>
using namespace std;
int main()
{
int n; cin >> n;
while (n--)
{
int sz, now[2], des[2];
cin >> sz >> now[0] >> now[1] >> des[0] >> des[1];
bool chk[305][305] = { 0, };
int idx = 0, cnt = 0;
queue<pair<int, int>> q[2];
q[idx].push(make_pair(now[0], now[1]));
while (1)
{
while (!q[idx].empty())
{
int x = q[idx].front().first;
int y = q[idx].front().second;
q[idx].pop();
if (chk[y][x]) continue;
if (des[0] == x && des[1] == y) goto end;
chk[y][x] = true;
if (x > 1 && y) q[!idx].push(make_pair(x - 2, y - 1));
if (x && y > 1) q[!idx].push(make_pair(x - 1, y - 2));
if (x < sz - 1 && y > 1) q[!idx].push(make_pair(x + 1, y - 2));
if (x < sz - 2 && y) q[!idx].push(make_pair(x + 2, y - 1));
if (x > 1 && y < sz - 1) q[!idx].push(make_pair(x - 2, y + 1));
if (x && y < sz - 2) q[!idx].push(make_pair(x - 1, y + 2));
if (x < sz - 1 && y < sz - 2) q[!idx].push(make_pair(x + 1, y + 2));
if (x < sz - 2 && y < sz - 1) q[!idx].push(make_pair(x + 2, y + 1));
}
idx ? idx = 0 : idx = 1;
cnt++;
}
end:
cout << cnt << endl;
}
return 0;
}