package CaptureKnight;
import java.io.FileInputStream;
import java.util.Scanner;
public class CaptureKnight {
static int front, rear;
static Node1[] queue;
static int test_case;
static int[][] arr_result;
static int n, m;
static int start_x, start_y;
static int end_x, end_y;
static int[][] mang = { { -2, 1 }, { 2, 1 }, { -2, -1 }, { 2, -1 },
{ 1, -2 }, { 1, 2 }, { -1, -2 }, { -1, 2 } };
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("CaptureKnight.txt"));
Scanner sc = new Scanner(System.in);
test_case = sc.nextInt();
for (int tc = 1; tc <= test_case; tc++) {
n = sc.nextInt();
m = sc.nextInt();
front = -1;
rear = -1;
queue = new Node1[n * m];
arr_result = new int[n + 1][m + 1];
for (int i = 0; i < n + 1; i++) {
for (int j = 0; j < m + 1; j++) {
arr_result[i][j] = -1;
}
}
start_x = sc.nextInt();
start_y = sc.nextInt();
end_x = sc.nextInt();
end_y = sc.nextInt();
Node1 node = new Node1(start_x, start_y);
BFS(node);
System.out.println("Case" + " #" + tc);
System.out.println(arr_result[end_x][end_y]);
}
}
public static void BFS(Node1 node_start) {
Node1 node;
arr_result[node_start.x][node_start.y] = 0;
enQueue(node_start);
while (!isEmpty()) {
node = queue[front];
for (int i = 0; i < 8; i++) {
int x = node.x + mang[i][0];
int y = node.y + mang[i][1];
if (x > 0 && x <= n && y > 0 && y <= m) {
if (arr_result[x][y] == -1) {
Node1 n = new Node1(x, y);
enQueue(n);
arr_result[n.x][n.y] = arr_result[node.x][node.y] + 1;
}
}
}
deQueue();
}
}
public static boolean isEmpty() {
if (front == -1 && rear == -1) {
return true;
}
return false;
}
public static void deQueue() {
if (isEmpty()) {
return;
} else if (front == rear) {
front = rear = -1;
} else {
front += 1;
}
}
public static void enQueue(Node1 node1) {
if (isFull()) {
return;
} else if (isEmpty()) {
front = rear = 0;
} else {
rear = rear + 1;
}
queue[rear] = node1;
}
public static boolean isFull() {
if (rear + 1 == queue.length) {
return true;
}
return false;
}
}
class Node1 {
int x;
int y;
public Node1(int x, int y) {
this.x = x;
this.y = y;
}
}
50
9 9
2 2 4 4
20 20
2 3 7 9
5 5
1 1 2 2
522 55
292 23 84 6
176 361
110 244 118 191
870 435
352 166 93 238
67 192
23 108 16 179
63 521
7 397 45 182
136 738
100 613 46 351
599 504
401 52 561 216
620 639
412 330 185 323
767 206
659 191 710 26
745 467
681 447 392 382
601 607
332 467 53 249
593 604
271 182 35 231
787 404
193 168 121 121
537 793
487 328 354 665
42 685
7 281 6 15
37 105
28 23 35 21
506 367
316 319 340 350
67 606
37 226 23 44
718 586
255 252 517 359
824 283
735 262 734 236
304 452
62 99 228 16
431 259
236 256 262 252
83 550
4 439 8 134
423 358
331 78 222 232
89 663
84 518 35 489
158 309
25 182 66 99
331 732
147 83 228 603
637 772
83 616 263 671
98 384
1 86 52 146
500 549
104 124 383 504
245 800
51 545 224 82
530 310
55 260 499 122
796 736
784 715 422 49
830 141
304 126 540 115
188 216
167 200 77 95
877 833
103 417 586 434
78 814
31 476 45 547
718 205
183 49 264 72
1000 1000
792 263 984 381
1000 1000
916 535 918 910
1000 1000
471 862 103 836
1000 1000
305 86 952 411
1000 1000
687 39 56 237
1000 1000
238 685 477 979
1000 1000
50 647 35 902
1000 1000
375 265 305 481
1000 1000
178 33 150 91
Case #1
4
Case #2
5
Case #3
4
Case #4
105
Case #5
27
Case #6
131
Case #7
36
Case #8
109
Case #9
132
Case #10
108
Case #11
114
Case #12
84
Case #13
146
Case #14
167
Case #15
119
Case #16
41
Case #17
170
Case #18
133
Case #19
5
Case #20
19
Case #21
92
Case #22
131
Case #23
13
Case #24
83
Case #25
14
Case #26
153
Case #27
89
Case #28
26
Case #29
42
Case #30
261
Case #31
91
Case #32
37
Case #33
221
Case #34
232
Case #35
222
Case #36
344
Case #37
119
Case #38
65
Case #39
242
Case #40
37
Case #41
42
Case #42
104
Case #43
189
Case #44
184
Case #45
324
Case #46
317
Case #47
179
Case #48
128
Case #49
108
Case #50
30
9 9
2 2 4 4
20 20
2 3 7 9
5 5
1 1 2 2
522 55
292 23 84 6
176 361
110 244 118 191
870 435
352 166 93 238
67 192
23 108 16 179
63 521
7 397 45 182
136 738
100 613 46 351
599 504
401 52 561 216
620 639
412 330 185 323
767 206
659 191 710 26
745 467
681 447 392 382
601 607
332 467 53 249
593 604
271 182 35 231
787 404
193 168 121 121
537 793
487 328 354 665
42 685
7 281 6 15
37 105
28 23 35 21
506 367
316 319 340 350
67 606
37 226 23 44
718 586
255 252 517 359
824 283
735 262 734 236
304 452
62 99 228 16
431 259
236 256 262 252
83 550
4 439 8 134
423 358
331 78 222 232
89 663
84 518 35 489
158 309
25 182 66 99
331 732
147 83 228 603
637 772
83 616 263 671
98 384
1 86 52 146
500 549
104 124 383 504
245 800
51 545 224 82
530 310
55 260 499 122
796 736
784 715 422 49
830 141
304 126 540 115
188 216
167 200 77 95
877 833
103 417 586 434
78 814
31 476 45 547
718 205
183 49 264 72
1000 1000
792 263 984 381
1000 1000
916 535 918 910
1000 1000
471 862 103 836
1000 1000
305 86 952 411
1000 1000
687 39 56 237
1000 1000
238 685 477 979
1000 1000
50 647 35 902
1000 1000
375 265 305 481
1000 1000
178 33 150 91
Case #1
4
Case #2
5
Case #3
4
Case #4
105
Case #5
27
Case #6
131
Case #7
36
Case #8
109
Case #9
132
Case #10
108
Case #11
114
Case #12
84
Case #13
146
Case #14
167
Case #15
119
Case #16
41
Case #17
170
Case #18
133
Case #19
5
Case #20
19
Case #21
92
Case #22
131
Case #23
13
Case #24
83
Case #25
14
Case #26
153
Case #27
89
Case #28
26
Case #29
42
Case #30
261
Case #31
91
Case #32
37
Case #33
221
Case #34
232
Case #35
222
Case #36
344
Case #37
119
Case #38
65
Case #39
242
Case #40
37
Case #41
42
Case #42
104
Case #43
189
Case #44
184
Case #45
324
Case #46
317
Case #47
179
Case #48
128
Case #49
108
Case #50
30
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.