# CaptureKnight

ptrdung
plain_text
2 months ago
3.8 kB
1
Indexable
Never
```Capture Knight
There is a mobile piece (a Knight) and a stationary piece on the N×M chessboard.
The available moves of the mobile piece are the same as set out in the image below.

You need to capture the stationary piece by moving the mobile piece with the minimum amount of moves.

Write a program to find out the minimum number moves to catch a piece.

[Input]

Several test cases can be included in the inputs. T, the number of cases is given in the first row of the inputs. After that, the test cases as many as T are given in a row.

N, the numbers of the rows and M, the number of columns of the chessboard are given in the first row of each test case.

R & C is the location information of the attacking piece and S & K is the location of the defending pieces and are given in the row at the second line. However, the location of the uppermost end of the left end is (1, 1)

Maximum value of N or M is 1000.

[Output]

Output the minimum number of movements to catch a defending piece at the first line of each test case.

Print "Case #tn" before each answer where "tn" is test case number.

[I/O Example]

Input

2

9 9

3 5 2 8

20 20

2 3 7 9

Output

Case #1

2

Case #2

5

#include <iostream>

using namespace std;

int n; // number of row so hang
int m; // number of column so cot
int xAttack, yAttack, xDefend, yDefend;
int ans;
int xQueue[1000000], yQueue[1000000], start, last, visited[1001][1001];
int dx[8] = { -1, -2, -2, -1,  1,  2,  2,  1};
int dy[8] = { -2, -1,  1,  2,  2,  1, -1, -2};

void BFS()
{
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
visited[i][j] = 0;
int x, y, xNext, yNext;
start = 0;
last = 0;
visited[xAttack][yAttack] = 1;
xQueue[last] = xAttack;
yQueue[last++] = yAttack;

while(start != last)
{
x = xQueue[start];
y = yQueue[start++];
for(int i = 0; i < 8; i++)
{
xNext = x + dx[i];
yNext = y + dy[i];
if(xNext >= 1 && xNext <= n && yNext >= 1 && yNext <= m)
{
if(visited[xNext][yNext] == 0 || visited[xNext][yNext] > visited[x][y] + 1)
{
visited[xNext][yNext] = visited[x][y] + 1;
xQueue[last] = xNext;
yQueue[last++] = yNext;
}

}
}
}
ans = visited[xDefend][yDefend];

}

int main()
{
freopen("input1.txt", "r", stdin);
int T;
cin >> T;
for(int tc = 1; tc <= T; tc++)
{
cin >> n >> m;
cin >> xAttack >> yAttack >> xDefend >> yDefend;

BFS();
cout << "Case #" << tc << endl << ans-1 << endl;
}
return 0;
}

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```