-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnakanj.cpp
160 lines (127 loc) · 2.44 KB
/
nakanj.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
using namespace std;
int decode(string arr) {
int c = (int)arr[0] - 96;
int r = (int)arr[1] - 49;
return (r * 8) + c - 1;
}
int next_posn( int r, int c, int p ) {
return (p + (r * 8) + c);
}
int bound_check( int r, int c, int posn ) {
int row = posn / 8;
int col = posn % 8;
if ( row + r < 8 && col + c < 8 && row + r >= 0 && col + c >= 0) {
return 1;
} else {
return 0;
}
}
void insert(int v1, int v2, int mat[][64] ) {
mat[v1][v2] = 1;
}
void floyd_warshall(int mat[][64] ) {
int k, i, j;
for ( k = 0; k < 64; k++ ) {
for ( i = 0; i < 64; i++ ) {
for ( j = 0; j < 64; j++ ) {
if ( mat[i][j] > mat[i][k] + mat[k][j] ) {
mat[i][j] = mat[i][k] + mat[k][j] ;
}
}
}
}
}
int main()
{
int t;
cin >>t;
int mat[64][64];
int i, j;
for ( i = 0; i < 64; i++ ) {
for ( j = 0; j < 64; j++ ) {
mat[i][j] = 1000;
}
}
for ( i = 0; i < 64; i++ ) {
int e = 1;
int v1 = i;
int v2 = next_posn( -2, 1, v1 );
int decn = bound_check( -2, 1, v1);
if ( decn ) {
insert(v1, v2, mat);
}
decn = bound_check( -2, -1, v1);
if ( decn ) {
v2 = next_posn( -2, -1, v1 );
insert(v1, v2, mat);
}
decn = bound_check( -1, 2, v1);
if ( decn ) {
v2 = next_posn( -1, 2, v1 );
insert(v1, v2, mat);
}
decn = bound_check( -1, -2, v1);
if ( decn ) {
v2 = next_posn( -1, -2, v1 );
insert(v1, v2, mat);
}
decn = bound_check( 1, 2, v1);
if ( decn ) {
v2 = next_posn( 1, 2, v1 );
insert(v1, v2, mat);
}
decn = bound_check( 1, -2, v1);
if ( decn ) {
v2 = next_posn( 1, -2, v1 );
insert(v1, v2, mat);
}
decn = bound_check( 2, -1, v1);
if ( decn ) {
v2 = next_posn( 2, -1, v1 );
insert(v1, v2, mat);
}
decn = bound_check( 2, 1, v1);
if ( decn ) {
v2 = next_posn( 2, 1, v1 );
insert(v1, v2, mat);
}
}
floyd_warshall(mat);
int src, dsn;
string p;
/* src = 0;
while(src < 64 ) {
for ( int i = 7; i >= 0; i-- ) {
for ( int j = 0; j < 8; j++ ){
if ( src == i*8 + j ) {
cout << 0 << " ";
} else {
cout << mat[src][(i * 8) + j] << " ";
}
}
cout << endl;
}
cout << endl;
cout << endl;
cout << endl;
src++;
}
*/
for ( j = 0; j < t; j++ ) {
string p1, p2;
cin >> p1;
cin >> p2;
src = decode(p1);
dsn = decode(p2);
if ( src == dsn ) {
cout << 0 << endl;
} else {
cout << mat[src][dsn] << endl;
}
}
return 0;
}