-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAOJ0132.cpp
190 lines (174 loc) · 4.55 KB
/
AOJ0132.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
#include <bits/stdc++.h>
using namespace std;
using VS = vector<string>; using LL = long long;
using VI = vector<int>; using VVI = vector<VI>;
using PII = pair<int, int>; using PLL = pair<LL, LL>;
using VL = vector<LL>; using VVL = vector<VL>;
#define ALL(a) begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
//#pragma GCC optimize ("-O3")
#ifdef YANG33
#include "mydebug.hpp"
#else
#define DD(x)
#endif
const int INF = 1e9; const LL LINF = 1e16;
const LL MOD = 1000000007; const double PI = acos(-1.0);
/* ----- 2019/07/21 Problem: AOJ 0132 / Link: https://onlinejudge.u-aizu.ac.jp/challenges/search/volumes/0132 ----- */
VS rotate2dR(const VS & a) { // rorateToR
VS res(a[0].size(), string(a.size(), 1));
FOR(i, 0, (int)a.size())FOR(j, 0, (int)a[0].size())res[j][a.size() - i - 1] = a[i][j];
return res;
}
// n<=10, rot -> 40
int main() {
int H, W;
while (cin >> H >> W, H || W) {
int target_space = 0;
VS target(H); {
FOR(i, 0, H) {
cin >> target[i];
FOR(j, 0, W) {
if (target[i][j] == '.')target_space++;
}
}
}
int N; cin >> N;
VI h(4 * N), w(4 * N);
vector<VS> piece(4 * N);
VI sz(4 * N);
FOR(i, 0, N) {
cin >> h[i] >> w[i];
VS a(h[i]); {
int black = 0;
FOR(y, 0, h[i]) {
cin >> a[y];
FOR(x, 0, w[i]) {
if (a[y][x] == '#')black++;
}
}
sz[i] = black;
}
piece[i] = a;
// rotate i -> N+i -> 2*N+i -> 3*N+i
FOR(k, 1, 4) {
piece[i + k * N] = rotate2dR(piece[i + (k - 1)*N]);
h[i + k * N] = w[i + (k - 1)*N];
w[i + k * N] = h[i + (k - 1)*N];
sz[i + k * N] = sz[i + (k - 1)*N];
}
}
if (0) {
FOR(i, 0, N) {
cout << "-----------" << endl;
FOR(k, 0, 4) {
cout << "!!!!!!!!!!" << endl;
int id = i + k * N;
FOR(y, 0, h[id]) {
cout << piece[id][y] << endl;
}
}
}
return 0;
}
int Q; cin >> Q;
FOR(_, 0, Q) {
int qn; cin >> qn;
int blacksum = 0;
VI query(qn);
FOR(i, 0, qn) {
cin >> query[i];
query[i]--;
blacksum += sz[query[i]];
}
// big sort
sort(ALL(query), [&](const int a, const int b) {
return sz[a] > sz[b];
});
function<bool(int)>f = [&](int idx) {
//DD(de(idx));
if (idx == qn) {
return true;
}
{ // check
VVI masu(H, VI(W, 0));
FOR(i, 0, H) {
FOR(j, 0, W) {
// まだ置いていなくて,置く可能性もないっぽい
if (target[i][j] == '.' && !masu[i][j]) {
FOR(ids, idx, qn) {
FOR(k, 0, 4) {
int id = query[ids] + k * N;
// 以降でぬれるかをチェック
for (int y = max(0, i - h[id] + 1); y <= i && y + h[id] <= H; y++) {
for (int x = max(0, j - w[id] + 1); x <= j && x + w[id] <= W; x++) {
FOR(a, 0, h[id])FOR(b, 0, w[id]) {
if (piece[id][a][b] == '#' && target[y + a][x + b] == '#') goto FAIL;
}
//DD(de("put check", ids, k));
FOR(a, 0, h[id]) FOR(b, 0, w[id]) {
if (piece[id][a][b] == '#') masu[y + a][x + b] = 1;
}
FAIL:;
}
}
}
}
// このマスをぬれる人が誰もいない
if (!masu[i][j])return false;
}
}
}
}
{ // put
FOR(k, 0, 4) {
int id = query[idx] + k * N;
FOR(y, 0, H - h[id] + 1) {
FOR(x, 0, W - w[id] + 1) {
bool can_set = 1;
[&] { // check whether we put
FOR(i, 0, h[id]) {
FOR(j, 0, w[id]) {
if (target[y + i][x + j] == '#'&&piece[id][i][j] == '#') {
can_set = 0;
return;
}
}
}
}();
if (can_set) {
FOR(i, 0, h[id]) { // put
FOR(j, 0, w[id]) {
if (piece[id][i][j] == '#') {
target[y + i][x + j] = '#';
}
}
}
//DD(de("put", idx, k));
bool res = f(idx + 1);
FOR(i, 0, h[id]) { // restore
FOR(j, 0, w[id]) {
if (piece[id][i][j] == '#') {
target[y + i][x + j] = '.';
}
}
}
if (res)return true;
}
}
}
}
}
return false;
};
cout << (blacksum == target_space && f(0) ? "YES" : "NO") << "\n";
}
}
return 0;
}