#include 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_001.txt", "r", stdin); } void file_output(){ FILE *out = freopen("out/testcase_001.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, player, }; // セルの情報 struct Cell{ cell_status status; Enemy* e_pointer = nullptr; Cell(cell_status st = cell_status::empty) : status(st) {} }; int N, D, M; vector enemy; vector S; vector> grid; vector> enemy_number; // 範囲外かどうか 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) : y(y), x(x){ hp = max_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> dist(N, vector(N, -1)); dist[sy][sx] = 0; queue> q; q.emplace(sy, sx); while(!q.empty()){ pair 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::block || 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> dist(N, vector(N, -1)); int sy = player.y, sx = player.x; dist[sy][sx] = 0; queue> q; q.emplace(sy, sx); int gy = -1, gx = -1; while(!q.empty()){ pair 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::block || 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; } // プレイヤーへのダメージ処理 void CalcDamage(vector> &grid, Player &player, int t){ int res = 1; for(int k = 0; k < 4; k++){ int y = player.y + dy[k], x = player.x + 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]; } } player.damage(res); } int action(Player &player, string &query){ if(query[0] == 'S') 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]; grid[sy][sx].status = cell_status::empty; grid[ny][nx].status = cell_status::player; } else if(c == 'B'){ return -1; } else if(c == 'F'){ return -1; } return -1; } void input(){ cin >> N >> D; S.resize(N); for(int i = 0; i < N; i++) cin >> S[i]; enemy_number.resize(N, vector(N)); 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(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 if(S[i][j] == 'S'){ grid[i][j].status = cell_status::player; } 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(grid[i][j].status == cell_status::player){ sy = i, sx = j; } else if(grid[i][j].status == cell_status::key){ ky = i, kx = j; } else if(grid[i][j].status == cell_status::goal){ gy = i, gx = j; } } } Player player(sy, sx); vector ans; int turn = 1; while(player.hp > 250){ string path = get_jewel(player); if(path.empty()) break; string query = "M "; query += path[0]; action(player, query); CalcDamage(grid, player, turn); ans.emplace_back(query); turn++; } string res; res += find_path(player.y, player.x, ky, kx); res += find_path(ky, kx, gy, gx); for(auto s : res){ string query = "M "; query += s; ans.emplace_back(query); } cerr << player.score << endl; int Q = ans.size(); cout << Q << endl; for(int i = 0; i < Q; i++){ cout << ans[i] << endl; } return 0; }