結果

問題 No.1434 Make Maze
ユーザー itohdakitohdak
提出日時 2021-03-19 23:37:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 2,855 bytes
コンパイル時間 2,340 ms
コンパイル使用メモリ 205,584 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-16 09:33:21
合計ジャッジ時間 7,920 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 7 ms
4,384 KB
testcase_03 AC 6 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 3 ms
4,384 KB
testcase_16 AC 2 ms
4,384 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,384 KB
testcase_19 AC 4 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 3 ms
4,384 KB
testcase_25 AC 5 ms
4,384 KB
testcase_26 AC 2 ms
4,384 KB
testcase_27 AC 3 ms
4,384 KB
testcase_28 AC 2 ms
4,384 KB
testcase_29 AC 2 ms
4,388 KB
testcase_30 AC 2 ms
4,384 KB
testcase_31 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,(n)-1,0)
#define all(v) v.begin(), v.end()
#define endk '\n'
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;
const ll mod2 = 998244353;
const ld eps = 1e-10;
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b){if(a>b) a=b;}
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b){if(a<b) a=b;}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w, x; cin >> h >> w >> x;
  if((x-(h+w-2))%4) {
    cout << -1 << endk;
    return 0;
  }
  vector<string> 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, h) ans[i][0] = '.';
  rep(j, w) ans[h-1][j] = '.';
  auto add_up = [&](pair<int, int> right_down, int upper_limit, int rem) {
    auto [x, y] = right_down;
    while(rem && x-2 >= upper_limit) {
      ans[x-1][y] = ans[x-2][y] = ans[x-1][y-2] = ans[x-2][y-2] = ans[x-2][y-1] = '.';
      ans[x][y-1] = '#';
      rem--;
      x -= 2;
    }
    return rem;
  };
  auto add_right = [&](pair<int, int> left_up, int right_limit, int rem) {
    auto [x, y] = left_up;
    while(rem && y+2 < right_limit) {
      ans[x][y+1] = ans[x][y+2] = ans[x+2][y+1] = ans[x+2][y+2] = ans[x+1][y+2] = '.';
      ans[x+1][y] = '#';
      rem--;
      y += 2;
    }
    return rem;
  };
  auto answer = [&]() {
    int dx[] = {-1, 0, 1, 0};
    int dy[] = {0, 1, 0, -1};
    for(int i=0; i<h; i+=2) {
      for(int j=0; j<w; j+=2) {
        bool ok = false;
        rep(k, 4) {
          int nx = i+dx[k];
          int ny = j+dy[k];
          if(nx >= 0 && nx < h && ny >= 0 && ny < w && ans[nx][ny] == '.') ok = true;
        }
        if(!ok) {
          int tmpj = j-1;
          while(tmpj >= 0 && ans[i][tmpj] == '#') {
            ans[i][tmpj--] = '.';
          }
        }
      }
    }
    rep(i, h) cout << ans[i] << endk;
  };
  int rem = (x-(h+w-2))/4;
  int hh = (h-1)/2%2, ww = (w-1)/2%2;
  if(hh && ww) {
    for(int j=w-1; j-2>=2; j-=4) {
      rem = add_up({h-1, j}, 0, rem);
      if(rem == 0) break;
    }
    for(int i=0; i+2<h-2; i+=4) {
      rem = add_right({i, 0}, 3, rem);
      if(rem == 0) break;
    }
  } else if(hh) {
    for(int j=w-1; j-2>=2; j-=4) {
      rem = add_up({h-1, j}, 0, rem);
      if(rem == 0) break;
    }
  } else if(ww) {
    for(int i=0; i+2<h-2; i+=4) {
      rem = add_right({i, 0}, w, rem);
      if(rem == 0) break;
    }
  } else {
    for(int j=w-1; j-2>=2; j-=4) {
      rem = add_up({h-1, j}, 0, rem);
      if(rem == 0) break;
    }
  }
  if(rem) {
    cout << -1 << endk;
  } else {
    answer();
  }
  return 0;
}
0