結果

問題 No.5019 Hakai Project
ユーザー wanuiwanui
提出日時 2023-11-17 18:33:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 10,461 bytes
コンパイル時間 3,403 ms
コンパイル使用メモリ 247,732 KB
実行使用メモリ 17,192 KB
スコア 0
最終ジャッジ日時 2023-11-17 18:33:41
合計ジャッジ時間 12,620 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int getmove(point, point)':
main.cpp:99:1: warning: control reaches end of non-void function [-Wreturn-type]
   99 | }
      | ^

ソースコード

diff #

#include <bits/stdc++.h>
// clang-format off
using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair<ll,ll>; const ll INF=4e18;
void print0(){}; template<typename H,typename... T> void print0(H h,T... t){cout<<h;print0(t...);}
void print(){print0("\n");}; template<typename H,typename... T>void print(H h,T... t){print0(h);if(sizeof...(T)>0)print0(" ");print(t...);}
#define debug1(a) { cerr<<#a<<":"<<a<<endl; }
#define debug2(a,b) { cerr<<#a<<":"<<a<<" "<<#b<<":"<<b<<endl; }
#define debug3(a,b,c) { cerr<<#a<<":"<<a<<" "<<#b<<":"<<b<<" "<<#c<<":"<<c<<endl; }
#define debug4(a,b,c,d) { cerr<<#a<<":"<<a<<" "<<#b<<":"<<b<<" "<<#c<<":"<<c<<" "<<#d<<":"<<d<<endl; }

struct point {int r; int c; };
bool operator==(const point &lhs, const point &rhs) { return (lhs.r == rhs.r && lhs.c == rhs.c); }
bool operator!=(const point &lhs, const point &rhs) { return !(lhs == rhs); }
bool operator<(const point &lhs, const point &rhs) {
    if (lhs.r != rhs.r){return lhs.r<rhs.r;}
    return lhs.c<rhs.c;
}
point operator-(const point &self){
    return {-self.r, -self.c};
}
point operator+(const point &lhs, const point &rhs){
    return {lhs.r+rhs.r, lhs.c+rhs.c};
}
point operator-(const point &lhs, const point &rhs){
    return lhs + (-rhs);
}
std::ostream &operator<<(std::ostream &os, point &pt) {
    string s;
    s = "(" + to_string(int(pt.r)) + ", " + to_string(int(pt.c)) + ")";
    return os << s;
};
const int inf = 1e9;
// 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 <typename T>
T random_choice(vector<T> &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) {
    const int ANNEAL_RND = 1e8;
    const double ANNEAL_EPS = 1e-6;
    double temp = (begin_temp * (end_time - cur_time) + end_temp * (cur_time - begin_time)) / (end_time - begin_time);
    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<point> 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;
vector<point> mvs = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
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);
}
vector<vector<int>> dijcost(point ini, vector<vector<char>> &tbl) {
    priority_queue<pair<int, point>, vector<pair<int, point>>, greater<pair<int, point>>> pq;
    pq.push({0, ini});
    vector<vector<int>> costs(N, vector<int>(N, inf));
    while (pq.size()) {
        int cost;
        point u;
        tie(cost, u) = pq.top();
        pq.pop();
        if (costs[u.r][u.c] <= cost) continue;
        costs[u.r][u.c] = cost;
        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;
            }
            // if (costs[v.r][v.c] <= nc) continue;
            // costs[v.r][v.c] = nc;
            pq.push({nc, v});
        }
    }
    return costs;
}
void dij_goto(point &hulk, point dest, vector<operation_t> &ops, vector<vector<char>> &tbl) {
    priority_queue<tuple<int, point, point>, vector<tuple<int, point, point>>, greater<tuple<int, point, point>>> pq;
    pq.push({0, hulk, {-1, -1}});
    vector<vector<int>> costs(N, vector<int>(N, inf));
    vector<vector<point>> froms(N, vector<point>(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<point> 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;
}
void solve() {
    point hulk = {0, 0};
    vector<vector<char>> tbl(N, vector<char>(N));
    for (int r = 0; r < N; r++) {
        for (int c = 0; c < N; c++) {
            tbl[r][c] = TBL_INIT[r][c];
        }
    }

    vector<operation_t> ops;
    for (int breakloop = 0; true; breakloop++) {
        debug2(breakloop, marathon::now());
        vector<point> shops;
        vector<point> walls;
        for (int r = 0; r < N; r++) {
            for (int c = 0; c < N; c++) {
                if (tbl[r][c] == '@') shops.push_back({r, c});
                if (tbl[r][c] == '#') walls.push_back({r, c});
            }
        }
        if (shops.size() == 0) break;
        // (移動コスト+爆弾コスト)/破壊個数 が一番小さい
        tuple<point, point, int> bestop = {{-1, -1}, {-1, -1}, -1};
        {
            double bestscore = -1e18;
            auto hulk_cost = dijcost(hulk, tbl);
            vector<vector<point>> midshop(N, vector<point>(N, {-1, -1}));
            vector<vector<int>> move_cost(N, vector<int>(N, inf));
            for (auto shop : shops) {
                auto costs_from_shop = dijcost(shop, tbl);
                for (int r = 0; r < N; r++) {
                    for (int c = 0; c < N; c++) {
                        int cost = costs_from_shop[r][c] * 4 + hulk_cost[r][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 now = {r, c};
                    for (int bid = 0; bid < M; bid++) {
                        int cost = move_cost[r][c] + BOMBS[bid].cost;

                        int break_wall = 0;
                        int break_shop = 0;

                        for (auto drc : BOMBS[bid].ranges) {
                            point pt = now + drc;
                            if (!ingrid(pt)) continue;
                            if (tbl[pt.r][pt.c] == '#') {
                                break_wall++;
                            }
                            if (tbl[pt.r][pt.c] == '@') {
                                break_shop++;
                            }

                            double score = 0;
                            if (int(shops.size()) - break_shop >= 20) {
                                if (break_wall == 0) continue;
                                score = 1.0 * (-cost) / break_wall;
                            } else if (shops.size() >= 2 && walls.size() > 0) {
                                if (break_wall == 0) continue;
                                score = 1.0 * (-cost - 10000 * break_shop) / break_wall;
                            } else {
                                if (break_wall + break_shop == 0) continue;
                                // score = 1.0 * (break_wall + 0.01 * break_shop) / (break_wall + break_shop);
                                score = 1.0 * (-cost) / (break_wall + break_shop);
                            }
                            if (bestscore < score) {
                                bestscore = score;
                                bestop = {now, midshop[now.r][now.c], bid};
                            }
                        }
                    }
                }
            }
        }
        {
            point midshop, bombpt;
            int bombid;
            tie(bombpt, midshop, bombid) = bestop;
            dij_goto(hulk, midshop, ops, tbl);
            ops.push_back({2, bombid});
            dij_goto(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 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;
    }
    solve();
}
0