結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー not_522not_522
提出日時 2016-11-23 11:50:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,524 bytes
コンパイル時間 1,711 ms
コンパイル使用メモリ 170,496 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-08-18 06:47:58
合計ジャッジ時間 2,585 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

struct Initializer {
  Initializer() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(15);
  }
} initializer;

template<typename T> long long inversion_number(const vector<T>& v) {
  function<long long(vector<T>&)> f = [&](vector<T>& v){
    if (v.size() <= 1u) return 0;
    int n = v.size();
    vector<int> a(v.begin(), v.begin() + n / 2);
    vector<int> b(v.begin() + n / 2, v.end());
    int count = f(a) + f(b);
    for (int i = 0, j = 0, k = 0; i < n; ++i) {
      if (k == (int)b.size()) {
        v[i] = a[j++];
      } else if (j == (int)a.size()) {
        v[i] = b[k++];
      } else if (a[j] <= b[k]) {
        v[i] = a[j++];
      } else {
        v[i] = b[k++];
        count += n / 2 - j;
      }
    }
    return count;
  };
  auto vv = v;
  return f(vv);
}

int main() {
  int a[4][4], z = -1;
  for (auto& i : a) {
    for (int& j : i) cin >> j;
  }
  vector<int> v;
  for (int i = 0; i < 4; ++i) {
    for (int j = 0; j < 4; ++j) {
      int k = a[i][i % 2 ? j : 3 - j];
      v.emplace_back(k ?: 16);
      if (k == 0) z = v.size();
    }
  }
  if ((inversion_number(v) + z) % 2) {
    cout << "No" << endl;
    return 0;
  }
  for (int i = 0; i < 4; ++i) {
    for (int j = 0; j < 4; ++j) {
      if (a[i][j] == 0) continue;
      int d = a[i][j] - (i * 4 + j + 1);
      if (d != -4 && d != -1 && d != 0 && d != 1 && d != 4) {
        cout << "No" << endl;
        return 0;
      }
    }
  }
  cout << "Yes" << endl;
}
0