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
| /////////////////////////////////////////////////////////////////////////////////////////
//Поле шахматной доски задается парой натуральных чисел: первое указывает номер вертикали
//при счете слева направо, второе – номер горизонтали при счете снизу вверх. Расстановка
//фигур задается таким образом, что в начале указываются поля, на которых стоят перечисленные
//белые фигуры, затем – поля, на которых стоят перечисленные черные фигуры. Решить задачу -
//на доске стоят два ферзя. Указать поля, на которые может пойти белый ферзь так, чтобы
//не попасть под удар черного ферзя.
/////////////////////////////////////////////////////////////////////////////////////////
#include <algorithm>
#include <complex>
#include <iostream>
#include <iterator>
#include <set>
#include <string>
#include <utility>
/////////////////////////////////////////////////////////////////////////////////////////
typedef std::string T_str;
typedef std::complex < int > T_cell;
/////////////////////////////////////////////////////////////////////////////////////////
namespace std
{
bool operator<
(
T_cell const & L,
T_cell const & R
)
{
return std::make_pair( L.imag(), L.real() )
< std::make_pair( R.imag(), R.real() );
}
}
/////////////////////////////////////////////////////////////////////////////////////////
typedef std::set < T_cell > T_cells;
/////////////////////////////////////////////////////////////////////////////////////////
template< typename T_set >
T_set operator-
(
T_set const & L,
T_set const & R
)
{
T_set res_set;
std::set_difference
(
L.begin (),
L.end (),
R.begin (),
R.end (),
std::inserter (
res_set,
res_set.begin()
)
);
return res_set;
}
/////////////////////////////////////////////////////////////////////////////////////////
class T_board
{
//-----------------------------------------------------------------------------------
friend class T_queen;
//-----------------------------------------------------------------------------------
class T_queen
{
//-------------------------------------------------------------------------------
friend class T_board;
//-------------------------------------------------------------------------------
T_cell cell_;
bool color_is_white_;
T_board * board_ptr_;
//-------------------------------------------------------------------------------
public:
//-------------------------------------------------------------------------------
T_queen
(
T_cell cell = T_cell (),
bool color_is_white = bool (),
T_board * board_ptr = nullptr
)
:
cell_ ( cell ),
color_is_white_ ( color_is_white ),
board_ptr_ ( board_ptr )
{}
//-------------------------------------------------------------------------------
T_cells get_attacked_cells_including_cells_of_same_color() const
{
T_cells res_cells;
for( int shift_x = -1; shift_x <= 1; ++shift_x )
{
for( int shift_y = -1; shift_y <= 1; ++shift_y )
{
if (
abs( shift_x )
+ abs( shift_y )
!= 0
)
{
auto shift_cell = T_cell (
shift_x,
shift_y
);
for (
T_cell cur_cell = cell_ + shift_cell;
board_ptr_->cell_is_correct( cur_cell );
cur_cell += shift_cell
)
{
res_cells.insert( cur_cell );
if (
board_ptr_->figures_white_cells_.count( cur_cell ) != 0
|| board_ptr_->figures_black_cells_.count( cur_cell ) != 0
)
{
break;
}//if
}//for
}//if
}//for
}//for
return res_cells;
}
//-------------------------------------------------------------------------------
};
//-----------------------------------------------------------------------------------
static const int BOARD_DIM = 8;
//-----------------------------------------------------------------------------------
T_cells figures_white_cells_;
T_cells figures_black_cells_;
T_queen queen_white_;
T_queen queen_black_;
//-----------------------------------------------------------------------------------
public:
//-----------------------------------------------------------------------------------
static const bool COLOR_IS_WHITE = true;
//-----------------------------------------------------------------------------------
T_board
(
T_cells const & figures_white_cells,
T_cell queen_white_cell,
T_cells const & figures_black_cells,
T_cell queen_black_cell
)
:
figures_white_cells_ ( figures_white_cells ),
figures_black_cells_ ( figures_black_cells )
{
queen_white_ = T_queen (
queen_white_cell,
COLOR_IS_WHITE,
this
);
queen_black_ = T_queen (
queen_black_cell,
!COLOR_IS_WHITE,
this
);
}
//-----------------------------------------------------------------------------------
void print_cells_to_that_can_go_white_queen_so_that_dont_get_attack_of_black_queen() const
{
auto res_cells
= queen_white_.get_attacked_cells_including_cells_of_same_color()
- figures_white_cells_
- queen_black_.get_attacked_cells_including_cells_of_same_color();
res_cells.erase( queen_black_.cell_ );
std::cout << std::endl
<< "Поля, на которые может пойти белый ферзь так, чтобы не попасть под удар черного ферзя:"
<< std::endl;
if (
res_cells.empty()
)
{
std::cout << "Нет таких полей."
<< std::endl;
}
else
{
std::copy
(
res_cells.begin (),
res_cells.end (),
std::ostream_iterator< T_cell > ( std::cout, "\t" )
);
}//else
std::cout << std::endl;
}
//-----------------------------------------------------------------------------------
static bool cell_is_correct( T_cell cell )
{
return 0 <= cell.real()
&& cell.real() < BOARD_DIM
&& 0 <= cell.imag()
&& cell.imag() < BOARD_DIM;
}
//-----------------------------------------------------------------------------------
};
/////////////////////////////////////////////////////////////////////////////////////////
void input_figures_position_of_specified_color_including_queen
(
bool color_is_white,
T_cells & figures_cells,
T_cell & queen_cell
)
{
std::cout << std::endl
<< "Введите количество "
<< (
color_is_white
? "белых"
: "черных"
)
<<" фигур: ";
int figures_total = 0;
std::cin >> figures_total;
std::cout << "Введите "
<< figures_total
<< " позиций фигур в виде (0,7). Позицию ферзя вводите первой."
<< std::endl;
for( long long i = 0; i < figures_total; ++i )
{
std::cout << (
i == 0
? "ферзь\t"
: T_str("фигура #") + std::to_string(i + 1)
)
<< "\t:\t";
T_cell cell_cur;
std::cin >> cell_cur;
figures_cells.insert( cell_cur );
if( i == 0 )
{
queen_cell = cell_cur;
}
}//for
}
/////////////////////////////////////////////////////////////////////////////////////////
int main()
{
std::locale::global(std::locale(""));
T_cells figures_white_cells;
T_cell queen_white_cell;
input_figures_position_of_specified_color_including_queen
(
T_board::COLOR_IS_WHITE,
figures_white_cells,
queen_white_cell
);
T_cells figures_black_cells;
T_cell queen_black_cell;
input_figures_position_of_specified_color_including_queen
(
!T_board::COLOR_IS_WHITE,
figures_black_cells,
queen_black_cell
);
T_board board (
figures_white_cells,
queen_white_cell,
figures_black_cells,
queen_black_cell
);
board.print_cells_to_that_can_go_white_queen_so_that_dont_get_attack_of_black_queen();
system("pause");
} |