-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcchess.cpp
172 lines (136 loc) · 2.63 KB
/
cchess.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
159
160
161
162
163
164
165
166
167
168
169
170
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
using namespace std;
struct posn {
int r;
int c;
};
int decode(posn p ) {
return (p.r * 8) + p.c;
}
posn encode ( int v ) {
posn p;
p.r = ( v / 8);
p.c = (v % 8 );
return p;
}
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] ) {
posn p1 = encode(v1);
posn p2 = encode(v2);
mat[v1][v2] = p1.r * p2.r + p1.c * p2.c;
}
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 mat[64][64];
int i, j;
for ( i = 0; i < 64; i++ ) {
for ( j = 0; j < 64; j++ ) {
mat[i][j] = 100000;
}
}
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++;
}
*/
char ch;
posn p1, p2;
while ( scanf("%d%d%d%d", &p1.r, &p1.c, &p2.r, &p2.c ) != EOF ) {
src = decode(p1);
dsn = decode(p2);
if ( src == dsn ) {
cout << 0 << endl;
} else if ( mat[src][dsn] < 100000 ) {
cout << mat[src][dsn] << endl;
} else {
cout << "-1" << endl;
}
}
return 0;
}