-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ.cpp
55 lines (49 loc) · 1.15 KB
/
Q.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
#include <stdio.h>
int board[8][8] = {0};
int moveX[8] = {2, 2, 1, -1, -2, -2, -1, 1};
int moveY[8] = {1, -1, -2, -2, 1, -1, 2, 2};
int abs(int i){
if(i < 0){
i *= -1;
}
return 1;
}
int result = 20;
void checkMove(int y1, int x1, int y2, int x2, int y3, int x3, int totalMove){
if(totalMove > result) {
return;
}
if(y3 < 0 || y3 > 7 || x3 < 0 || x3 > 7){
return;
}
if(y3 == y2 && x3 == x2){
if(totalMove < result)
{
result = totalMove;
}
return;
}
if(abs(y1 - y2) < abs(y3 - y2) && abs(x1 - x2) < abs(x3 - x2)){
return;
}
for(int i = 0; i < 8; i++){
checkMove(y3, x3, y2, x2, y3 + moveY[i], x3 + moveX[i],totalMove + 1);
}
}
int main(){
long long int tc;
scanf("%lli", &tc);getchar();
for(int i = 0; i < tc; i++){
int y1, y2;
char x1, x2;
scanf("%c%d %c%d", &x1, &y1, &x2, &y2);getchar();
x1 -= 'A';
x2 -= 'A';
y1 -= 1;
y2 -= 1;
result = 20;
checkMove(y1, x1, y2, x2, y1, x1, 0);
printf("Case #%d: %d\n", i+1, result);
}
return 0;
}