結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー yosupotyosupot
提出日時 2016-07-16 13:50:00
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,644 bytes
コンパイル時間 993 ms
コンパイル使用メモリ 98,968 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-05 16:38:49
合計ジャッジ時間 1,917 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cassert>
#include <cstring>
#include <vector>
#include <valarray>
#include <array>
#include <queue>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <cmath>
#include <complex>
#include <random>

using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr int TEN(int n) {return (n==0)?1:10*TEN(n-1);}

int main() {
    int a[4][4];
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            cin >> a[i][j]; a[i][j]--;
            if (a[i][j] == -1) a[i][j] = 15;
        }
    }
    using P = array<int, 2>;
    constexpr P d4[4] = {
        {1, 0},
        {0, 1},
        {-1, 0},
        {0, -1},
    };
    while (true) {
        P p;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (a[i][j] == 15) {
                    p = P{i, j};
                }
            }
        }
        if (p == P{3, 3}) break;
        bool f = false;
        for (int i = 0; i < 4; i++) {
            P np = P{p[0]+d4[i][0], p[1]+d4[i][1]};
            if (!(0 <= min(np[0], np[1]) and max(np[0], np[1]) <= 3)) continue;
            if (a[np[0]][np[1]] == p[0]*4+p[1]) {
                f = true;
                swap(a[np[0]][np[1]], a[p[0]][p[1]]);
            }
        }
        if (!f) break;
    }
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            if (a[i][j] != i*4+j) {
                cout << "No" << endl;
                return 0;
            }
        }
    }
    cout << "Yes" << endl;
    return 0;
}
0