結果

問題 No.1427 Simplified Tetris
ユーザー SSRSSSRS
提出日時 2021-03-13 00:34:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 85 ms / 2,000 ms
コード長 4,336 bytes
コンパイル時間 2,230 ms
コンパイル使用メモリ 181,996 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-22 15:43:44
合計ジャッジ時間 4,328 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 48 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 10 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 14 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 10 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 49 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 4 ms
5,376 KB
testcase_25 AC 47 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 4 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 15 ms
5,376 KB
testcase_32 AC 39 ms
5,376 KB
testcase_33 AC 24 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
testcase_36 AC 79 ms
5,376 KB
testcase_37 AC 47 ms
5,376 KB
testcase_38 AC 67 ms
5,376 KB
testcase_39 AC 15 ms
5,376 KB
testcase_40 AC 85 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  int H, W;
  cin >> H >> W;
  vector<string> s(H);
  for (int i = H - 1; i >= 0; i--){
    cin >> s[i];
  }
  int mx = H;
  bool ok = true;
  for (int i = H - 1; i >= 0; i--){
    bool ok1 = false, ok2 = false;
    for (int j = 0; j < W; j++){
      if (s[i][j] == '.'){
        ok1 = true;
      }
      if (s[i][j] == '#'){
        ok2 = true;
      }
    }
    if (!ok1){
      ok = false;
    }
    if (ok2 && mx == H){
      mx = i;
    }
    if (!ok2 && mx != H){
      ok = false;
    }
  }
  if (!ok){
    cout << "No" << endl;
  } else if (mx == H){
    cout << "Yes" << endl;
    for (int i = 0; i < H; i++){
      cout << string(W, '.') << endl;
    }
  } else {
    int cnt = mx + 1;
    bool ok2 = false;
    vector<string> ans;
    for (int i = 0; i < (1 << H); i++){
      if (__builtin_popcount(i) == cnt){
        vector<int> p;
        for (int j = 0; j < H; j++){
          if ((i >> j & 1) == 1){
            p.push_back(j);
          }
        }
        vector<string> s2(H, string(W, '.'));
        for (int j = 0; j < p[cnt - 1]; j++){
          s2[j] = string(W, '#');
        }
        for (int j = 0; j < cnt; j++){
          s2[p[j]] = s[j];
        }
        for (int j = p[cnt - 1] + 1; j <= H; j++){
          vector<vector<bool>> dp(H * W + 1, vector<bool>(1 << W, false));
          dp[0][(1 << W) - 1] = true;
          for (int k = 0; k < H * W; k++){
            for (int l = 0; l < (1 << W); l++){
              if (dp[k][l]){
                int x = k / W, y = k % W;
                if (s2[x][y] == '.'){
                  if ((l >> (W - 1) & 1) == 1){
                    int l2 = l - (1 << (W - 1));
                    dp[k + 1][l2 * 2 + 1] = true;
                  }
                } else {
                  if ((l >> (W - 1) & 1) == 1){
                    int l2 = l - (1 << (W - 1));
                    dp[k + 1][l2 * 2] = true;
                    if (y > 0){
                      if ((l & 1) == 0){
                        dp[k + 1][l2 * 2 + 3] = true;
                      }
                    }
                  } else {
                    dp[k + 1][l * 2 + 1] = true;
                  }
                }
              }
            }
          }
          if (dp[H * W][(1 << W) - 1]){
            ok2 = true;
            vector<char> c;
            for (int k = 0; k < 26; k++){
              c.push_back('a' + k);
            }
            for (int k = 0; k < 26; k++){
              c.push_back('A' + k);
            }
            int id = 0;
            int S = (1 << W) - 1;
            for (int k = H * W - 1; k >= 0; k--){
              int x = k / W, y = k % W;
              if ((S & 1) == 0){
                if (dp[k][S / 2]){
                  S = S / 2;
                } else {
                  S = S / 2 + (1 << (W - 1));
                }
              } else if (s2[x][y] == '.'){
                if (dp[k][(S - 1) / 2]){
                  S = (S - 1) / 2;
                } else {
                  S = (S - 1) / 2 + (1 << (W - 1));
                }
              } else {
                bool h = false;
                if (y > 0){
                  if (s2[x][y - 1] == '#'){
                    if ((S >> 1 & 1) == 1){
                      if (dp[k][(S - 3) / 2]){
                        S = (S - 3) / 2;
                        h = true;
                      } else if (dp[k][(S - 3) / 2 + (1 << (W - 1))]){
                        S = (S - 3) / 2 + (1 << (W - 1));
                        h = true;
                      }
                    }
                  }
                }
                if (h){
                  s2[x][y] = c[id];
                  s2[x][y - 1] = c[id];
                  id++;
                } else {
                  s2[x][y] = c[id];
                  s2[x - 1][y] = c[id];
                  id++;
                  S = (S - 1) / 2;
                }
              }
            }
            ans = s2;
            break;
          }
          if (ok2){
            break;
          }
          if (j < H){
            s2[j] = string(W, '#');
          }
        }
      }
    }
    if (!ok2){
      cout << "No" << endl;
    } else {
      cout << "Yes" << endl;
      for (int i = H - 1; i >= 0; i--){
        cout << ans[i] << endl;
      }
    }
  }
}
0