結果

問題 No.5015 Escape from Labyrinth
ユーザー 👑 platinumplatinum
提出日時 2023-04-04 12:16:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 14,046 bytes
コンパイル時間 3,942 ms
コンパイル使用メモリ 262,100 KB
実行使用メモリ 5,316 KB
スコア 1,080
最終ジャッジ日時 2023-04-15 11:34:44
合計ジャッジ時間 225,100 ms
ジャッジサーバーID
(参考情報)
judge16 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 AC 2,188 ms
4,828 KB
testcase_62 WA -
testcase_63 WA -
testcase_64 WA -
testcase_65 WA -
testcase_66 WA -
testcase_67 WA -
testcase_68 WA -
testcase_69 WA -
testcase_70 WA -
testcase_71 WA -
testcase_72 WA -
testcase_73 WA -
testcase_74 WA -
testcase_75 WA -
testcase_76 WA -
testcase_77 WA -
testcase_78 WA -
testcase_79 WA -
testcase_80 WA -
testcase_81 WA -
testcase_82 WA -
testcase_83 WA -
testcase_84 WA -
testcase_85 WA -
testcase_86 WA -
testcase_87 WA -
testcase_88 WA -
testcase_89 WA -
testcase_90 WA -
testcase_91 WA -
testcase_92 WA -
testcase_93 WA -
testcase_94 WA -
testcase_95 WA -
testcase_96 WA -
testcase_97 WA -
testcase_98 WA -
testcase_99 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using pii = pair<int,int>;
const int INF = (int)1e9;

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

const int testcase = 1;

enum class file_status{
	local,
	score,
	submit,
};
file_status now_status = file_status::submit;

template<typename T>
struct v2{
    std::vector<std::vector<T>> v;
    v2(){}
    v2(int x){
        v.resize(x);
    }
    v2(int x, int y){
        v.resize(x, std::vector<T>(y));
    }
	v2(int x, int y, T val){
		v.resize(x, std::vector<T>(y, val));
	}
    std::vector<T>& operator [] (const int num){
        return v[num];
    }
    void init(int x){
        v.resize(x);
    }
    void init(int x, int y){
        v.resize(x, std::vector<T>(y));
    }
	void init(int x, int y, T val){
		v.resize(x, std::vector<T>(y, val));
	}
    void push_back(std::vector<T>& e){
        v.emplace_back(e);
    }
    int size(){
        return (int)v.size();
    }
    bool empty(){
        return v.empty();
    }
    void clear(){
        v.clear();
    }
    void erase(int pos){
        v.erase(v.begin() + pos);
    }
};

template<typename T>
struct v3{
    std::vector<v2<T>> v;
    v3(){}
    v3(int x){
        v.resize(x);
    }
    v3(int x, int y){
        v.resize(x, v2<T>(y));
    }
    v3(int x, int y, int z){
        v.resize(x, v2<T>(y, z));
    }
	v3(int x, int y, int z, T val){
		v.resize(x, v2<T>(y, z, val));
	}
    v2<T>& operator [] (const int num){
        return v[num];
    }
    void init(int x, int y){
        v.resize(x, v2<T>(y));
    }
    void init(int x, int y, int z){
        v.resize(x, v2<T>(y, z));
    }
    void push_back(v2<T>& e){
        v.emplace_back(e);
    }
    bool empty(){
        return v.empty();
    }
    void clear(){
        v.clear();
    }
};

void read_input(){
	std::stringstream ss;
	std::string num = std::to_string(testcase);
	int siz = num.size();
	for(int i = 0; i < 3 - siz; i++) num = '0' + num;
	ss << "in/testcase_" << 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 < 3 - siz; i++) num = '0' + num;
	ss << "out/testcase_" << num << ".txt";
	FILE *out = freopen(ss.str().c_str(), "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;
v2<Cell> grid;
v2<int> enemy_number;

int sy, sx, ky, kx, gy, gx;

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 get_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){
	v2<int> dist(N, 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){
	v2<int> dist(N, 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 = get_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 = get_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);
}

// ゴールまでの最小の体力減少値をダイクストラ法で見積もる
pair<int,string> CalcMinDistance(int start_t, pii start, pii goal){
	pair<int,string> res;
	// 最小公倍数 = 60 倍の頂点を用意する
	const int lcm = 60;
	v3<int> dist(N, N, lcm, INF);
	v3<char> prev_dir(N, N, lcm, 'S');
	int sy = start.first, sx = start.second;
	dist[sy][sx][start_t%lcm] = 0;
	priority_queue<pii,vector<pii>,greater<pii>> pq;
	pq.emplace(0, sy * N * N + sx * N + start_t % lcm);
	while(!pq.empty()){
		pii pos = pq.top(); pq.pop();
		int d = pos.first;
		int y = pos.second / (N * N);
		int x = pos.second / N - y * N;
		int t = pos.second % N;
		if(d != dist[y][x][t]) continue;
		// 上下左右への移動またはその場にとどまる
		for(int k = 0; k < 5; k++){
			int ny = y + dy[k], nx = x + dx[k];
			if(range_out(ny, nx)) continue;
			int damage = CalcDamage(ny, nx, t + 1);
			int nd = d + damage;
			int nt = (t + 1) % lcm;
			if(nd >= dist[ny][nx][nt]) continue;
			dist[ny][nx][nt] = nd;
			if(k < 4) prev_dir[ny][nx][nt] = dir[k];
			pq.emplace(nd, ny * N * N + nx * N + nt);
		}
	}
	int dist_min = INF, goal_t = -1;
	for(int t = 0; t < lcm; t++){
		int dist_t = dist[goal.first][goal.second][t];
		if(dist_min > dist_t){
			dist_min = dist_t;
			goal_t = t;
		}
	}
	res.first = dist_min;
	assert(goal_t != -1);
	// 経路復元
	string path;
	int now_y = goal.first, now_x = goal.second, now_t = goal_t;
	while(now_y != sy || now_x != sx){
		char c = prev_dir[now_y][now_x][now_t];
		path.push_back(c);
		int k = -1;
		if(c == 'S') k = 4;
		else{
			k = get_direction(c);
			k ^= 1;
		}
		now_y += dy[k]; now_x += dx[k];
		now_t = (now_t - 1 + lcm) % lcm;
	}
	reverse(path.begin(), path.end());
	return res;
}

void input(){
	cin >> N >> D >> H;
	S.resize(N);
	for(int i = 0; i < N; i++) cin >> S[i];
	enemy_number.init(N, 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.init(N, N, cell_status::empty);
	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;
			}
		}
	}
	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;
			}
		}
	}
}

int main(){
	if(now_status == file_status::local){
		read_input();
		file_output();
	}
	input();
	Player player(sy, sx, H);

	while(true){
		string path = get_jewel(player);
		if(path.empty()){
			cerr << "no jewel" << endl;
			break;
		}
		string action = DecideAction(path[0], player, turn);
		StepTurn(player, action);

		if(player.hp < 500 && turn % 50 == 0){
			pair<int,string> to_key = CalcMinDistance(
			turn, pii(player.y, player.x), pii(ky, kx));
			int siz = to_key.second.size();
			int key_to_goal = CalcMinDistance(turn + siz,
			pii(ky, kx), pii(gy, gx)).first;
			int player_to_goal = to_key.first + key_to_goal;
			if(player_to_goal > player.hp + 50) break;
		}
	}

	string path_to_key = CalcMinDistance(turn, pii(player.y, player.x)
	, pii(ky, kx)).second;
	for(auto &c : path_to_key){
		string get_key = DecideAction(c, player, turn);
		StepTurn(player, get_key);
	}
	string path_to_goal = CalcMinDistance(turn, pii(player.y, player.x)
	, pii(gy, gx)).second;
	for(auto &c : path_to_goal){
		string escape = DecideAction(c, 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