#include // clang-format off using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair; const ll INF=4e18; void print0(){}; template void print0(H h,T... t){cout<void print(H h,T... t){print0(h);if(sizeof...(T)>0)print0(" ");print(t...);} #define debug1(a) { cerr<<#a<<":"<; // clang-format on namespace marathon { mt19937 engine(0); clock_t start_time; double now() { return 1000.0 * (clock() - start_time) / CLOCKS_PER_SEC; } void marathon_init() { start_time = clock(); random_device seed_gen; engine.seed(seed_gen()); } int randint(int mn, int mx) { int rng = mx - mn + 1; return mn + (engine() % rng); } double uniform(double x, double y) { const int RND = 1e8; double mean = (x + y) / 2.0; double dif = y - mean; double p = double(engine() % RND) / RND; return mean + dif * (1.0 - 2.0 * p); } template T random_choice(vector &vec) { return vec[engine() % vec.size()]; } bool anneal_accept(double new_score, double old_score, double cur_time, double begin_time, double end_time, double begin_temp, double end_temp) { static int called = 0; static double temp = 0; if ((called & 127) == 0) { double ratio = (cur_time - begin_time) / (end_time - begin_time); ratio = min(ratio, 1.0); temp = pow(begin_temp, 1.0 - ratio) * pow(end_temp, ratio); } called++; const int ANNEAL_RND = 1e8; const double ANNEAL_EPS = 1e-6; return (exp((new_score - old_score) / temp) > double(engine() % ANNEAL_RND) / ANNEAL_RND + ANNEAL_EPS); } } // namespace marathon struct operation_t { int kind; int xyz; }; struct bomb_t { int cost; vector ranges; }; const int _U_ = 0; const int _R_ = 1; const int _D_ = 2; const int _L_ = 3; const int N = 50; const int M = 20; const int N2 = 2500; vector mvs = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; using bsn2 = bitset; vector> mvtbl(N2); bsn2 break_bomb_bs[N][N][M]; char TBL_INIT[N][N]; bomb_t BOMBS[M]; int getmove(point src, point dest) { assert(abs(src.r - dest.r) + abs(src.c - dest.c) == 1); if (src.r < dest.r) { return _D_; } if (src.r > dest.r) { return _U_; } if (src.c < dest.c) { return _R_; } if (src.c > dest.c) { return _L_; } } bool ingrid(int i, int j) { return 0 <= i && i < N && 0 <= j && j < N; } bool ingrid(point p) { return ingrid(p.r, p.c); } int _p(int r, int c) { return r * N + c; } int _p(point p) { return _p(p.r, p.c); } void init_data() { for (int r = 0; r < N; r++) { for (int c = 0; c < N; c++) { point u = {r, c}; for (auto mv : mvs) { auto v = mv + u; if (!ingrid(v)) continue; mvtbl[_p(u)].push_back(_p(v)); } } } for (int r = 0; r < N; r++) { for (int c = 0; c < N; c++) { for (int bid = 0; bid < M; bid++) { break_bomb_bs[r][c][bid] = 0; point now = {r, c}; for (auto drc : BOMBS[bid].ranges) { point pt = now + drc; if (!ingrid(pt)) continue; break_bomb_bs[r][c][bid][_p(pt)] = 1; } } } } } vector> dij_cost(point ini, vector> &tbl) { // コスト上限が400くらいなのでpriority_queue不要 vector tbl_(N2); for (int r = 0; r < N; r++) { for (int c = 0; c < N; c++) { tbl_[_p(r, c)] = tbl[r][c]; } } vector costs(N2, inf); vector> cost_points(N * 4); cost_points[0].push_back(_p(ini)); costs[_p(ini)] = 0; for (int cost = 0; cost < N * 4; cost++) { for (auto u : cost_points[cost]) { for (auto v : mvtbl[u]) { int nc = cost + 1; if (tbl_[v] != '.') { nc = cost + 2; } if (costs[v] <= nc) continue; costs[v] = nc; cost_points[nc].push_back(v); } } } vector> result(N, vector(N)); for (int r = 0; r < N; r++) { for (int c = 0; c < N; c++) { result[r][c] = costs[_p(r, c)]; } } return result; } void dij_goto_ops(point &hulk, point dest, vector &ops, vector> &tbl) { priority_queue, vector>, greater>> pq; pq.push({0, hulk, {-1, -1}}); vector> costs(N, vector(N, inf)); vector> froms(N, vector(N, {-1, -1})); while (pq.size()) { int cost; point u, frm; tie(cost, u, frm) = pq.top(); pq.pop(); if (costs[u.r][u.c] <= cost) continue; costs[u.r][u.c] = cost; froms[u.r][u.c] = frm; if (u == dest) break; for (auto mv : mvs) { point v = mv + u; if (!ingrid(v)) continue; int nc = cost + 1; if (tbl[v.r][v.c] != '.') { nc = cost + 2; } pq.push({nc, v, u}); } } { point cur = dest; vector route; while (true) { route.push_back(cur); if (cur == hulk) break; cur = froms[cur.r][cur.c]; } reverse(route.begin(), route.end()); for (int i = 1; i < int(route.size()); i++) { point pre = route[i - 1]; point nxt = route[i]; int mvid = getmove(pre, nxt); ops.push_back({1, mvid}); } } hulk = dest; } pii evaluate_ops(vector &ops) { vector> tbl(N, vector(N)); for (int r = 0; r < N; r++) { for (int c = 0; c < N; c++) { tbl[r][c] = TBL_INIT[r][c]; } } int move_cost = 0; int bomb_cost = 0; point hulk = {0, 0}; int bombs = 0; for (auto op : ops) { if (op.kind == 1) { hulk = hulk + mvs[op.xyz]; if (tbl[hulk.r][hulk.c] == '.') { move_cost += (1 + bombs) * (1 + bombs); } else { move_cost += 2 * (1 + bombs) * (1 + bombs); } } else if (op.kind == 2) { bombs++; int bombid = op.xyz; bomb_cost += BOMBS[bombid].cost; } else { bombs--; // WAにはならない前提 int bombid = op.xyz; for (auto drc : BOMBS[bombid].ranges) { point pt = hulk + drc; if (!ingrid(pt)) continue; tbl[pt.r][pt.c] = '.'; } } } return {move_cost, bomb_cost}; } int bomb_place_id(point p, int b) { return b * N * N + p.r * N + p.c; } int bpid2bombid(int bpid) { return bpid / N2; } point bpid2point(int bpid) { return point{(bpid / N) % N, bpid % N}; } vector hc_bomb_places(vector init_bomb_places, vector init_fillcnt, vector> bpid2walls, vector> wall2bpids) { // 破壊再構築系の山登り vector bpid2cost(N * N * M); for (int bpid = 0; bpid < N * N * M; bpid++) { bpid2cost[bpid] = BOMBS[bpid2bombid(bpid)].cost; } int old_score = 0; auto bomb_places = init_bomb_places; auto fillcnt = init_fillcnt; for (auto bpid : bomb_places) { old_score += -bpid2cost[bpid]; } double begin_time = marathon::now(); double end_time = begin_time + 1000; int hc_iter = 0; int hc_accept = 0; static uint16_t bpid_cnt[N * N * M]; while (marathon::now() < end_time) { hc_iter++; vector new_bomb_places = bomb_places; auto new_fillcnt = fillcnt; vector notfilled; int new_score = old_score; int rmnum = marathon::randint(4, 6); for (int r = 0; r < rmnum; r++) { int remove_bpid = marathon::random_choice(new_bomb_places); new_bomb_places.erase(std::remove(new_bomb_places.begin(), new_bomb_places.end(), remove_bpid), new_bomb_places.end()); new_score += bpid2cost[remove_bpid]; for (int pt : bpid2walls[remove_bpid]) { new_fillcnt[pt]--; if (new_fillcnt[pt] == 0) notfilled.push_back(pt); } } while (notfilled.size()) { // この中の処理が非常に重い for (int bpid = 0; bpid < N * N * M; bpid++) { bpid_cnt[bpid] = 0; } for (auto wall : notfilled) { for (auto bpid : wall2bpids[wall]) { bpid_cnt[bpid]++; } } int bestbpid = 0; int bestscore = -inf; for (int bpid = 0; bpid < N * N * M; bpid++) { if (bpid_cnt[bpid] > 0) { // int score = -1000 * marathon::uniform(1.0, 2.0) * bpid2cost[bpid] / bpid_cnt[bpid]; int score = -1000 * bpid2cost[bpid] / bpid_cnt[bpid]; if (bestscore < score) { bestscore = score; bestbpid = bpid; } } } for (auto wall : bpid2walls[bestbpid]) { new_fillcnt[wall]++; if (new_fillcnt[wall] == 1) { notfilled.erase(std::remove(notfilled.begin(), notfilled.end(), wall), notfilled.end()); } } new_score -= bpid2cost[bestbpid]; new_bomb_places.push_back(bestbpid); } if (new_score > old_score) { old_score = new_score; bomb_places = new_bomb_places; fillcnt = new_fillcnt; hc_accept++; } } debug2(hc_iter, hc_accept); return bomb_places; } vector make_bomb_places() { // 詰み防止のため建物がなくなるまで破壊してはいけない店舗を1個だけ決めておく vector is_hub_shop(N2); { point center = {12, 12}; pair minshop = {inf, {-1, -1}}; for (int r = 0; r < N; r++) { for (int c = 0; c < N; c++) { if (TBL_INIT[r][c] == '@') { point now = {r, c}; int distance = abs(center.r - now.r) + abs(center.c - now.c); minshop = min(minshop, {distance, now}); } } } is_hub_shop[_p(minshop.second)] = true; } vector> wall2bpids(N2); // buildingと書くのがタイプ数多いのでwallとしておく vector> bpid2walls(N * N * M); for (int r = 0; r < N; r++) { for (int c = 0; c < N; c++) { for (int b = 0; b < M; b++) { point place = point{r, c}; int bpid = bomb_place_id(place, b); // ハブ店を破壊する爆弾配置は除外する bool valid = true; for (auto drc : BOMBS[b].ranges) { point pt = place + drc; if (!ingrid(pt)) continue; if (is_hub_shop[_p(pt)]) valid = false; } if (!valid) continue; for (auto drc : BOMBS[b].ranges) { point pt = place + drc; if (!ingrid(pt)) continue; if (TBL_INIT[pt.r][pt.c] != '#') continue; bpid2walls[bpid].push_back(_p(pt)); wall2bpids[_p(pt)].push_back(bpid); } } } } // 適当な初期解 vector bomb_places; vector fillcnt(N2); { while (true) { pair bestbpid = {-inf, -inf}; for (int bpid = 0; bpid < N * N * M; bpid++) { int cnt = 0; for (auto wall : bpid2walls[bpid]) { if (fillcnt[wall] == 0) cnt++; } if (cnt > 0) { double score = -1.0 * BOMBS[bpid2bombid(bpid)].cost / cnt; bestbpid = max(bestbpid, {score, bpid}); } } if (bestbpid.second == -inf) break; bomb_places.push_back(bestbpid.second); for (auto wall : bpid2walls[bestbpid.second]) { fillcnt[wall]++; } } } return hc_bomb_places(bomb_places, fillcnt, bpid2walls, wall2bpids); } double calc_tsp_cost(vector &route, vector &bomb_places) { point hulk = {0, 0}; vector> tbl(N, vector(N)); for (int r = 0; r < N; r++) { for (int c = 0; c < N; c++) { tbl[r][c] = TBL_INIT[r][c]; } } int cost = 0; int m = route.size(); for (int r = 1; r < int(route.size()) - 1; r++) { int bpid = bomb_places[route[r]]; point bombpt = bpid2point(bpid); auto cost_hulk = dij_cost(hulk, tbl); auto cost_bomb = dij_cost(bombpt, tbl); int now_mincost = inf; for (int r = 0; r < N; r++) { for (int c = 0; c < N; c++) { if (tbl[r][c] == '@') { now_mincost = min(now_mincost, cost_hulk[r][c] + cost_bomb[r][c] * 4); } } } for (auto drc : BOMBS[bpid2bombid(bpid)].ranges) { point pt = bombpt + drc; if (!ingrid(pt)) continue; tbl[pt.r][pt.c] = '.'; } cost += now_mincost; hulk = bombpt; } return cost; } vector bomb_places_tsp(vector bomb_places) { int m = bomb_places.size() + 2; int start = m - 2; int goal = m - 1; vector best_route; double best_score = -1e18; int route_search_iter = 0; while (marathon::now() < 2400) { route_search_iter++; // コスト固定のTSP vector route; route.push_back(start); { vector perm(m - 2); iota(perm.begin(), perm.end(), 0); shuffle(perm.begin(), perm.end(), marathon::engine); for (auto r : perm) { route.push_back(r); } } route.push_back(goal); point ini = {0, 0}; vector> costtbl(m, vector(m)); for (int i = 0; i < m - 1; i++) { for (int j = 0; j < m - 1; j++) { point src = i == start ? ini : bpid2point(bomb_places[i]); point dest = i == start ? ini : bpid2point(bomb_places[j]); costtbl[i][j] = abs(src.r - dest.r) + abs(src.c - dest.c); } } double begin_temp = 5; double end_temp = 0.5; double old_score = 0; for (int r = 1; r < int(route.size()); r++) { old_score -= costtbl[route[r - 1]][route[r]]; } int anneal_accepted = 0; int anneal_iter = 0; while (anneal_iter < 50000) { anneal_iter++; vector new_route = route; int x = 0; int y = 0; while (x == y) { x = 1 + marathon::engine() % (int(new_route.size()) - 2); y = 1 + marathon::engine() % (int(new_route.size()) - 2); } if (x > y) { swap(x, y); } for (int i = x + 1; i <= y - 1; i++) { int j = y - 1 - (i - (x + 1)); if (i > j) break; swap(new_route[i], new_route[j]); } double new_score = 0; for (int r = 1; r < int(new_route.size()); r++) { new_score -= costtbl[new_route[r - 1]][new_route[r]]; } if (marathon::anneal_accept(new_score, old_score, anneal_iter, 0, 50000, begin_temp, end_temp)) { route = new_route; old_score = new_score; anneal_accepted++; } } // 正しいコストで評価 double now_score = -calc_tsp_cost(route, bomb_places); if (best_score < now_score) { best_route = route; best_score = now_score; debug1(best_score); } } debug2(route_search_iter, best_score); vector result; for (int r = 1; r < int(best_route.size()) - 1; r++) { result.push_back(bomb_places[best_route[r]]); } return result; } void solve() { // 爆弾位置と爆弾IDからなる「爆弾配置」 の集合を山登りにより作る。 // 爆弾配置は N*N*M 種類存在し、そこから全ての建物を破壊しつつ爆弾コストが小さい集合を選ぶ。 vector bomb_places = make_bomb_places(); vector ops; { point hulk = {0, 0}; vector> tbl(N, vector(N)); for (int r = 0; r < N; r++) { for (int c = 0; c < N; c++) { tbl[r][c] = TBL_INIT[r][c]; } } { // 爆弾配置が決まったら、コストが動的に変化するTSP的な問題を解く。  bomb_places = bomb_places_tsp(bomb_places); for (auto bpid : bomb_places) { point midshop = {-1, -1}; { // TODO 2個同時に運ぶのをDP的にやるとよい可能性がある auto hulk_cost = dij_cost(hulk, tbl); auto bp_cost = dij_cost(bpid2point(bpid), tbl); pair bestmidshop = {inf, {inf, inf}}; for (int r = 0; r < N; r++) { for (int c = 0; c < N; c++) { if (tbl[r][c] == '@') { int cost = hulk_cost[r][c] + bp_cost[r][c] * 4; pair now = {cost, {r, c}}; bestmidshop = min(now, bestmidshop); } } } midshop = bestmidshop.second; } int bombid = bpid2bombid(bpid); point bombpt = bpid2point(bpid); dij_goto_ops(hulk, midshop, ops, tbl); ops.push_back({2, bombid}); dij_goto_ops(hulk, bombpt, ops, tbl); ops.push_back({3, bombid}); for (auto drc : BOMBS[bombid].ranges) { point pt = hulk + drc; if (!ingrid(pt)) continue; tbl[pt.r][pt.c] = '.'; } } } // 残った店を貪欲に破壊 while (true) { vector shops; for (int r = 0; r < N; r++) { for (int c = 0; c < N; c++) { if (tbl[r][c] == '@') shops.push_back({r, c}); } } if (shops.size() == 0) break; pair bestop = {1e18, -1}; auto hulk_cost = dij_cost(hulk, tbl); vector> midshop(N, vector(N, {-1, -1})); vector> move_cost(N, vector(N, inf)); for (auto shop : shops) { auto shop_cost = dij_cost(shop, tbl); for (int r = 0; r < N; r++) { for (int c = 0; c < N; c++) { int cost = shop_cost[r][c] * 4 + hulk_cost[shop.r][shop.c]; if (move_cost[r][c] > cost) { move_cost[r][c] = cost; midshop[r][c] = shop; } } } } for (int r = 0; r < N; r++) { for (int c = 0; c < N; c++) { point place = {r, c}; for (int bid = 0; bid < M; bid++) { int break_shop = 0; int break_hub_shop = 0; for (auto drc : BOMBS[bid].ranges) { point pt = place + drc; if (!ingrid(pt)) continue; if (tbl[pt.r][pt.c] == '@') break_shop++; } // (移動コスト+爆弾コスト)/破壊する店数 が最小の爆弾配置を選ぶ double score = 1.0 * (move_cost[r][c] + BOMBS[bid].cost) / break_shop; bestop = min(bestop, {score, bomb_place_id(place, bid)}); } } } { int bpid = bestop.second; int bombid = bpid / N2; point bombpt = bpid2point(bpid); dij_goto_ops(hulk, midshop[bombpt.r][bombpt.c], ops, tbl); ops.push_back({2, bombid}); dij_goto_ops(hulk, bombpt, ops, tbl); ops.push_back({3, bombid}); for (auto drc : BOMBS[bombid].ranges) { point pt = hulk + drc; if (!ingrid(pt)) continue; tbl[pt.r][pt.c] = '.'; } } } } { cout << ops.size() << endl; for (auto op : ops) { if (op.kind == 1) { char d = '.'; if (op.xyz == _L_) d = 'L'; if (op.xyz == _R_) d = 'R'; if (op.xyz == _U_) d = 'U'; if (op.xyz == _D_) d = 'D'; cout << op.kind << " " << d << endl; } else { cout << op.kind << " " << op.xyz + 1 << endl; } } } { int move_cost, bomb_cost; tie(move_cost, bomb_cost) = evaluate_ops(ops); int totalcost = move_cost + bomb_cost; int score = 1e12 / (1e4 + totalcost); cerr << "score==" << score << " move_cost==" << move_cost << " bomb_cost==" << bomb_cost << " time==" << marathon::now() << endl; } } int main() { marathon::marathon_init(); int n, m; cin >> n >> m; for (int r = 0; r < N; r++) { for (int c = 0; c < N; c++) { cin >> TBL_INIT[r][c]; } } for (int b = 0; b < M; b++) { int c, l; cin >> c >> l; bomb_t bomb; bomb.cost = c; for (int i = 0; i < l; i++) { int dr, dc; cin >> dr >> dc; bomb.ranges.push_back({dr, dc}); } BOMBS[b] = bomb; } init_data(); solve(); }