結果

問題 No.5015 Escape from Labyrinth
ユーザー 👑 platinumplatinum
提出日時 2023-03-11 00:25:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 32 ms / 3,000 ms
コード長 10,053 bytes
コンパイル時間 2,970 ms
コンパイル使用メモリ 232,284 KB
実行使用メモリ 4,376 KB
スコア 108,390
最終ジャッジ日時 2023-04-15 11:31:02
合計ジャッジ時間 9,749 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
4,372 KB
testcase_01 AC 28 ms
4,372 KB
testcase_02 AC 17 ms
4,372 KB
testcase_03 AC 19 ms
4,368 KB
testcase_04 AC 20 ms
4,372 KB
testcase_05 AC 20 ms
4,368 KB
testcase_06 AC 17 ms
4,372 KB
testcase_07 AC 16 ms
4,368 KB
testcase_08 AC 17 ms
4,372 KB
testcase_09 AC 19 ms
4,368 KB
testcase_10 AC 28 ms
4,368 KB
testcase_11 AC 27 ms
4,368 KB
testcase_12 AC 20 ms
4,372 KB
testcase_13 AC 20 ms
4,372 KB
testcase_14 AC 29 ms
4,368 KB
testcase_15 AC 25 ms
4,368 KB
testcase_16 AC 24 ms
4,376 KB
testcase_17 AC 15 ms
4,368 KB
testcase_18 AC 16 ms
4,372 KB
testcase_19 AC 15 ms
4,372 KB
testcase_20 AC 29 ms
4,372 KB
testcase_21 AC 24 ms
4,372 KB
testcase_22 AC 23 ms
4,372 KB
testcase_23 AC 21 ms
4,368 KB
testcase_24 AC 17 ms
4,372 KB
testcase_25 AC 23 ms
4,372 KB
testcase_26 AC 21 ms
4,368 KB
testcase_27 AC 20 ms
4,368 KB
testcase_28 AC 19 ms
4,368 KB
testcase_29 AC 15 ms
4,368 KB
testcase_30 AC 17 ms
4,368 KB
testcase_31 AC 16 ms
4,372 KB
testcase_32 AC 29 ms
4,372 KB
testcase_33 AC 15 ms
4,372 KB
testcase_34 AC 25 ms
4,372 KB
testcase_35 AC 19 ms
4,372 KB
testcase_36 AC 17 ms
4,368 KB
testcase_37 AC 21 ms
4,368 KB
testcase_38 AC 20 ms
4,372 KB
testcase_39 AC 17 ms
4,372 KB
testcase_40 AC 23 ms
4,368 KB
testcase_41 AC 25 ms
4,368 KB
testcase_42 AC 20 ms
4,372 KB
testcase_43 AC 13 ms
4,368 KB
testcase_44 AC 19 ms
4,368 KB
testcase_45 AC 20 ms
4,368 KB
testcase_46 AC 22 ms
4,368 KB
testcase_47 AC 13 ms
4,368 KB
testcase_48 AC 23 ms
4,372 KB
testcase_49 AC 32 ms
4,372 KB
testcase_50 AC 25 ms
4,368 KB
testcase_51 AC 18 ms
4,368 KB
testcase_52 AC 24 ms
4,368 KB
testcase_53 AC 15 ms
4,372 KB
testcase_54 AC 23 ms
4,372 KB
testcase_55 AC 24 ms
4,372 KB
testcase_56 AC 17 ms
4,368 KB
testcase_57 AC 19 ms
4,372 KB
testcase_58 AC 23 ms
4,368 KB
testcase_59 AC 18 ms
4,368 KB
testcase_60 AC 16 ms
4,368 KB
testcase_61 AC 15 ms
4,368 KB
testcase_62 AC 16 ms
4,372 KB
testcase_63 AC 21 ms
4,368 KB
testcase_64 AC 24 ms
4,372 KB
testcase_65 AC 22 ms
4,372 KB
testcase_66 AC 29 ms
4,372 KB
testcase_67 AC 19 ms
4,368 KB
testcase_68 AC 22 ms
4,368 KB
testcase_69 AC 14 ms
4,368 KB
testcase_70 AC 20 ms
4,368 KB
testcase_71 AC 20 ms
4,372 KB
testcase_72 AC 27 ms
4,368 KB
testcase_73 AC 13 ms
4,372 KB
testcase_74 AC 19 ms
4,372 KB
testcase_75 AC 15 ms
4,372 KB
testcase_76 AC 19 ms
4,372 KB
testcase_77 AC 20 ms
4,368 KB
testcase_78 AC 14 ms
4,368 KB
testcase_79 AC 17 ms
4,368 KB
testcase_80 AC 25 ms
4,368 KB
testcase_81 AC 20 ms
4,368 KB
testcase_82 AC 17 ms
4,368 KB
testcase_83 AC 17 ms
4,368 KB
testcase_84 AC 15 ms
4,372 KB
testcase_85 AC 26 ms
4,368 KB
testcase_86 AC 14 ms
4,368 KB
testcase_87 AC 17 ms
4,372 KB
testcase_88 AC 18 ms
4,368 KB
testcase_89 AC 14 ms
4,372 KB
testcase_90 AC 22 ms
4,372 KB
testcase_91 AC 22 ms
4,372 KB
testcase_92 AC 17 ms
4,368 KB
testcase_93 AC 15 ms
4,372 KB
testcase_94 AC 16 ms
4,368 KB
testcase_95 AC 25 ms
4,368 KB
testcase_96 AC 18 ms
4,368 KB
testcase_97 AC 14 ms
4,372 KB
testcase_98 AC 22 ms
4,368 KB
testcase_99 AC 21 ms
4,372 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:422:75: 警告: ‘gx’ may be used uninitialized [-Wmaybe-uninitialized]
  422 |                 string path_to_goal = find_path(player.y, player.x, gy, gx);
      |                                                                           ^
main.cpp:387:33: 備考: ‘gx’ はここで定義されています
  387 |         int sy, sx, ky, kx, gy, gx;
      |                                 ^~
main.cpp:422:75: 警告: ‘gy’ may be used uninitialized [-Wmaybe-uninitialized]
  422 |                 string path_to_goal = find_path(player.y, player.x, gy, gx);
      |                                                                           ^
main.cpp:387:29: 備考: ‘gy’ はここで定義されています
  387 |         int sy, sx, ky, kx, gy, gx;
      |                             ^~
main.cpp:415:74: 警告: ‘kx’ may be used uninitialized [-Wmaybe-uninitialized]
  415 |                 string path_to_key = find_path(player.y, player.x, ky, kx);
      |                                                                          ^
main.cpp:387:25: 備考: ‘kx’ はここで定義されています
  387 |         int sy, sx, ky, kx, gy, gx;
      |                         ^~
main.cpp:415:74: 警告: ‘ky’ may be used uninitialized [-Wmaybe-uninitialized]
  415 |                 string path_to_key = find_path(player.y, player.x, ky, kx);
      |                                                                          ^
main.cpp:387:21: 備考: ‘ky’ はここで定義されています
  387 |         int sy, sx, ky, kx, gy, gx;
      |                     ^~
コンストラクタ ‘Player::Player(int, int, int)’ 内,
    inlined from ‘int main()’ at main.cpp:401:25:
main.cpp:71:46: 警告: ‘sx’ may be used uninitialized [-Wmaybe-uninitialized]
   71 |         Player(int y, int x, int hp) : y(y), x(x), hp(hp){
      |                                              ^~~~
main.cpp: 関数 ‘

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

const int grid_size = 60;
const int max_hp = 1500;
const int jewel_value = 10;
int dy[4] = {-1, 1, 0, 0};
int dx[4] = {0, 0, -1, 1};
string dir = "UDLR";

void file_input(){
	FILE *in = freopen("in/testcase_057.txt", "r", stdin);
}
void file_output(){
	FILE *out = freopen("out/testcase_057.txt", "w", stdout);
}

// 探知機の情報
struct Enemy{
	int y, x, d, num;
	bool destroyed;
	Enemy(int y, int x, int d, int num){
		this->y = y, this->x = x, this->d = d;
		this-> num = num;
		destroyed = false;
	}
};

// セルの情報の候補
enum class cell_status{
    empty,
    wall,
    block,
    enemy,
    key,
    goal,
    fire,
    jewel,
};

// セルの情報
struct Cell{
	cell_status status;
	Enemy* e_pointer = nullptr;
	Cell(cell_status st = cell_status::empty) : status(st) {}
};

int N, D, H, M;
vector<Enemy> enemy;
vector<string> S;
vector<vector<Cell>> grid;
vector<vector<int>> enemy_number;

vector<string> ans;
int turn = 0;

// 範囲外かどうか
bool range_out(int y, int x){
	if(y < 0 || y >= grid_size) return true;
	if(x < 0 || x >= grid_size) return true;
	return false;
}

// プレイヤーの情報
struct Player{
	int y, x;
	int hp, fire;
	int score;
	bool get_key, using_magic;
	Player(int y, int x, int hp) : y(y), x(x), hp(hp){
		fire = 0;
		score = 0;
		get_key = false;
		using_magic = false;
	}
	int move(int k){
		int ny = y + dy[k], nx = x + dx[k];
		if(range_out(ny, nx)) return -1;
		if(grid[ny][nx].status == cell_status::wall
		|| grid[ny][nx].status == cell_status::block
		|| grid[ny][nx].status == cell_status::enemy){
			return -1;
		}
		if(grid[ny][nx].status == cell_status::fire){
			fire++;
		}
		else if(grid[ny][nx].status == cell_status::jewel){
			score += jewel_value;
		}
		else if(grid[ny][nx].status == cell_status::key){
			get_key = true;
		}
		y = ny; x = nx;
		return 0; 
	}
	void damage(int d){
		hp -= d;
	}
};

// 方向を取得
int direction(char d){
	for(int k = 0; k < 4; k++){
		if(d == dir[k]) return k;
	}
	return -1;
}

// BFSによる経路探索
string find_path(int sy, int sx, int gy, int gx){
	vector<vector<int>> dist(N, vector<int>(N, -1));
	dist[sy][sx] = 0;
	queue<pair<int,int>> q;
	q.emplace(sy, sx);
	while(!q.empty()){
		pair<int,int> p = q.front(); q.pop();
		int y = p.first, x = p.second;
		for(int k = 0; k < 4; k++){
			int ny = y + dy[k], nx = x + dx[k];
			if(range_out(ny, nx)) continue;
			if(dist[ny][nx] != -1) continue;
			cell_status cell = grid[ny][nx].status;
			if(cell == cell_status::wall || cell == cell_status::enemy){
				continue;
			}
			dist[ny][nx] = dist[y][x] + 1;
			q.emplace(ny, nx);
		}
	}
	string res;
	if(dist[gy][gx] == -1) return res;
	int now_y = gy, now_x = gx, now_d = dist[gy][gx];
	while(now_y != sy || now_x != sx){
		bool moved = false;
		for(int k = 0; k < 4; k++){
			int new_y = now_y + dy[k], new_x = now_x + dx[k];
			if(range_out(new_y, new_x)) continue;
			if(dist[new_y][new_x] != now_d - 1) continue;
			now_y = new_y, now_x = new_x;
			now_d--;
			res.push_back(dir[k^1]);
			moved = true;
			break;
		}
		assert(moved);
	}
	reverse(res.begin(), res.end());
	return res;
}

// 一番近い宝石を取りに行く
string get_jewel(Player &player){
	vector<vector<int>> dist(N, vector<int>(N, -1));
	int sy = player.y, sx = player.x;
	dist[sy][sx] = 0;
	queue<pair<int,int>> q;
	q.emplace(sy, sx);
	int gy = -1, gx = -1;

	while(!q.empty()){
		pair<int,int> p = q.front(); q.pop();
		int y = p.first, x = p.second;
		bool find_jewel = false;
		for(int k = 0; k < 4; k++){
			int ny = y + dy[k], nx = x + dx[k];
			if(range_out(ny, nx)) continue;
			if(dist[ny][nx] != -1) continue;
			cell_status cell = grid[ny][nx].status;
			if(cell == cell_status::wall || cell == cell_status::enemy){
				continue;
			}
			dist[ny][nx] = dist[y][x] + 1;
			if(cell == cell_status::jewel){
				gy = ny; gx = nx;
				find_jewel = true;
				break;
			}
			q.emplace(ny, nx);
		}
		if(find_jewel) break;
	}

	string res;
	if(gy == -1) return res;
	int now_y = gy, now_x = gx, now_d = dist[gy][gx];
	while(now_y != sy || now_x != sx){
		bool moved = false;
		for(int k = 0; k < 4; k++){
			int new_y = now_y + dy[k], new_x = now_x + dx[k];
			if(range_out(new_y, new_x)) continue;
			if(dist[new_y][new_x] != now_d - 1) continue;
			now_y = new_y, now_x = new_x;
			now_d--;
			res.push_back(dir[k^1]);
			moved = true;
			break;
		}
		assert(moved);
	}
	reverse(res.begin(), res.end());
	return res;	
}

// プレイヤーへのダメージ計算
int CalcDamage(int py, int px, int t){
    int res = 1;
    for(int k = 0; k < 4; k++){
        int y = py + dy[k], x = px + dx[k];
        while(!range_out(y, x)){
            if(grid[y][x].status == cell_status::wall ||
               grid[y][x].status == cell_status::block){
                break;
            }
            if(grid[y][x].status == cell_status::enemy){
                if(t > 0 && t % grid[y][x].e_pointer->d == 0){
                    res += D;
                }
                break;
            }
            y += dy[k]; x += dx[k];
        }
    }
    return res;
}

// 次の行動を決定
string DecideAction(char move_dir, Player &player, int t){
	int y = player.y, x = player.x;
	t++;
	int j = direction(move_dir);
	if(grid[y+dy[j]][x+dx[j]].status == cell_status::block){
		string action = "B ";
		action += move_dir;
		return action;		
	}
	int stay_damage = CalcDamage(y, x, t);
	int move_damage = CalcDamage(y + dy[j], x + dx[j], t);
	if(move_damage == 1){
		string action = "M ";
		action += move_dir;
		return action;
	}
	if(stay_damage == 1){
		return "S";
	}
	else{
		for(int k = 0; k < 4; k++){
			int ny = y + dy[k], nx = x + dx[k];
			while(!range_out(ny, nx)){
				if(grid[ny][nx].status == cell_status::wall
				|| grid[ny][nx].status == cell_status::block){
					break;
				}
				if(grid[ny][nx].status == cell_status::enemy
				&& t % grid[ny][nx].e_pointer->d == 0){
					if(player.fire > 0){
						string move = "F ";
						move += dir[k];
						return move;
					}
					else if(k != j && grid[y+dy[k]][x+dx[k]].status
					== cell_status::empty){
						string move = "B ";
						move += dir[k];
						return move;
					}
					break;
				}
				if(grid[ny][nx].status == cell_status::enemy){
					break;
				}
				ny += dy[k], nx += dx[k];
			}
		}
	}
	// 行動が決まらない場合
	if(move_damage <= stay_damage){
		string action = "M ";
		action += move_dir;
		return action;		
	}
	else return "S";
}

int Action(Player &player, string &query){
	if(query[0] == 'S' || query[0] == '#') return 0;
	assert((int)query.size() == 3);
	int sy = player.y, sx = player.x;
	char c = query[0], d = query[2];
	int k = direction(d);
	if(c == 'M'){
		int res = player.move(k);
		if(res == -1) return -1;
		int ny = sy + dy[k], nx = sx + dx[k];
		if(grid[ny][nx].status != cell_status::goal){
			grid[ny][nx].status = cell_status::empty;
		}
	}
	else if(c == 'B'){
		int ny = sy + dy[k], nx = sx + dx[k];
		if(range_out(ny, nx)) return -1;
		if(grid[ny][nx].status == cell_status::empty){
			grid[ny][nx].status = cell_status::block;
		}
		else if(grid[ny][nx].status == cell_status::block){
			grid[ny][nx].status = cell_status::empty;
		}
		else return -1;
	}
	else if(c == 'F'){
		if(player.fire <= 0) return -1;
		player.fire--;
		int ny = sy + dy[k], nx = sx + dx[k];
		while(!range_out(ny, nx)){
			if(grid[ny][nx].status == cell_status::wall
			|| grid[ny][nx].status == cell_status::block){
				break;
			}
			if(grid[ny][nx].status == cell_status::enemy){
				if(grid[ny][nx].e_pointer->destroyed) return -1;
				grid[ny][nx].status = cell_status::empty;
				grid[ny][nx].e_pointer->destroyed = true;
				break;
			}
			ny += dy[k], nx += dx[k];
		}
	}
	return 0;
}

void StepTurn(Player &player, string &query){
	Action(player, query);
	if(query[0] != '#'){
		turn++;
		player.damage(CalcDamage(player.y, player.x, turn));
	}
	ans.emplace_back(query);
}

void input(){
	cin >> N >> D >> H;
	S.resize(N);
	for(int i = 0; i < N; i++) cin >> S[i];
	enemy_number.resize(N, vector<int>(N, -1));
	cin >> M;
	for(int i = 0; i < M; i++){
		int y, x, d;
		cin >> y >> x >> d;
		enemy.emplace_back(y, x, d, i);
		enemy_number[y][x] = i;
	}
	grid.resize(N, vector<Cell>(N));
	for(int i = 0; i < N; i++){
		for(int j = 0; j < N; j++){
			if(S[i][j] == '#'){
				grid[i][j].status = cell_status::wall;
			}
			else if(S[i][j] == 'E'){
				grid[i][j].status = cell_status::enemy;
				grid[i][j].e_pointer = &enemy[enemy_number[i][j]];
			}
			else if(S[i][j] == 'K'){
				grid[i][j].status = cell_status::key;
			}
			else if(S[i][j] == 'G'){
				grid[i][j].status = cell_status::goal;
			}
			else if(S[i][j] == 'F'){
				grid[i][j].status = cell_status::fire;
			}
			else if(S[i][j] == 'J'){
				grid[i][j].status = cell_status::jewel;
			}
			else{
				grid[i][j].status = cell_status::empty;
			}
		}
	}
}

int main(){
	//file_input();
	//file_output();
	input();

	int sy, sx, ky, kx, gy, gx;
	for(int i = 0; i < N; i++){
		for(int j = 0; j < N; j++){
			if(S[i][j] == 'S'){
				sy = i, sx = j;
			}
			else if(S[i][j] == 'K'){
				ky = i, kx = j;
			}
			else if(S[i][j] == 'G'){
				gy = i, gx = j;
			}
		}
	}
	Player player(sy, sx, H);

	while(player.hp > 400 + D * 50){
		string path = get_jewel(player);
		if(path.empty()){
			cerr << "no jewel" << endl;
			break;
		}
		string action = DecideAction(path[0], player, turn);
		StepTurn(player, action);
	}
	//cerr << __LINE__ << endl;

	while(player.y != ky || player.x != kx){
		string path_to_key = find_path(player.y, player.x, ky, kx);
		string find_key = DecideAction(path_to_key[0], player, turn);
		StepTurn(player, find_key);
	}
	//cerr << __LINE__ << endl;

	while(player.y != gy || player.x != gx){
		string path_to_goal = find_path(player.y, player.x, gy, gx);
		string escape = DecideAction(path_to_goal[0], player, turn);
		StepTurn(player, escape);
	}

	cerr << "score = " << player.score << endl;
	cerr << "hp = " << player.hp << endl;
	for(auto s : ans){
		cout << s << endl;
		cout << "#" << s << endl;
	}

	return 0;
}
0