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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
| /////////////////////////////////////////////////////////////////////////////////////////
#include <algorithm>
#include <cctype>
#include <complex>
#include <iostream>
#include <map>
#include <string>
#include <vector>
/////////////////////////////////////////////////////////////////////////////////////////
typedef int T_coord;
typedef std::complex<T_coord> T_coords;
typedef std::string T_str;
/////////////////////////////////////////////////////////////////////////////////////////
class T_bishop_cell
{
static const char VERT_MIN = 'a';
static const char HORIZ_MIN = '1';
//-----------------------------------------------------------------------------------
T_coords coords_;
//-----------------------------------------------------------------------------------
public:
//-----------------------------------------------------------------------------------
static const T_coord COORD_MIN = 1;
static const T_coord COORD_MAX = 8;
//-----------------------------------------------------------------------------------
T_bishop_cell
(
T_coord horiz_coord = T_coord(),
T_coord vert_coord = T_coord()
)
:
coords_
(
horiz_coord,
vert_coord
)
{}
//-----------------------------------------------------------------------------------
bool color() const
{
return (
coords_.real()
+ coords_.imag()
)
% 2 == 0;
}
//-----------------------------------------------------------------------------------
T_str name() const
{
T_str res_str;
res_str.push_back( char(VERT_MIN + coords_.imag() - COORD_MIN) );
res_str.push_back( char(HORIZ_MIN + coords_.real() - COORD_MIN) );
return res_str;
}
//-----------------------------------------------------------------------------------
bool successfully_assign( const T_str& cell_name )
{
T_coord horiz_coord = 0;
T_coord vert_coord = 0;
bool bool_res = cell_name.size() == 2
&& get_horiz_coord
(
cell_name,
horiz_coord
)
&& get_vert_coord
(
cell_name,
vert_coord
);
if(bool_res)
{
coords_ = T_coords
(
horiz_coord,
vert_coord
);
}
return bool_res;
}
//-----------------------------------------------------------------------------------
bool operator== (T_bishop_cell cell) const
{
return coords_ == cell.coords_;
}
//-----------------------------------------------------------------------------------
T_bishop_cell operator- (T_bishop_cell cell) const
{
T_bishop_cell res;
res.coords_ = coords_ - cell.coords_;
return res;
}
//-----------------------------------------------------------------------------------
bool operator< (T_bishop_cell cell) const
{
return std::make_pair
(
coords_.real(),
coords_.imag()
)
< std::make_pair
(
cell.coords_.real(),
cell.coords_.imag()
);
}
//-----------------------------------------------------------------------------------
T_coord abs_bishop_shift() const
{
return abs( coords_.real() )
== abs( coords_.imag() )
? abs( coords_.real() )
: 0;
}
//-----------------------------------------------------------------------------------
bool bishop_shift_direction() const
{
return coords_.real()
* coords_.imag() > 0;
}
//-----------------------------------------------------------------------------------
private:
//-----------------------------------------------------------------------------------
static bool get_horiz_coord
(
const T_str& s,
T_coord& horiz_coord
)
{
horiz_coord = s[1] - HORIZ_MIN + COORD_MIN;
return coord_is_correct( horiz_coord );
}
//-----------------------------------------------------------------------------------
static bool get_vert_coord
(
const T_str& s,
T_coord& vert_coord
)
{
vert_coord = tolower(s[0]) - VERT_MIN + COORD_MIN;
return coord_is_correct( vert_coord );
}
//-----------------------------------------------------------------------------------
static bool coord_is_correct(T_coord coord)
{
return COORD_MIN <= coord
&& coord <= COORD_MAX;
}
//-----------------------------------------------------------------------------------
};
/////////////////////////////////////////////////////////////////////////////////////////
typedef std::vector <T_bishop_cell> T_move;
typedef std::vector <T_move> T_moves;
typedef std::map <T_bishop_cell, T_bishop_cell > T_adjacent_row;
typedef std::map <T_bishop_cell, T_adjacent_row > T_adjacent_matr;
/////////////////////////////////////////////////////////////////////////////////////////
void input_cell_name
(
const T_str& prompt,
T_str& cell_name
)
{
std::cout << prompt;
std::cin >> cell_name;
}
/////////////////////////////////////////////////////////////////////////////////////////
void make_adjacent_matr(T_adjacent_matr& adjacent_matr)
{
for(
int
H = T_bishop_cell::COORD_MIN;
H <= T_bishop_cell::COORD_MAX;
++H
)
{
for(
int
V = T_bishop_cell::COORD_MIN;
V <= T_bishop_cell::COORD_MAX;
++V
)
{
T_bishop_cell start_cell(H, V);
for(
int
HH = T_bishop_cell::COORD_MIN;
HH <= T_bishop_cell::COORD_MAX;
++HH
)
{
for(
int
VV = T_bishop_cell::COORD_MIN;
VV <= T_bishop_cell::COORD_MAX;
++VV
)
{
T_bishop_cell finish_cell(HH, VV);
T_bishop_cell shift_cell = finish_cell - start_cell;
if( shift_cell.abs_bishop_shift() != 0 )
{
adjacent_matr[start_cell][finish_cell] = shift_cell;
}
}
}
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////
void print_move( const T_move& move )
{
for(
T_move::const_iterator
cell_it = move.begin();
cell_it != move.end();
++cell_it
)
{
std::cout << cell_it->name()
<< '\t';
}
std::cout << std::endl;
}
/////////////////////////////////////////////////////////////////////////////////////////
void make_move
(
const T_adjacent_matr& adjacent_matr,
T_bishop_cell& start_cell,
T_bishop_cell& finish_cell,
T_moves& shortest_moves,
int& min_move_len,
bool& is_first_move,
T_move cur_move,
T_coord cur_move_len
)
{
if( cur_move.empty() )
{
cur_move.push_back( start_cell );
make_move
(
adjacent_matr,
start_cell,
finish_cell,
shortest_moves,
min_move_len,
is_first_move,
cur_move,
cur_move_len
);
}
else if( cur_move.back() == finish_cell )
{
//Проверяем ход на минимаьную длину.
if(
is_first_move
|| cur_move_len < min_move_len
)
{
is_first_move = false;
min_move_len = cur_move_len;
shortest_moves.clear ();
shortest_moves.push_back (cur_move);
}
else if( cur_move_len == min_move_len )
{
shortest_moves.push_back(cur_move);
}
}
else if(
shortest_moves.empty()
|| cur_move_len < min_move_len
)
{
const T_adjacent_row& cur_matr_row
= adjacent_matr.find( cur_move.back() )->second;
for(
T_adjacent_row::const_iterator
cell_shift_it = cur_matr_row.begin ();
cell_shift_it != cur_matr_row.end ();
++cell_shift_it
)
{
//Если клетки еще нет в ходе, и направление сдвига не совпадает с предыдущим,
//то добавляем ее в ход.
bool is_new_cell = std::find
(
cur_move.begin (),
cur_move.end (),
cell_shift_it->first
)
== cur_move.end();
if(!is_new_cell) continue;
bool is_new_shift_direction
= cur_move.size() < 2
|| cell_shift_it->second
.bishop_shift_direction()
!= ( cur_move.back() - cur_move.rbegin()[1] )
.bishop_shift_direction();
if(!is_new_shift_direction) continue;
T_move new_cur_move(cur_move);
new_cur_move.push_back(cell_shift_it->first);
make_move
(
adjacent_matr,
start_cell,
finish_cell,
shortest_moves,
min_move_len,
is_first_move,
new_cur_move,
cur_move_len + cell_shift_it->second.abs_bishop_shift()
);
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////
int main()
{
std::locale::global(std::locale(""));
std::cout << "Введите координаты стартовой и финишной клеток для слона в виде d3 или f7:"
<< std::endl;
T_str start_cell_name;
T_bishop_cell start_cell;
do
{
input_cell_name
(
"\tстартовая клетка:\t",
start_cell_name
);
}while(
!start_cell.successfully_assign( start_cell_name )
);
std::cout << std::endl;
T_str finish_cell_name;
T_bishop_cell finish_cell;
do
{
input_cell_name
(
"\tфинишная клетка:\t",
finish_cell_name
);
}while(
!finish_cell.successfully_assign( finish_cell_name )
|| finish_cell == start_cell
|| finish_cell.color() != start_cell.color()
);
T_adjacent_matr adjacent_matr;
make_adjacent_matr( adjacent_matr );
T_moves shortest_moves;
int min_move_len = 0;
bool is_first_move = true;
make_move
(
adjacent_matr,
start_cell,
finish_cell,
shortest_moves,
min_move_len,
is_first_move,
T_move(),
T_coord()
);
std::cout << std::endl
<< "Кратчайшие пути слона из клетки "
<< start_cell.name()
<< " в клетку "
<< finish_cell.name()
<< ":"
<< std::endl;
std::for_each
(
shortest_moves.begin(),
shortest_moves.end(),
print_move
);
std::cout << std::endl;
} |