結果
問題 | No.5017 Tool-assisted Shooting |
ユーザー |
![]() |
提出日時 | 2023-07-07 00:14:23 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,670 ms / 2,000 ms |
コード長 | 12,154 bytes |
コンパイル時間 | 1,999 ms |
コンパイル使用メモリ | 130,772 KB |
実行使用メモリ | 24,348 KB |
スコア | 3,945,524 |
平均クエリ数 | 990.00 |
最終ジャッジ日時 | 2023-07-16 13:43:28 |
合計ジャッジ時間 | 153,598 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge13 |
純コード判定しない問題か言語 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 100 |
ソースコード
#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 lx = (nx - 1 + width) % width, rx = (nx + 1) % width;int des = (enemy.h + level - 1) / level;if(enemy.y >= des) return true;// 目の前の敵を倒せない場合は左右に動けるなら動くelse if(move == 0) {if(enemies[lx].empty() || enemies[rx].empty()) return false;if(enemies[lx].front().y > 1 || enemies[rx].front().y > 1) return false;}// 次のターン確実に当たるならfalseif(enemy.y <= 1) return false;// 次のターン包囲されるならfalseelse if(enemy.y <= 3) {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 : 1void 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 int EXPAND_THRESHOLD = 20;class Node {private:State state;double value;double C;public:vector<Node> child_nodes;int trial;Node(const State& state) : state(state) {value = 0.0;trial = 0;C = state.player.score;}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) {std::cerr << "turn = " << T << std::endl;std::cerr << "score = " << player.score << std::endl;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 >= 750) {int depth = 30; // MCTSの深さを設定State state(T, depth, player, enemies);int playout = 200; // 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;double max_value = -1.0;for(int x = 0; x < width; x++) {int y = front_enemy[x].first;if(y == -1) continue;int des = front_enemy[x].second;double value = 0.0;if(T < 400) {value = (double)enemies[x].front().p / des;}else {value = (double)enemies[x].front().init_hp / des;}if(value > max_value) {max_value = value;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;}