-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs_MovingKnight.cpp
More file actions
73 lines (57 loc) · 1.21 KB
/
bfs_MovingKnight.cpp
File metadata and controls
73 lines (57 loc) · 1.21 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <bits/stdc++.h>
// F_I 사용하면 cin 과 scanf 를 섞어서 쓰면 안된다!
#define F_I ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pl;
typedef pair<int, int> pi;
ll Min(ll a, ll b) { return (a < b) ? a : b; }
ll Max(ll a, ll b) { return (a < b) ? b : a; }
ll gcd(ll m, ll n) { if (n == 0) return m; return gcd(n, m % n); } //최대공약수
ll lcm(ll m, ll n) { return m * n / gcd(m, n); } //최소공배수
int dx[8] = { -2,-2,-1,-1,1,1,2,2 };
int dy[8] = { 1,-1,2,-2,2,-2,1,-1 };
void solve()
{
int n;
cin >> n;
vector< vector<int> > arr(n, vector<int>(n,-1));
int a,b;
cin >> a >> b;
arr[a][b] = 0;
queue<pi> q;
q.push(make_pair(a, b));
while (!q.empty())
{
int x, y;
tie(x, y) = q.front();
q.pop();
for (int i = 0; i < 8; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && nx < n && ny >= 0 && ny < n)
{
if (arr[nx][ny] == -1)
{
arr[nx][ny] = arr[x][y] + 1;
q.push(make_pair(nx, ny));
}
}
}
}
int a1, b1;
cin >> a1 >> b1;
cout << arr[a1][b1] << '\n';
return;
}
int main()
{
F_I;
//[백준] 7562번 : 나이트의 이동 (bfs)
int tc;
cin >> tc;
while (tc--)
solve();
return 0;
}