結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー kk
提出日時 2020-08-08 23:46:17
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,225 bytes
コンパイル時間 2,326 ms
コンパイル使用メモリ 205,848 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-10 07:27:58
合計ジャッジ時間 3,199 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); i++)

int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

bool check(vector<vector<int> > &s, vector<vector<int> > &t, vector<vector<bool> > &moved) {
  if (s == t)
    return true;

  REP (y, 4) REP (x, 4) {
    if (s[y][x] == 0) {
      REP (k, 4) {
        int y2 = y + dy[k];
        int x2 = x + dx[k];
        if (y2 < 0 || y2 >= 4 || x2 < 0 || x2 >= 4) continue;
        if (moved[y2][x2]) continue;

        swap(s[y][x], s[y2][x2]);
        moved[y][x] = true;
        if (check(s, t, moved))
          return true;
        moved[y][x] = false;
        swap(s[y][x], s[y2][x2]);        
      }
    }
  }
  
  return false;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  vector<vector<int> > s {
                          {1, 2, 3, 4},
                          {5, 6, 7, 8},
                          {9, 10, 11, 12},
                          {13, 14, 15, 0}};

  vector<vector<int> > t(4, vector<int>(4));
  REP (i, 4) REP (j, 4) cin >> t[i][j];

  vector<vector<bool> > moved(4, vector<bool>(4));
  
  if (check(s, t, moved))
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  
  return 0;
}
0