結果
問題 | No.2412 YOU Grow Bigger! |
ユーザー |
|
提出日時 | 2023-07-19 22:38:29 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,185 ms / 6,000 ms |
コード長 | 3,828 bytes |
コンパイル時間 | 2,760 ms |
コンパイル使用メモリ | 223,040 KB |
最終ジャッジ日時 | 2025-02-15 15:48:35 |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 29 |
ソースコード
#ifndef hari64#include <bits/stdc++.h>#pragma GCC target("avx2")#pragma GCC optimize("O3")#pragma GCC optimize("unroll-loops")#define debug(...)#else#include "util/viewer.hpp"#define debug(...) viewer::_debug(__LINE__, #__VA_ARGS__, __VA_ARGS__)#endif// clang-format offusing namespace std;using ll = long long; using ld = long double;using pii = pair<int, int>; using pll = pair<ll, ll>;template <typename T> using vc = vector<T>;template <typename T> using vvc = vector<vc<T>>;template <typename T> using vvvc = vector<vvc<T>>;using vi = vc<int>; using vll = vc<ll>; using vpii = vc<pii>; using vpll = vc<pll>;using vvi = vc<vi>; using vvll = vc<vll>; using vvpi = vc<vpii>; using vvpll = vc<vpll>;#define ALL(x) begin(x), end(x)#define RALL(x) (x).rbegin(), (x).rend()constexpr int INF = 1001001001; constexpr long long INFll = 1001001001001001001;template <class T> bool chmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }template <class T> bool chmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }// clang-format onint H, W;vector<string> Ss;bool canReach() {vector<vector<bool>> grid(H - 2, vector<bool>(W - 2, INF));for (int h = 1; h < H - 1; h++) {for (int w = 1; w < W - 1; w++) {grid[h - 1][w - 1] = true;for (int dh = -1; dh <= 1; dh++) {for (int dw = -1; dw <= 1; dw++) {if (Ss[h + dh][w + dw] == '#') {grid[h - 1][w - 1] = false;}}}}}assert(grid[0][0] == true && grid[H - 3][W - 3] == true);// 0,0からH-3,W-3までの経路があるかvvc<vpii> adj(H - 2, vc<vpii>(W - 2));for (int h = 0; h < H - 2; h++) {for (int w = 0; w < W - 2; w++) {if (!grid[h][w]) continue;for (int dh = -1; dh <= 1; dh++) {for (int dw = -1; dw <= 1; dw++) {if (dh == 0 && dw == 0) continue;if (h + dh < 0 || h + dh >= H - 2) continue;if (w + dw < 0 || w + dw >= W - 2) continue;if (grid[h + dh][w + dw]) {adj[h][w].push_back({h + dh, w + dw});}}}}}queue<pii> q;q.push({0, 0});vector<vector<bool>> visited(H - 2, vector<bool>(W - 2));visited[0][0] = true;while (!q.empty()) {auto [h, w] = q.front();q.pop();for (auto [nh, nw] : adj[h][w]) {if (visited[nh][nw]) continue;visited[nh][nw] = true;q.push({nh, nw});}}return visited[H - 3][W - 3];}int main() {cin.tie(0);ios::sync_with_stdio(false);cin >> H >> W;assert(6 <= H && 6 <= W);assert(H * W <= 2400);Ss.resize(H);for (auto& S : Ss) {cin >> S;assert(int(S.size()) == W);for (auto& c : S) {assert(c == '.' || c == '#');}}for (int i = 0; i < 3; i++) {for (int j = 0; j < 3; j++) {assert(Ss[i][j] == '.');}}for (int i = H - 3; i < H; i++) {for (int j = W - 3; j < W; j++) {assert(Ss[i][j] == '.');}}int ans = 2;if (!canReach()) {ans = 0;} else {for (int h = 0; h < H && ans == 2; h++) {for (int w = 0; w < W && ans == 2; w++) {if (h < 3 && w < 3) continue;if (h >= H - 3 && w >= W - 3) continue;if (Ss[h][w] == '#') continue;Ss[h][w] = '#';if (!canReach()) {ans = 1;}Ss[h][w] = '.';}}}cout << ans << endl;return 0;}