結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー firiexpfiriexp
提出日時 2019-03-27 18:07:06
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,463 bytes
コンパイル時間 955 ms
コンパイル使用メモリ 89,156 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-09 21:22:46
合計ジャッジ時間 2,138 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <limits>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>

static const int MOD = 1000000007;
using ll = int64_t;
using u32 = uint32_t;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;



int main() {
    int v[4][4], swapped[4][4];
    int y = 0, x = 0;
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            cin >> v[i][j];
            swapped[i][j] = 0;
            if(v[i][j] == 0){
                y = i, x = j;
            }
        }
    }

    auto f = [&](int x){ return 0 <= x && x <= 3; };
    int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
    while(true){
        int y2 = y, x2 = x;
        for (int i = 0; i < 4; ++i) {
            if(f(y+dy[i]) && f(x+dx[i])){
                if(v[y+dy[i]][x+dx[i]] == 4*y+x+1 && !swapped[y+dy[i]][x+dx[i]]) {
                    y2 += dy[i], x2 += dx[i];
                    swapped[y2][x2] = 1;
                    break;
                }
            }
        }
        if(y2 == y && x2 == x) break;
        swap(v[y][x], v[y2][x2]);
        y = y2; x = x2;
    }
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            if((i != 3 || j != 3) && v[i][j] != 4*i+j+1){
                puts("No");
                return 0;
            }
        }
    }
    puts("Yes");
    return 0;
}
0