版本比较
U658163 逝川之弈 题解
正文
增加 27 行删除 27 行
51 行未改动5253545556565758585960616162636465108 行未改动全部展开17417517617717817817918018018118218318418518518618718818918919019119219219319419519619719819920020020120220320420520520620720820921021121221321321421521621721821922022122122222322422522622622722822923023123223323423423523623723823924024124224224324424524624724824925025025125225325425525625725825825926026126226326426526626626726826927027127227327427527627627727827928028128228328428528628628728828929029129229329329429529629729829930030030130230330330430530530630730830830931031131231331431531631731831831932032132232332432532632732832832933051 行未改动 bitset<MAXN*MAXN> past; bitset<MAXN*MAXN> now; int steps; int resonance;
bool operator==(const State& other) const { return light == other.light && shadow == other.shadow && return light == other.light && shadow == other.shadow && past == other.past && now == other.now; }
struct Hash { size_t operator()(const State& s) const { size_t h = 0; hash<bitset<MAXN*MAXN>> hasher;108 行未改动全部展开 } } init.resonance = compute_resonance(init); init.steps = 0;
if (init.resonance >= target) return 0;
queue<State> q; unordered_set<State, State::Hash> visited; q.push(init); visited.insert(init);
while (!q.empty()) { State cur = q.front(); q.pop();
if (cur.steps >= K) continue; int next_steps = cur.steps + 1;
// 放置追光者 for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (board[i][j] != '.') continue; int id = idx(i, j); if (cur.light.test(id) || cur.shadow.test(id)) continue; if (!check_distance(cur, id, '+')) continue;
State next = cur; next.light.set(id); next.steps = next_steps; next.resonance = compute_resonance(next);
if (next.resonance >= target) return next.steps; if (next.steps < K && !visited.count(next)) { visited.insert(next); q.push(next); } } }
// 放置溯影者 for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (board[i][j] != '.') continue; int id = idx(i, j); if (cur.light.test(id) || cur.shadow.test(id)) continue; if (!check_distance(cur, id, '-')) continue;
State next = cur; next.shadow.set(id); next.steps = next_steps; next.resonance = compute_resonance(next);
if (next.resonance >= target) return next.steps; if (next.steps < K && !visited.count(next)) { visited.insert(next); q.push(next); } } }
// 移除追光者 for (int i = 0; i < n*m; i++) { if (cur.light.test(i)) { State next = cur; next.light.reset(i); next.steps = next_steps; next.resonance = compute_resonance(next);
if (next.resonance >= target) return next.steps; if (next.steps < K && !visited.count(next)) { visited.insert(next); q.push(next); } } }
// 移除溯影者 for (int i = 0; i < n*m; i++) { if (cur.shadow.test(i)) { State next = cur; next.shadow.reset(i); next.steps = next_steps; next.resonance = compute_resonance(next);
if (next.resonance >= target) return next.steps; if (next.steps < K && !visited.count(next)) { visited.insert(next); q.push(next); } } }
// 转化当下格为往昔格 if (cur.past.count() < max_past) { for (int i = 0; i < n*m; i++) { if (cur.now.test(i)) { State next = cur; next.now.reset(i); next.past.set(i); next.steps = next_steps; next.resonance = compute_resonance(next);
if (next.resonance >= target) return next.steps; if (next.steps < K && !visited.count(next)) { visited.insert(next); q.push(next); } } } } }
return INF;}
int main() { ios::sync_with_stdio(false); cin.tie(0);
cin >> n >> m >> K >> max_past >> R_plus >> R_minus >> D_same >> D_diff; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> board[i][j]; } }
precompute_light(); precompute_shadow();
cin >> Q;
vector<int> answers(n*m+1, INF); answers[0] = 0;
for (int target = 1; target <= n*m; target++) { int ops = bfs_search(target); if (ops < INF) { answers[target] = min(answers[target], ops); } if (target > 0 && answers[target] == INF) { answers[target] = answers[target-1]; } }
while (Q--) { int x; cin >> x; if (answers[x] <= K) { cout << "YES " << answers[x] << "\n"; } else { cout << "NO\n"; } }
return 0;}