結果

問題 No.1434 Make Maze
ユーザー 👑 emthrmemthrm
提出日時 2021-03-19 23:02:55
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 49 ms / 2,000 ms
コード長 3,879 bytes
コンパイル時間 2,500 ms
コンパイル使用メモリ 208,696 KB
最終ジャッジ日時 2025-01-19 19:18:20
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

int main() {
  int h, w, x; cin >> h >> w >> x;
  if (x & 1) {
    cout << "-1\n";
    return 0;
  }
  x /= 2;
  int r = (h + 1) / 2, c = (w + 1) / 2;
  if (x < r - 1 + c - 1) {
    cout << "-1\n";
    return 0;
  }
  vector dir(r, vector(c, vector(4, false)));
  auto solve = [&]() -> void {
    for (int i = 0; i < r; i += 2) {
      REP(j, c - 1) dir[i][j][3] = true;
      dir[i][c - 1][0] = true;
    }
    for (int i = 1; i < r; i += 2) {
      dir[i][0][0] = true;
      FOR(j, 1, c) dir[i][j][1] = true;
    }
    fill(ALL(dir[r - 1][c - 1]), false);
    int cur = r * c - 1;
    for (int i = r - 2; i >= 0; i -= 2) {
      REP(j, c - 1) {
        if (cur > x) {
          dir[i][j][0] = false;
          dir[i + 1][j][3] = false;
          dir[i][j + 1][0] = true;
          dir[i + 1][j + 1][1] = true;
          cur -= 2;
        }
      }
    }
  };
  if (r % 2 == 1) {
    if (x > r * c - 1 || (r * c - 1 - x) % 2 == 1) {
      cout << "-1\n";
      return 0;
    }
    solve();
  } else if (c % 2 == 1) {
    if (x > r * c - 1 || (r * c - 1 - x) % 2 == 1) {
      cout << "-1\n";
      return 0;
    }
    swap(r, c);
    dir = vector(r, vector(c, vector(4, false)));
    solve();
    swap(r, c);
    vector nx(r, vector(c, vector(4, false)));
    REP(i, r) REP(j, c) REP(k, 4) nx[i][j][3 - k] = dir[j][i][k];
    dir.swap(nx);
  } else {
    if (x > r * c - 2 || (r * c - 2 - x) % 2 == 1) {
      cout << "-1\n";
      return 0;
    }
    for (int i = 0; i < r - 2; i += 2) {
      REP(j, c - 1) dir[i][j][3] = true;
      dir[i][c - 1][0] = true;
    }
    for (int i = 1; i < r - 2; i += 2) {
      dir[i][0][0] = true;
      FOR(j, 1, c) dir[i][j][1] = true;
    }
    dir[r - 2][0][3] = true;
    for (int j = 1; j < c; j += 2) {
      dir[r - 2][j][0] = true;
      dir[r - 1][j][3] = true;
    }
    for (int j = 2; j < c; j += 2) {
      dir[r - 2][j][3] = true;
      dir[r - 1][j][2] = true;
    }
    fill(ALL(dir[r - 1][c - 1]), false);
    dir[r - 2][0][0] = true;
    int cur = r * c - 2;
    for (int j = 2; j < c; j += 2) {
      if (cur > x) {
        dir[r - 2][j - 1][3] = true;
        dir[r - 1][j - 1][3] = false;
        dir[r - 2][j][3] = true;
        dir[r - 1][j][1] = false;
        cur -= 2;
      }
    }
    for (int i = r - 3; i >= 0; i -= 2) {
      REP(j, c - 1) {
        if (cur > x) {
          dir[i + 1][j][3] = false;
          dir[i][j + 1][0] = true;
          cur -= 2;
        }
      }
    }
  }
  // REP(k, 4) {
  //   REP(i, r) REP(j, c) cout << dir[i][j][k] << " \n"[j + 1 == c];
  //   cout << '\n';
  // }
  vector ans(h, string(w, '#'));
  for (int i = 0; i < h; i += 2) for (int j = 0; j < w; j += 2) ans[i][j] = '.';
  REP(i, r) REP(j, c) REP(k, 4) {
    if (dir[i][j][k]) ans[i * 2 + dy[k]][j * 2 + dx[k]] = '.';
  }
  REP(i, h) REP(j, w) {
    if (i % 2 == 0 && j % 2 == 0) assert(ans[i][j] == '.');
    if (i % 2 == 1 && j % 2 == 1) assert(ans[i][j] == '#');
  }
  REP(i, h) cout << ans[i] << '\n';
  return 0;
}
0