#define _CRT_NONSTDC_NO_WARNINGS #define _SILENCE_CXX17_ITERATOR_BASE_CLASS_DEPRECATION_WARNING #include #include #include #include #include #ifdef _MSC_VER #include #include #include #include #include #include #include #include #include /* g++ functions */ int __builtin_clz(unsigned int n) { unsigned long index; _BitScanReverse(&index, n); return 31 - index; } int __builtin_ctz(unsigned int n) { unsigned long index; _BitScanForward(&index, n); return index; } namespace std { inline int __lg(int __n) { return sizeof(int) * 8 - 1 - __builtin_clz(__n); } } /* enable __uint128_t in MSVC */ //#include //using __uint128_t = boost::multiprecision::uint128_t; #else #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #endif /** compro io **/ namespace aux { template struct tp { static void output(std::ostream& os, const T& v) { os << std::get(v) << ", "; tp::output(os, v); } }; template struct tp { static void output(std::ostream& os, const T& v) { os << std::get(v); } }; } template std::ostream& operator<<(std::ostream& os, const std::tuple& t) { os << '{'; aux::tp, 0, sizeof...(Ts) - 1>::output(os, t); return os << '}'; } // tuple out template std::basic_ostream& operator<<(std::basic_ostream& os, const Container& x); // container out (fwd decl) template std::ostream& operator<<(std::ostream& os, const std::pair& p) { return os << '{' << p.first << ", " << p.second << '}'; } // pair out template std::istream& operator>>(std::istream& is, std::pair& p) { return is >> p.first >> p.second; } // pair in std::ostream& operator<<(std::ostream& os, const std::vector::reference& v) { os << (v ? '1' : '0'); return os; } // bool (vector) out std::ostream& operator<<(std::ostream& os, const std::vector& v) { bool f = true; os << '{'; for (const auto& x : v) { os << (f ? "" : ", ") << x; f = false; } os << '}'; return os; } // vector out template std::basic_ostream& operator<<(std::basic_ostream& os, const Container& x) { bool f = true; os << '{'; for (auto& y : x) { os << (f ? "" : ", ") << y; f = false; } return os << '}'; } // container out template())), class = typename std::enable_if::value>::type> std::istream& operator>>(std::istream& is, T& a) { for (auto& x : a) is >> x; return is; } // container in template auto operator<<(std::ostream& out, const T& t) -> decltype(out << t.stringify()) { out << t.stringify(); return out; } // struct (has stringify() func) out /** io setup **/ struct IOSetup { IOSetup(bool f) { if (f) { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); } std::cout << std::fixed << std::setprecision(15); } } iosetup(true); // set false when solving interective problems /** string formatter **/ template std::string format(const std::string& f, Ts... t) { size_t l = std::snprintf(nullptr, 0, f.c_str(), t...); std::vector b(l + 1); std::snprintf(&b[0], l + 1, f.c_str(), t...); return std::string(&b[0], &b[0] + l); } /** dump **/ #define DUMPOUT std::cerr std::ostringstream DUMPBUF; #define dump(...) do{DUMPBUF<<" ";DUMPBUF<<#__VA_ARGS__<<" :[DUMP - "<<__LINE__<<":"<<__FUNCTION__<<']'< void dump_func(Head&& head, Tail&&... tail) { DUMPBUF << head; if (sizeof...(Tail) == 0) { DUMPBUF << " "; } else { DUMPBUF << ", "; } dump_func(std::move(tail)...); } /** timer **/ class Timer { double t = 0, paused = 0, tmp; public: Timer() { reset(); } static double time() { #ifdef _MSC_VER return __rdtsc() / 2.9e9; #else unsigned long long a, d; __asm__ volatile("rdtsc" : "=a"(a), "=d"(d)); return (d << 32 | a) / 2.9e9; #endif } void reset() { t = time(); } void pause() { tmp = time(); } void restart() { paused += time() - tmp; } double elapsed_ms() const { return (time() - t - paused) * 1000.0; } }; /** rand **/ struct Xorshift { static constexpr uint64_t M = INT_MAX; static constexpr double e = 1.0 / M; uint64_t x = 88172645463325252LL; Xorshift() {} Xorshift(uint64_t seed) { reseed(seed); } inline void reseed(uint64_t seed) { x = 0x498b3bc5 ^ seed; for (int i = 0; i < 20; i++) next(); } inline uint64_t next() { x = x ^ (x << 7); return x = x ^ (x >> 9); } inline int next_int() { return next() & M; } inline int next_int(int mod) { return next() % mod; } inline int next_int(int l, int r) { return l + next_int(r - l + 1); } inline double next_double() { return next_int() * e; } }; /** shuffle **/ template void shuffle_vector(std::vector& v, Xorshift& rnd) { int n = v.size(); for (int i = n - 1; i >= 1; i--) { int r = rnd.next_int(i); std::swap(v[i], v[r]); } } /** split **/ std::vector split(const std::string& str, const std::string& delim) { std::vector res; std::string buf; for (const auto& c : str) { if (delim.find(c) != std::string::npos) { if (!buf.empty()) res.push_back(buf); buf.clear(); } else buf += c; } if (!buf.empty()) res.push_back(buf); return res; } /** misc **/ template inline void Fill(A(&array)[N], const T& val) { std::fill((T*)array, (T*)(array + N), val); } // fill array template auto make_vector(T x, int arg, Args ...args) { if constexpr (sizeof...(args) == 0)return std::vector(arg, x); else return std::vector(arg, make_vector(x, args...)); } template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; } template bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } return false; } /* fast queue */ class FastQueue { int front = 0; int back = 0; int v[4096]; public: inline bool empty() { return front == back; } inline void push(int x) { v[front++] = x; } inline int pop() { return v[back++]; } inline void reset() { front = back = 0; } inline int size() { return front - back; } }; class RandomQueue { int sz = 0; int v[4096]; public: inline bool empty() const { return !sz; } inline int size() const { return sz; } inline void push(int x) { v[sz++] = x; } inline void reset() { sz = 0; } inline int pop(int i) { std::swap(v[i], v[sz - 1]); return v[--sz]; } inline int pop(Xorshift& rnd) { return pop(rnd.next_int(sz)); } }; #if 1 inline double get_temp(double stemp, double etemp, double t, double T) { return etemp + (stemp - etemp) * (T - t) / T; }; #else inline double get_temp(double stemp, double etemp, double t, double T) { return stemp * pow(etemp / stemp, t / T); }; #endif constexpr int N = 50; constexpr int M = 20; constexpr int dy[] = { 0, -1, 0, 1 }; constexpr int dx[] = { 1, 0, -1, 0 }; constexpr char d2c[] = "RULD"; int c2d[256]; namespace NInitializer { struct Initializer { Initializer() { c2d['R'] = 0; c2d['U'] = 1; c2d['L'] = 2; c2d['D'] = 3; } } initialize_instance; } struct Pos { int y, x; Pos(int y_ = 0, int x_ = 0) : y(y_), x(x_) {} inline int distance(const Pos& rhs) const { return abs(y - rhs.y) + abs(x - rhs.x); } inline int distance(int ry, int rx) const { return abs(y - ry) + abs(x - rx); } }; std::istream& operator>>(std::istream& in, Pos& p) { in >> p.y >> p.x; return in; } struct Bomb { int cost; std::vector displacements; inline size_t size() const { return displacements.size(); } }; std::istream& operator>>(std::istream& in, Bomb& b) { int L; in >> b.cost >> L; b.displacements.resize(L); in >> b.displacements; return in; } struct Input { std::array, N> grid; std::array bombs; Input(std::istream& in) { { int buf; in >> buf >> buf; } // ignore N, M in >> grid >> bombs; } }; struct State { std::array, N> grid; std::array bombs; std::array, N> shop_grid; std::vector shops; std::vector shop_bombed; int64_t cost; Pos pos; int num_total_bombs; std::vector num_bombs; std::vector> commands; int remaining; State() {} State(const Input& input) : grid(input.grid), bombs(input.bombs) { remaining = 0; { int id = 0; for (auto& v : shop_grid) { std::fill(v.begin(), v.end(), -1); } for (int y = 0; y < N; y++) { for (int x = 0; x < N; x++) { if (grid[y][x] != '.') { remaining++; } if (grid[y][x] == '@') { shop_grid[y][x] = id; shops.emplace_back(y, x); shop_bombed.push_back(false); id++; } } } } cost = 0; pos = { 0, 0 }; num_total_bombs = 0; num_bombs.resize(M); } inline bool is_inside(int y, int x) const { return 0 <= y && y < N && 0 <= x && x < N; } inline bool is_inside(const Pos& p) const { return is_inside(p.y, p.x); } void move(int dir) { pos.y += dy[dir]; pos.x += dx[dir]; assert(is_inside(pos)); cost += int64_t(num_total_bombs + 1) * (num_total_bombs + 1) * (grid[pos.y][pos.x] == '.' ? 1 : 2); commands.emplace_back(1, dir); } void move(char cdir) { move(c2d[cdir]); } void move_simple(int y, int x) { while (pos.x < x) move('R'); while (pos.x > x) move('L'); while (pos.y < y) move('D'); while (pos.y > y) move('U'); } void move_simple(const Pos& target) { move_simple(target.y, target.x); } void move_shop(int shop_id) { assert(!shop_bombed[shop_id]); move_simple(shops[shop_id]); } void purchase(int bomb_id) { assert(grid[pos.y][pos.x] == '@'); cost += bombs[bomb_id].cost; num_bombs[bomb_id]++; num_total_bombs++; commands.emplace_back(2, bomb_id); } void use(int bomb_id) { assert(num_bombs[bomb_id]); num_bombs[bomb_id]--; num_total_bombs--; for (const auto& [dy, dx] : bombs[bomb_id].displacements) { int ny = pos.y + dy, nx = pos.x + dx; if (!is_inside(ny, nx)) continue; if (grid[ny][nx] == '@') { int shop_id = shop_grid[ny][nx]; shop_bombed[shop_id] = true; } if (grid[ny][nx] != '.') { remaining--; grid[ny][nx] = '.'; } } commands.emplace_back(3, bomb_id); } void output(std::ostream& out) const { out << commands.size() << '\n'; for (const auto& [t, x] : commands) { out << t << ' '; if (t == 1) out << d2c[x]; else out << x + 1; out << '\n'; } } void solve_naive() { move_shop(0); for (int i = 0; i < 2500; i++) purchase(0); for (int y = 0; y < N; y++) { if (y & 1) { for (int x = N - 1; x >= 0; x--) { move_simple({ y, x }); use(0); if (!remaining) return; } } else { for (int x = 0; x < N; x++) { move_simple({ y, x }); use(0); if (!remaining) return; } } } } int nearest_shop(const Pos& p) const { int nearest = -1, min_dist = INT_MAX; for (int s = 0; s < (int)shops.size(); s++) { if (chmin(min_dist, p.distance(shops[s]))) { nearest = s; } } return nearest; } void solve() { // shop s で爆弾 b を購入し、位置 p で起動して shop s に戻るのに必要なコスト // 爆弾コスト C_b // manhattan_dist(s,p)=d として、d*4+d (行きと帰り 実際は'#'の数だけコスト増) auto cost_map = make_vector(0, N, N, M); for (int y = 0; y < N; y++) { for (int x = 0; x < N; x++) { int s = nearest_shop({ y, x }); int d = shops[s].distance(y, x); for (int b = 0; b < M; b++) { cost_map[y][x][b] = bombs[b].cost + d * 5; } } } auto can_bomb = make_vector(true, N, N, M); for (int b = 0; b < M; b++) { for (auto [dy, dx] : bombs[b].displacements) { for (auto [sy, sx] : shops) { int y = sy - dy, x = sx - dx; if (!is_inside(y, x)) continue; can_bomb[y][x][b] = false; } } } auto overlap = make_vector(0, N, N); std::vector> used; for (int y = 0; y < N; y++) { for (int x = 0; x < N; x++) { for (int b = 0; b < M; b++) { if (!can_bomb[y][x][b]) continue; for (auto [dy, dx] : bombs[b].displacements) { if (!is_inside(y + dy, x + dx)) continue; overlap[y + dy][x + dx]++; } used.emplace_back(y, x, b); } } } Xorshift rnd; //shuffle_vector(used, rnd); std::sort(used.begin(), used.end(), [&](const std::tuple& a, const std::tuple& b) { auto [y1, x1, b1] = a; auto [y2, x2, b2] = b; return cost_map[y1][x1][b1] > cost_map[y2][x2][b2]; }); { std::vector> nused; for (auto [y, x, b] : used) { bool ok = true; for (auto [dy, dx] : bombs[b].displacements) { int ny = y + dy, nx = x + dx; if (!is_inside(ny, nx)) continue; int lo = grid[ny][nx] != '.'; if (overlap[ny][nx] <= lo) { ok = false; break; } } if (!ok) { nused.emplace_back(y, x, b); } else { for (auto [dy, dx] : bombs[b].displacements) { if (!is_inside(y + dy, x + dx)) continue; overlap[y + dy][x + dx]--; } } } used = nused; } dump(used.size()); { // nearest neighbor std::vector> nused; int y = 0, x = 0; std::vector visited(used.size()); for (int i = 0; i < (int)used.size(); i++) { int nearest = -1, min_dist = INT_MAX; for (int j = 0; j < (int)used.size(); j++) { if (visited[j]) continue; auto [ny, nx, d] = used[j]; if (chmin(min_dist, abs(y - ny) + abs(x - nx))) { nearest = j; } } visited[nearest] = true; nused.push_back(used[nearest]); y = std::get<0>(used[nearest]); x = std::get<1>(used[nearest]); } used = nused; } for (auto [y, x, b] : used) { // 現在地 -> shop -> (y, x) int min_dist = INT_MAX, sid = -1; for (int s = 0; s < (int)shops.size(); s++) { if (chmin(min_dist, shops[s].distance(pos) + shops[s].distance(y, x) * 4)) { sid = s; } } move_simple(shops[sid]); purchase(b); move_simple(y, x); use(b); } { int cheapest = -1, min_cost = INT_MAX; for (int b = 0; b < M; b++) { if (chmin(min_cost, bombs[b].cost)) { cheapest = b; } } while (true) { // nearest shop int nearest = -1, min_dist = INT_MAX; for (int s = 0; s < (int)shops.size(); s++) { auto [y, x] = shops[s]; if (grid[y][x] != '@') continue; if (chmin(min_dist, pos.distance(y, x))) { nearest = s; } } if (nearest == -1) break; move_simple(shops[nearest]); purchase(cheapest); use(cheapest); } } dump(remaining, cost); } }; int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv) { //batch_execution(); //exit(1); Timer timer; #ifdef HAVE_OPENCV_HIGHGUI cv::utils::logging::setLogLevel(cv::utils::logging::LogLevel::LOG_LEVEL_SILENT); #endif #if 0 std::ifstream ifs("../../Hakai-Project/visualizer/in.txt"); std::istream& in = ifs; std::ofstream ofs("../../Hakai-Project/visualizer/out.txt"); std::ostream& out = ofs; #else std::istream& in = std::cin; std::ostream& out = std::cout; #endif Input input(in); State state(input); state.solve(); dump(state.cost); dump(state.commands.size()); state.output(out); return 0; }