結果

問題 No.5015 Escape from Labyrinth
ユーザー platinumplatinum
提出日時 2023-04-08 21:34:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,337 ms / 3,000 ms
コード長 20,074 bytes
コンパイル時間 5,174 ms
コンパイル使用メモリ 281,560 KB
実行使用メモリ 118,452 KB
スコア 148,300
最終ジャッジ日時 2023-04-15 11:39:54
合計ジャッジ時間 121,686 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,211 ms
52,956 KB
testcase_01 AC 867 ms
44,824 KB
testcase_02 AC 867 ms
48,476 KB
testcase_03 AC 834 ms
58,492 KB
testcase_04 AC 843 ms
34,112 KB
testcase_05 AC 2,337 ms
96,600 KB
testcase_06 AC 865 ms
52,468 KB
testcase_07 AC 875 ms
37,772 KB
testcase_08 AC 1,233 ms
39,252 KB
testcase_09 AC 836 ms
32,416 KB
testcase_10 AC 1,163 ms
71,432 KB
testcase_11 AC 810 ms
32,624 KB
testcase_12 AC 930 ms
63,500 KB
testcase_13 AC 823 ms
47,816 KB
testcase_14 AC 1,163 ms
29,356 KB
testcase_15 AC 2,001 ms
118,452 KB
testcase_16 AC 1,954 ms
91,236 KB
testcase_17 AC 827 ms
29,528 KB
testcase_18 AC 828 ms
53,736 KB
testcase_19 AC 857 ms
37,092 KB
testcase_20 AC 925 ms
51,684 KB
testcase_21 AC 880 ms
48,224 KB
testcase_22 AC 1,801 ms
60,220 KB
testcase_23 AC 1,726 ms
66,028 KB
testcase_24 AC 807 ms
31,140 KB
testcase_25 AC 1,214 ms
91,248 KB
testcase_26 AC 1,492 ms
70,080 KB
testcase_27 AC 831 ms
32,344 KB
testcase_28 AC 930 ms
46,824 KB
testcase_29 AC 917 ms
41,912 KB
testcase_30 AC 1,332 ms
99,840 KB
testcase_31 AC 1,192 ms
68,016 KB
testcase_32 AC 912 ms
68,016 KB
testcase_33 AC 945 ms
53,060 KB
testcase_34 AC 1,304 ms
46,320 KB
testcase_35 AC 1,261 ms
62,212 KB
testcase_36 AC 1,940 ms
90,568 KB
testcase_37 AC 990 ms
72,824 KB
testcase_38 AC 855 ms
48,744 KB
testcase_39 AC 762 ms
34,616 KB
testcase_40 AC 1,333 ms
94,684 KB
testcase_41 AC 789 ms
29,796 KB
testcase_42 AC 908 ms
50,624 KB
testcase_43 AC 881 ms
44,396 KB
testcase_44 AC 863 ms
48,460 KB
testcase_45 AC 2,272 ms
117,192 KB
testcase_46 AC 1,295 ms
50,636 KB
testcase_47 AC 865 ms
39,644 KB
testcase_48 AC 781 ms
35,452 KB
testcase_49 AC 1,155 ms
28,096 KB
testcase_50 AC 1,583 ms
91,880 KB
testcase_51 AC 951 ms
63,700 KB
testcase_52 AC 847 ms
42,432 KB
testcase_53 AC 848 ms
56,324 KB
testcase_54 AC 809 ms
35,688 KB
testcase_55 AC 1,138 ms
60,088 KB
testcase_56 AC 935 ms
61,384 KB
testcase_57 AC 822 ms
37,228 KB
testcase_58 AC 1,539 ms
76,820 KB
testcase_59 AC 779 ms
30,648 KB
testcase_60 AC 993 ms
91,728 KB
testcase_61 AC 1,093 ms
61,716 KB
testcase_62 AC 861 ms
48,236 KB
testcase_63 AC 807 ms
38,952 KB
testcase_64 AC 1,069 ms
38,876 KB
testcase_65 AC 1,294 ms
83,736 KB
testcase_66 AC 937 ms
66,844 KB
testcase_67 AC 1,661 ms
99,568 KB
testcase_68 AC 940 ms
46,624 KB
testcase_69 AC 850 ms
42,036 KB
testcase_70 AC 926 ms
51,448 KB
testcase_71 AC 1,033 ms
41,556 KB
testcase_72 AC 1,320 ms
62,720 KB
testcase_73 AC 797 ms
31,476 KB
testcase_74 AC 920 ms
40,608 KB
testcase_75 AC 1,017 ms
89,436 KB
testcase_76 AC 877 ms
52,732 KB
testcase_77 AC 865 ms
34,148 KB
testcase_78 AC 800 ms
30,484 KB
testcase_79 AC 814 ms
38,492 KB
testcase_80 AC 924 ms
46,592 KB
testcase_81 AC 887 ms
48,808 KB
testcase_82 AC 735 ms
24,440 KB
testcase_83 AC 829 ms
42,996 KB
testcase_84 AC 1,338 ms
62,000 KB
testcase_85 AC 1,498 ms
62,784 KB
testcase_86 AC 1,114 ms
55,524 KB
testcase_87 AC 841 ms
38,112 KB
testcase_88 AC 1,281 ms
50,384 KB
testcase_89 AC 864 ms
43,456 KB
testcase_90 AC 956 ms
68,436 KB
testcase_91 AC 936 ms
59,108 KB
testcase_92 AC 963 ms
59,820 KB
testcase_93 AC 796 ms
23,784 KB
testcase_94 AC 1,386 ms
66,296 KB
testcase_95 AC 2,007 ms
106,376 KB
testcase_96 AC 1,805 ms
108,312 KB
testcase_97 AC 875 ms
43,568 KB
testcase_98 AC 845 ms
44,396 KB
testcase_99 AC 1,053 ms
28,748 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
次のファイルから読み込み:  /usr/local/gcc7/include/c++/12.2.0/bits/stl_algobase.h:64,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/bits/specfun.h:45,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/cmath:1935,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/x86_64-pc-linux-gnu/bits/stdc++.h:41,
         次から読み込み:  main.cpp:1:
コンストラクタ ‘constexpr std::pair<_T1, _T2>::pair(_U1&&, _U2&&) [with _U1 = int&; _U2 = int&; typename std::enable_if<(std::_PCC<true, _T1, _T2>::_MoveConstructiblePair<_U1, _U2>() && std::_PCC<true, _T1, _T2>::_ImplicitlyMoveConvertiblePair<_U1, _U2>()), bool>::type <anonymous> = true; _T1 = int; _T2 = int]’ 内,
    inlined from ‘int main()’ at main.cpp:796:44:
/usr/local/gcc7/include/c++/12.2.0/bits/stl_pair.h:535:42: 警告: ‘gx’ may be used uninitialized [-Wmaybe-uninitialized]
  535 |         : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { }
      |                                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp: 関数 ‘int main()’ 内:
main.cpp:701:33: 備考: ‘gx’ はここで定義されています
  701 |         int sy, sx, ky, kx, gy, gx;
      |                                 ^~
コンストラクタ ‘constexpr std::pair<_T1, _T2>::pair(_U1&&, _U2&&) [with _U1 = int&; _U2 = int&; typename std::enable_if<(std::_PCC<true, _T1, _T2>::_MoveConstructiblePair<_U1, _U2>() && std::_PCC<true, _T1, _T2>::_ImplicitlyMoveConvertiblePair<_U1, _U2>()), bool>::type <anonymous> = true; _T1 = int; _T2 = int]’ 内,
    inlined from ‘int main()’ at main.cpp:796:44:
/usr/local/gcc7/include/c++/12.2.0/bits/stl_pair.h:535:11: 警告: ‘gy’ may be used uninitialized [-Wmaybe-uninitialized]
  535 |         : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp: 関数 ‘int main()’ 内:
main.cpp:701:29: 備考: ‘gy’ 

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

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

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 = "UDLRS";

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,
	cell_kinds,
};
int GetCellStatusNumber(cell_status st){
	if(st == cell_status::empty) return 0;
	else if(st == cell_status::wall) return 1;
	else if(st == cell_status::block) return 2;
	else if(st == cell_status::enemy) return 3;
	else if(st == cell_status::key) return 4;
	else if(st == cell_status::goal) return 5;
	else if(st == cell_status::fire) return 6;
	else if(st == cell_status::jewel) return 7;
	else if(st == cell_status::cell_kinds) return 8;
	return -1;
}

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

// ダイクストラ法で用いる情報
struct pathway{
	int dist;
	int y, x, t;
	long long hash;
	pathway(int d, int y, int x, int t, long long h) :
	dist(d), y(y), x(x), t(t), hash(h) {}
	bool operator> (const pathway& p) const{
		return dist > p.dist;
	}
};

//盤面をハッシュ化
random_device rnd;
mt19937 engine(rnd());
vector<long long> hash_list;
void hash_init(){
	uniform_int_distribution<> h(1, (int)1e9);
	int kinds_num = GetCellStatusNumber(cell_status::cell_kinds);
	for(int i = 0; i < grid_size * grid_size * kinds_num; i++){
		long long val1 = h(engine);
		long long val2 = h(engine);
		hash_list.emplace_back(val1 * val2);
	}
}
long long get_hash(int y, int x, cell_status st){
	int kinds_num = GetCellStatusNumber(cell_status::cell_kinds);
	int num = (y * grid_size + x) * kinds_num + GetCellStatusNumber(st);
	return hash_list[num];
}
long long zobrist_hash(v2<Cell>& now_grid){
	long long res = 0;
	for(int i = 0; i < grid_size; i++){
		for(int j = 0; j < grid_size; j++){
			res ^= get_hash(i, j, now_grid[i][j].status);
		}
	}
	return res;
}
long long hash_update(int ny, int nx, v2<Cell>& now_grid){
	long long res = 0;
	long long o = get_hash(ny, nx, now_grid[ny][nx].status);
	long long n = get_hash(ny, nx, cell_status::empty);
	res = o ^ n;
	return res;
}
unordered_map<long long, v2<Cell>> Grids;

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

vector<string> ans;
int turn = 0;

void output(){
	for(auto s : ans){
		cout << s << endl;
		cout << "#" << s << endl;
	}
}

// 範囲外かどうか
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, bool block = true){
    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){
				break;
			}
            if(grid[y][x].status == cell_status::block && 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;
}
int CalcDamage(int py, int px, int t, v2<Cell>& now_grid){
    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(now_grid[y][x].status == cell_status::wall){
				break;
			}
            if(now_grid[y][x].status == cell_status::block){
                break;
            }
            if(now_grid[y][x].status == cell_status::enemy){
                if(t > 0 && t % now_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'){
		if(player.move(k) == -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 -2;
		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 -2;
	}
	else if(c == 'F'){
		if(player.fire <= 0) return -3;
		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 -4;
				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){
	int res = Action(player, query);
	if(res != 0){
		cerr << "Action Error : " << res << endl;
		//ans.emplace_back(query);
		//output();
	}
	//assert(res == 0);
	if(query[0] != '#'){
		turn++;
		player.damage(CalcDamage(player.y, player.x, turn));
	}
	ans.emplace_back(query);
}

// ゴールまでの最小の体力減少値をダイクストラ法で見積もる
pair<int,vector<string>> CalcMinDistance(int start_t, pii start, pii goal, bool debug = false){
	pair<int,vector<string>> res;
	// 最小公倍数 = 60 倍の頂点を用意する
	const int lcm = 60;
	start_t %= lcm;
	v3<int> dist(N, N, lcm, INF);
	v3<string> prev_dir(N, N, lcm, "S");
	int start_y = start.first, start_x = start.second;
	int goal_y = goal.first, goal_x = goal.second;
	dist[start_y][start_x][start_t] = 0;
	priority_queue<pathway,vector<pathway>,greater<pathway>> pq;
	// 初期盤面をハッシュ化しておく
	long long init_hash = zobrist_hash(grid);
	Grids[init_hash] = grid;
	pq.emplace(0, start_y, start_x, start_t, init_hash);
	while(!pq.empty()){
		pathway pos = pq.top(); pq.pop();
		int d = pos.dist;
		int y = pos.y;
		int x = pos.x;
		if(y == goal_y && x == goal_x) continue;
		int t = pos.t;
		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;
			cell_status stat = Grids[pos.hash][ny][nx].status;
			if(stat == cell_status::enemy || stat == cell_status::wall){
				continue;
			}
			// ブロックを破壊する場合
			if(stat == cell_status::block && k < 4){
				v2<Cell> now_grid = Grids[pos.hash];
				long long change = hash_update(ny, nx, Grids[pos.hash]);
				long long new_hash = pos.hash ^ change;
				now_grid[ny][nx].status = cell_status::empty;

				int damage = CalcDamage(y, x, t + 1, now_grid)
				+ CalcDamage(ny, nx, t + 2, now_grid);
				int nd = d + damage;
				int nt = (t + 2) % lcm;
				if(nd >= dist[ny][nx][nt]) continue;
				dist[ny][nx][nt] = nd;
				prev_dir[ny][nx][nt] = dir[k];
				prev_dir[ny][nx][nt].push_back(dir[k]);

				Grids[new_hash] = now_grid;
				pq.emplace(nd, ny, nx, nt, new_hash);
			}
			// 単に移動する場合
			else{
				int damage = CalcDamage(ny, nx, t + 1, Grids[pos.hash]);
				int nd = d + damage;
				int nt = (t + 1) % lcm;
				if(nd >= dist[ny][nx][nt]) continue;
				dist[ny][nx][nt] = nd;
				prev_dir[ny][nx][nt] = dir[k];
				pq.emplace(nd, ny, nx, nt, pos.hash);
			}
		}
	}
	int dist_min = INF, goal_t = -1;
	for(int t = 0; t < lcm; t++){
		int dist_t = dist[goal_y][goal_x][t];
		if(dist_min > dist_t){
			dist_min = dist_t;
			goal_t = t;
		}
	}
	res.first = dist_min;
	assert(goal_t != -1);
	// 経路復元
	vector<string> queries;
	int now_y = goal_y, now_x = goal_x, now_t = goal_t;
	if(debug) cout << "#debug start" << endl;
	while(now_y != start_y || now_x != start_x || now_t != start_t){
		if(debug){
			cout << "#t = " << now_t << endl;
			cout << "#(" << now_y  << ", " << now_x << ")" << endl;
			cout << "#d = " << dist[now_y][now_x][now_t] << endl;
		}
		char c = prev_dir[now_y][now_x][now_t][0];
		string query = "M ";
		if(c == 'S') query = c;
		else query += c;
		queries.push_back(query);
		int k = -1;
		if(c == 'S') k = 4;
		else{
			k = get_direction(c);
			k ^= 1;
		}
		// ブロックを破壊した場合
		if(prev_dir[now_y][now_x][now_t].size() > 1){
			c = prev_dir[now_y][now_x][now_t][1];
			string query = "B ";
			query += c;
			queries.push_back(query);
			now_t = (now_t - 1 + lcm) % lcm;
		}
		now_y += dy[k]; now_x += dx[k];
		now_t = (now_t - 1 + lcm) % lcm;
	}
	if(debug){
		cout << "#t = " << now_t << endl;
		cout << "#(" << now_y  << ", " << now_x << ")" << endl;
		cout << "#d = " << dist[now_y][now_x][now_t] << endl;
	}
	if(debug) cout << "#debug end" << endl;
	reverse(queries.begin(), queries.end());
	res.second = queries;
	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;
			}
		}
	}
}

int main(){
	if(now_status == file_status::local){
		read_input();
		file_output();
	}
	input();
	hash_init();
	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);

	int dist_check = -1;
	while(true){
		int dy = abs(player.y - ky), dx = abs(player.x - kx);
		if(!player.get_key && dy + dx <= 20){
			pair<int,vector<string>> to_key = CalcMinDistance(
			turn, pii(player.y, player.x), pii(ky, kx));
			for(auto &query : to_key.second){
				StepTurn(player, query);
			}			
		}
		else{
			string path = get_jewel(player);
			if(path.empty()){
				cerr << "no jewel" << endl;
				break;
			}
			string query = DecideAction(path[0], player, turn);
			StepTurn(player, query);
		}

		if(player.hp < 500 && dist_check == -1){
			if(!player.get_key){
				pair<int,vector<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;
				dist_check = key_to_goal + to_key.first;
			}
			else{
				int to_goal = CalcMinDistance(turn,
				pii(ky, kx), pii(gy, gx)).first;
				dist_check = to_goal;				
			}
		}

		if(dist_check == -1) continue;
		if(player.hp < dist_check + 100 && turn % 20 == 0){
			double time = 1.0 * clock() / CLOCKS_PER_SEC;
			if(time > TL){
				cerr << "time = " << time << endl;
				cerr << "turn = " << turn << endl;
				cerr << "hp = " << player.hp << endl;
				break;
			}

			int player_to_goal = 0;
			if(!player.get_key){
				pair<int,vector<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;
				player_to_goal = to_key.first + key_to_goal;
			}
			else{
				int to_goal = CalcMinDistance(turn,
				pii(ky, kx), pii(gy, gx)).first;
				player_to_goal = to_goal;				
			}
			if(player_to_goal + D * 10 + 50 > player.hp){
				cerr << "turn = " << turn << endl;
				cerr << "hp = " << player.hp << endl;
				cerr << "dist = " << player_to_goal << endl;
				break;
			}
		}
	}

	if(!player.get_key){
		pair<int,vector<string>> path_to_key = CalcMinDistance(turn,
		pii(player.y, player.x), pii(ky, kx), true);
		cerr << path_to_key.first << endl;
		for(auto &query : path_to_key.second){
			StepTurn(player, query);
		}
	}
	cerr << "hp = " << player.hp << endl;
	pair<int,vector<string>> path_to_goal = CalcMinDistance(turn,
	pii(player.y, player.x), pii(gy, gx), true);
	cerr << path_to_goal.first << endl;
	for(auto &query : path_to_goal.second){
		StepTurn(player, query);
	}

	cerr << "score = " << player.score << endl;
	cerr << "hp = " << player.hp << endl;
	output();

	return 0;
}
0