-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path241107-7562.java
59 lines (53 loc) · 1.94 KB
/
241107-7562.java
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
static int[][] visited;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
int[] dx = {1, 2, 2, 1, -1, -2, -2, -1};
int[] dy = {2, 1, -1, -2, -2, -1, 1, 2};
StringBuilder sb = new StringBuilder();
while (T-- > 0) {
int I = Integer.parseInt(br.readLine());
visited = new int[I][I];
StringTokenizer st = new StringTokenizer(br.readLine());
int fx = Integer.parseInt(st.nextToken());
int fy = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
int tx = Integer.parseInt(st.nextToken());
int ty = Integer.parseInt(st.nextToken());
Queue<Node> q = new ArrayDeque<>();
q.offer(new Node(fx, fy));
visited[fx][fy] = 1;
while (!q.isEmpty()) {
Node node = q.poll();
if (node.x == tx && node.y == ty) {
break;
}
for (int i = 0; i < 8; i++) {
int nx = node.x + dx[i];
int ny = node.y + dy[i];
if (nx >= 0 && nx < I && ny >= 0 && ny < I && visited[nx][ny] == 0) {
visited[nx][ny] = visited[node.x][node.y] + 1;
q.offer(new Node(nx, ny));
}
}
}
sb.append(visited[tx][ty] - 1).append("\n");
}
System.out.println(sb);
}
static class Node {
int x;
int y;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
}
}