結果
問題 | No.5017 Tool-assisted Shooting |
ユーザー | platinum |
提出日時 | 2023-07-05 23:50:28 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 11,679 bytes |
コンパイル時間 | 1,862 ms |
コンパイル使用メモリ | 129,976 KB |
実行使用メモリ | 24,384 KB |
スコア | 0 |
最終ジャッジ日時 | 2023-07-16 13:35:40 |
合計ジャッジ時間 | 23,736 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge12 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | TLE | - |
testcase_01 | TLE | - |
testcase_02 | TLE | - |
testcase_03 | TLE | - |
testcase_04 | TLE | - |
testcase_05 | TLE | - |
testcase_06 | TLE | - |
testcase_07 | TLE | - |
testcase_08 | TLE | - |
testcase_09 | TLE | - |
testcase_10 | TLE | - |
testcase_11 | TLE | - |
testcase_12 | TLE | - |
testcase_13 | TLE | - |
testcase_14 | TLE | - |
testcase_15 | TLE | - |
testcase_16 | TLE | - |
testcase_17 | TLE | - |
testcase_18 | TLE | - |
testcase_19 | TLE | - |
testcase_20 | TLE | - |
testcase_21 | TLE | - |
testcase_22 | TLE | - |
testcase_23 | TLE | - |
testcase_24 | TLE | - |
testcase_25 | TLE | - |
testcase_26 | TLE | - |
testcase_27 | TLE | - |
testcase_28 | TLE | - |
testcase_29 | TLE | - |
testcase_30 | TLE | - |
testcase_31 | TLE | - |
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 | -- | - |
testcase_50 | -- | - |
testcase_51 | -- | - |
testcase_52 | -- | - |
testcase_53 | -- | - |
testcase_54 | -- | - |
testcase_55 | -- | - |
testcase_56 | -- | - |
testcase_57 | -- | - |
testcase_58 | -- | - |
testcase_59 | -- | - |
testcase_60 | -- | - |
testcase_61 | -- | - |
testcase_62 | -- | - |
testcase_63 | -- | - |
testcase_64 | -- | - |
testcase_65 | -- | - |
testcase_66 | -- | - |
testcase_67 | -- | - |
testcase_68 | -- | - |
testcase_69 | -- | - |
testcase_70 | -- | - |
testcase_71 | -- | - |
testcase_72 | -- | - |
testcase_73 | -- | - |
testcase_74 | -- | - |
testcase_75 | -- | - |
testcase_76 | -- | - |
testcase_77 | -- | - |
testcase_78 | -- | - |
testcase_79 | -- | - |
testcase_80 | -- | - |
testcase_81 | -- | - |
testcase_82 | -- | - |
testcase_83 | -- | - |
testcase_84 | -- | - |
testcase_85 | -- | - |
testcase_86 | -- | - |
testcase_87 | -- | - |
testcase_88 | -- | - |
testcase_89 | -- | - |
testcase_90 | -- | - |
testcase_91 | -- | - |
testcase_92 | -- | - |
testcase_93 | -- | - |
testcase_94 | -- | - |
testcase_95 | -- | - |
testcase_96 | -- | - |
testcase_97 | -- | - |
testcase_98 | -- | - |
testcase_99 | -- | - |
ソースコード
#include <iostream> #include <vector> #include <queue> #include <string> #include <sstream> #include <random> #include <cassert> using namespace std; using pii = pair<int,int>; constexpr int width = 25; // フィールドの幅 constexpr int height = 60; // フィールドの高さ constexpr int max_turn = 1000; // ゲームの最大ターン数 constexpr int level_up = 100; // レベルアップに必要なパワー値 constexpr int min_P = 1; constexpr int max_P = 8; constexpr int INF = (int)1e9; constexpr int testcase = 0; bool submit = true; void read_input(){ std::stringstream ss; std::string num = std::to_string(testcase); int siz = num.size(); for(int i = 0; i < 4 - siz; i++) num = '0' + num; ss << "in/" << num << ".txt"; FILE *in = freopen(ss.str().c_str(), "r", stdin); } void file_output(){ std::stringstream ss; std::string num = std::to_string(testcase); int siz = num.size(); for(int i = 0; i < 4 - siz; i++) num = '0' + num; ss << "out_mcts/" << num << ".txt"; FILE *out = freopen(ss.str().c_str(), "w", stdout); } // 敵機の情報 struct Enemy { int init_hp; int h, p; int x, y; Enemy(int h, int p, int x) : h(h), p(p), x(x) { init_hp = h; y = height - 1; } }; void MoveEnemy(vector<queue<Enemy>>& enemies) { for(int x = 0; x < width; x++) { int siz = enemies[x].size(); for(int i = 0; i < siz; i++) { Enemy enemy = enemies[x].front(); enemies[x].pop(); enemy.y--; if(enemy.y < 0) continue; enemies[x].push(enemy); } } } // 自機の情報 struct Player { int x, y; int score; int power, level; Player(int x, int y) : x(x), y(y) { score = 0; power = 0; level = 1; } void destroy_enemy(const Enemy& e) { score += e.init_hp; power += e.p; level = 1 + power / level_up; } bool try_move(const vector<queue<Enemy>>& enemies, int move) const { // 目の前に倒せる敵がいるなら動かない if(!enemies[x].empty()) { const Enemy& enemy = enemies[x].front(); int des = (enemy.h + level - 1) / level; if(enemy.y >= des) { if(move == 0) return true; else return false; } } int nx = (x + move + width) % width; if(enemies[nx].empty()) return true; const Enemy& enemy = enemies[nx].front(); int des = (enemy.h + level - 1) / level; if(enemy.y >= des) return true; // 目の前の敵を倒せないのに動かないことを許可しない else if(move == 0) return false; // 次のターン確実に当たるならfalse if(enemy.y <= 1) return false; // 次のターン包囲されるならfalse else if(enemy.y == 2) { int lx = (nx - 1 + width) % width, rx = (nx + 1) % width; if(enemies[lx].empty() || enemies[rx].empty()) { return true; } const Enemy& left_enemy = enemies[lx].front(); const Enemy& right_enemy = enemies[rx].front(); if(left_enemy.y <= 2 && right_enemy.y <= 2) { return false; } else return true; } else return true; } void apply_move(int move, bool output = false) { if(output) { const string actions = "LSR"; std::cout << actions[move+1] << std::endl; //std::cout << "# " << c << std::endl; } x = (x + move + width) % width; } }; // 敵機の攻撃処理、与えたダメージが返る int AttackEnemy(Player& player, vector<queue<Enemy>>& enemies) { if(enemies[player.x].empty()) return 0; Enemy& target_enemy = enemies[player.x].front(); int damage = min(target_enemy.h, player.level); target_enemy.h -= player.level; if(target_enemy.h <= 0) { player.destroy_enemy(target_enemy); enemies[player.x].pop(); } return damage; } struct beta_distribution { double alpha, beta; beta_distribution(double a, double b) : alpha (a), beta(b) {} void update(bool appeared) { if(appeared) alpha += 1.0; else beta += 1.0; } double mu() { return alpha / (alpha + beta); } double var() { bool tot = alpha + beta; return alpha * beta / tot / tot / (tot + 1.0); } }; vector<beta_distribution> Beta(width, {1.0, 24.0}); random_device rnd; mt19937 engine(rnd()); uniform_int_distribution<> cent(1, 100); struct State { int now_turn, depth; //double score; Player player; vector<queue<Enemy>> enemies; vector<int> legal_actions; // L : -1, S : 0, R : 1 void check_actions() { legal_actions.clear(); for(int move = -1; move <= 1; move++) { if(player.try_move(enemies, move)) { legal_actions.emplace_back(move); } } } State(int now_turn, int depth, const Player& player, const vector<queue<Enemy>>& enemies) : now_turn(now_turn), depth(depth), player(player), enemies(enemies) { check_actions(); } void advance(int move) { this->now_turn++; this->depth--; this->player.apply_move(move); AttackEnemy(this->player, this->enemies); MoveEnemy(this->enemies); // ベイズ推定した確率に基づいてランダムに敵機を生成する int T = now_turn; for(int x = 0; x < width; x++) { int poss = Beta[x].mu() * 100; poss = max(poss, min_P); poss = min(poss, max_P); if(cent(engine) <= poss) { double d_hp = 7.5 + 0.15 * T; double d_power = d_hp * 0.8; int hp = max(1, (int)d_hp); int power = max(0, (int)d_power); this->enemies[x].emplace(hp, power, x); } } check_actions(); } bool is_end() { if(now_turn >= max_turn) return true; if(depth <= 0) return true; return false; } bool random_action() { if(legal_actions.empty()) return false; if(is_end()) return false; int choice = legal_actions.size(); uniform_int_distribution<> choose_action(0, choice - 1); int move = choose_action(engine); advance(legal_actions[move]); return true; } }; double RandomPlayout(State& state) { while(state.random_action()); return state.player.score; } constexpr double C = 10000; constexpr int EXPAND_THRESHOLD = 20; class Node { private: State state; double value; public: vector<Node> child_nodes; int trial; Node(const State& state) : state(state) { value = 0.0; trial = 0; } double evaluate() { if(this->state.is_end()) { double res = this->state.player.score; this->value += res; this->trial++; return res; } if(this->child_nodes.empty()) { State copy_state = this->state; double res = RandomPlayout(copy_state); this->value += res; this->trial++; if(this->trial == EXPAND_THRESHOLD) { this->expand(); } return res; } else { double res = this->NextChildNode().evaluate(); this->value += res; this->trial++; return res; } } bool expand() { auto legal_actions = this->state.legal_actions; if(legal_actions.empty()) return false;; this->child_nodes.clear(); for(const auto move : legal_actions) { this->child_nodes.emplace_back(this->state); this->child_nodes.back().state.advance(move); } return true; } Node& NextChildNode() { // 試行回数が0なら優先的に選択 for(auto& child_node : this->child_nodes) { if(child_node.trial == 0) { return child_node; } } // UCB1に基づいてノードを選択 double total_trial = 0.0; for(auto& child_node : this->child_nodes) { total_trial += child_node.trial; } double best_value = -INF; int arg_best = -1; for(int i = 0; i < (int)this->child_nodes.size(); i++) { const auto& child_node = this->child_nodes[i]; double ucb1 = child_node.value / (double)child_node.trial + C * sqrt(2.0 * log(total_trial) / (double)child_node.trial); if(ucb1 > best_value) { best_value = ucb1; arg_best = i; } } return this->child_nodes[arg_best]; } }; int MCTS(const State& state, const int playout) { Node root_node = Node(state); // 合法手がない場合は諦める if(!root_node.expand()) { return 0; } for(int i = 0; i < playout; i++) { root_node.evaluate(); } auto legal_actions = state.legal_actions; int max_trial = -1; int arg_best = -1; for(int i = 0; i < (int)legal_actions.size(); i++) { int trial = root_node.child_nodes[i].trial; if(trial > max_trial) { max_trial = trial; arg_best = i; } } return legal_actions[arg_best]; } int main(){ if(!submit) { read_input(); file_output(); } Player player(12, 0); vector<queue<Enemy>> enemies(width); vector<int> P(width); if(!submit) { for(int i = 0; i < width; i++) cin >> P[i]; } for(int T = 1; T <= max_turn; T++) { // 敵機の移動 MoveEnemy(enemies); // 入力の受け取り int n; cin >> n; if(n == -1) return 0; vector<int> appeared(width); for(int i = 0; i < n; i++) { int h, p, x; cin >> h >> p >> x; enemies[x].emplace(h, p, x); appeared[x] = 1; } for(int x = 0; x < width; x++) { Beta[x].update(appeared[x]); } // 自機の移動 // 後半はMCTSで決定する if(T >= 800) { int depth = 50; // MCTSの深さを設定 State state(T, depth, player, enemies); int playout = 300; // MCTSのプレイアウト数を設定 int next_move = MCTS(state, playout); player.apply_move(next_move, true); } else { // 敵機を倒せない列には移動しない vector<pii> front_enemy(width, {-1, -1}); for(int x = 0; x < width; x++) { if(enemies[x].empty()) continue; int dx1 = abs(x - player.x); int dx2 = abs(x - (player.x - width)); int dx = max(1, min(dx1, dx2)); const Enemy& enemy = enemies[x].front(); int des = dx - 1 + (enemy.h + player.level - 1) / player.level; if(enemy.y >= des) { front_enemy[x] = {enemy.y, des}; } } // 一番早く倒せる敵を探す const Enemy* near_enemy = nullptr; int min_dist = INF; for(int x = 0; x < width; x++) { int y = front_enemy[x].first; if(y == -1) continue; int dist = front_enemy[x].second; if(dist < min_dist) { min_dist = dist; near_enemy = &enemies[x].front(); } } // 倒せる敵がいない場合 if(!near_enemy) { if(player.try_move(enemies, 0)) { player.apply_move(0, true); } else if(player.try_move(enemies, -1)) { player.apply_move(-1, true); } else if(player.try_move(enemies, 1)) { player.apply_move(1, true); } else player.apply_move(0, true); } // 同列に倒せる敵がいるならとどまる else if(near_enemy->x == player.x) { player.apply_move(0, true); } // 敵が左にいる場合 else if(near_enemy->x < player.x) { int ex = near_enemy->x, px = player.x; int left = px - ex, right = ex - (px - width); if(left < right) { if(player.try_move(enemies, -1)) { player.apply_move(-1, true); } else if(player.try_move(enemies, 0)) { player.apply_move(0, true); } else player.apply_move(1, true); } else { if(player.try_move(enemies, 1)) { player.apply_move(1, true); } else if(player.try_move(enemies, 0)) { player.apply_move(0, true); } else player.apply_move(-1, true); } } // 敵が右にいる場合 else { int ex = near_enemy->x, px = player.x; int left = px + width - ex, right = ex - px; if(left < right) { if(player.try_move(enemies, -1)) { player.apply_move(-1, true); } else if(player.try_move(enemies, 0)) { player.apply_move(0, true); } else player.apply_move(1, true); } else { if(player.try_move(enemies, 1)) { player.apply_move(1, true); } else if(player.try_move(enemies, 0)) { player.apply_move(0, true); } else player.apply_move(-1, true); } } /* if(near_enemy) { cout << "# " << near_enemy->x << " " << near_enemy->y << endl; cout << "# hp = " << near_enemy->h << endl; } */ } // 自機の攻撃 AttackEnemy(player, enemies); } std::cerr << "turn = " << max_turn + 1 << endl; std::cerr << "score = " << player.score << endl; return 0; }