-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.cpp
206 lines (189 loc) · 5.33 KB
/
simple.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
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
#include<iostream>
#include <cstdlib>
#include <cstdio>
#include <array>
#include <utility>
#include <cassert>
using RawField = std::array<std::array<char, 32>, 32>;
using RawStone = std::array<std::array<char, 8>, 8>;
constexpr int ROTATE_90 = 1,
ROTATE_180 = 2,
ROTATE_270 = 4,
REVERSE = 8;
class Field {
private:
int score = 0;
public:
RawField raw = {};
int Score() const {
return score;
}
bool TryPutStone(int stone_number, int base_x, int base_y, int manipulate_info);
};
class Stone {
public:
RawStone raw = {};
};
Field max_score_field; // 最終的にscoreが最大になっているfieldが入る
Stone reserved_stones[256];
int number_of_stone = 0;
void Parse(Field* f) { // 今後、標準入力以外の場所から入力を受け付けるのかしら
for (int i = 0; i < 32; ++i) {
fread(f->raw[i].data(), sizeof(char[32]), 1, stdin);
getchar(); //CR
getchar(); //LF
}
scanf("\n%d\n", &number_of_stone);
for (int i = 0; i < number_of_stone; ++i) {
for (int j = 0; j < 8; ++j) {
fread(reserved_stones[i].raw[j].data(), sizeof(char[8]), 1, stdin);
getchar(); //CR
getchar(); //LF
}
getchar(); //CR
getchar(); //LF
}
}
void DumpField(const Field& f) {
for (int i = 0; i < 32; ++i) {
for (int j = 0; j < 32; ++j) {
putc(f.raw[i][j], stdout);
}
puts("");
}
}
void Solve(Field f, const int look_nth_stone) {
if (look_nth_stone > number_of_stone) { //終了判定
if (f.Score() > max_score_field.Score()) {
max_score_field = f;
}
return;
}
Solve(f, look_nth_stone + 1); // 石を置かない場合
Field backup = f;
for (int x = -7; x < 32; ++x) {
for (int y = -7; y < 32; ++y) {
for (int i = 0; i < 8; ++i) {
if (f.TryPutStone(look_nth_stone, x, y, i)) { //置いてみておけたら
Solve(f, look_nth_stone+1);
f = backup;
}
}
}
}
}
void DumpStones() {
for (int i = 0; i < number_of_stone; ++i) {
for (int j = 0; j < 8; ++j) {
for (int k = 0; k < 8; ++k) {
putc(reserved_stones[i].raw[j][k], stdout);
}
puts("");
}
puts("");
}
}
void test();
int main() {
test();
Field reserved_field;
// get_problemfile();
Parse(&reserved_field);
Solve(reserved_field, 0);
DumpField(max_score_field);
// solve();
// submit_answer();
return 0;
}
RawStone StoneRotate(RawStone stone, int manipulate_info) { //石回す
RawStone rotated;
switch (manipulate_info) {
case ROTATE_90:
for (int a = 0; a < 8; ++a) {
for (int b = 0; b < 8; ++b) {
rotated[b][7-a] = stone[a][b];
}
}
return rotated;
case ROTATE_180:
return StoneRotate(std::move(StoneRotate(std::move(stone), ROTATE_90)), ROTATE_90);
case ROTATE_270:
return StoneRotate(std::move(StoneRotate(std::move(stone), ROTATE_180)), ROTATE_90);
case REVERSE:
for (int y = 0; y < 8; ++y) {
for (int x = 0; x < 8; ++x) {
rotated[y][x] = stone[y][7-x];
}
}
return rotated;
case REVERSE | ROTATE_90:
return StoneRotate(std::move(StoneRotate(std::move(stone), ROTATE_90)), REVERSE);
case REVERSE | ROTATE_180:
return StoneRotate(std::move(StoneRotate(std::move(stone), ROTATE_180)), REVERSE);
case REVERSE | ROTATE_270:
return StoneRotate(std::move(StoneRotate(std::move(stone), ROTATE_270)), REVERSE);
}
return stone; // 0のとき
}
bool Field::TryPutStone(int stone_number, int base_x, int base_y, int manipulate_info) {
int dx[] = {-1, 0, 0, 1}, // 隣接判定の上下左右
dy[] = {0, -1, 1, 0};
// 書き換えるので、もどせるようにしておく
RawField backup_field = raw;
int backup_score = score;
// 石回す
RawStone sraw = StoneRotate(std::move(reserved_stones[stone_number].raw), manipulate_info);
// score = 0なら隣接判定しない
bool exist_neighbor = (score == 0);
for (int x = 0; x < 8; ++x) {
for (int y = 0; y < 8; ++y) {
if (y + base_y < 0 || y + base_y >= 32 || x + base_x < 0 || x + base_x >= 32) { // 範囲チェック
continue;
}
if (sraw[y][x] == '1') {
if(raw[y + base_y][x + base_x] != '0') { //かぶるとき
raw = backup_field;
score = backup_score;
return false;
}
if (! exist_neighbor) { //隣接判定
for (int i = 0; i < 4; ++i) {
if (y + base_y + dy[i] < 0 || y + base_y + dy[i] >= 32 || x + base_x + dx[i] < 0 || x + base_x + dx[i] >= 32) {
continue;
}
if (raw[y + base_y + dy[i]][x + base_x + dx[i]] == '2') {
exist_neighbor = true;
}
}
}
// 更新
raw[y + base_y][x + base_x] = '2';
++score;
}
}
}
return true;
}
//
void test() { //テスト走らせる
RawStone rawstone;
for (int x = 0; x < 8; ++x) {
rawstone[0][x] = '1';
}
for (int y = 1; y < 8; ++y) {
for (int x = 0; x < 8; ++x) {
rawstone[y][x] = '0';
}
}
RawStone rotated_90;
for (int y = 0; y < 8; ++y) {
rotated_90[y][7] = '1';
}
for (int y = 0; y < 8; ++y) {
for (int x = 0; x < 7; ++x) {
rotated_90[y][x] = '0';
}
}
RawStone rotated_by_f = std::move(StoneRotate(rawstone, ROTATE_270|REVERSE));
assert(rotated_by_f == rotated_90);
}