-
Notifications
You must be signed in to change notification settings - Fork 0
/
chess.cpp
60 lines (54 loc) · 1.28 KB
/
chess.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
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<iostream>
#include<queue>
#include<stdio.h>
#include<string.h>
#include<map>
using namespace std;
pair<int, int> p, tp;
int nex[] = {2, 1, -1, -2, -2, -1, 1, 2};
int ney[] = {-1, -2, -2, -1, 1, 2, 2, 1};
int N, l, sx, sy, ex, ey, v[304][304] = {0}, s[304][304];
int bfs(int x, int y);
bool isafe(int x, int y);
int main()
{
scanf("%d", &N);
while(N--){
memset(v, 0, sizeof(v));
scanf("%d", &l);
scanf("%d %d %d %d", &sx, &sy, &ex, &ey);
int cnt = bfs(sx, sy);
cout << cnt << endl;
}
return 0;
}
int bfs(int x, int y){
queue<pair<int, int> > q;
q.push(make_pair(x, y));
v[y][x] = 1;
s[y][x] = 0;
while(!q.empty()){
tp = q.front();
q.pop();
if(tp.first == ex && tp.second == ey){
return s[tp.second][tp.first];
}
int nx, ny;
for(int i = 0; i < 8; i++){
nx = tp.first + nex[i];
ny = tp.second + ney[i];
if(isafe(nx, ny)){
q.push(make_pair(nx, ny));
v[ny][nx] = 1;
s[ny][nx] = s[tp.second][tp.first] + 1;
}
}
}
return 0;
}
bool isafe(int x, int y){
if(x < 0 || x >= l || v[y][x] || y < 0 || y >= l){
return false;
}
return true;
}