結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー firiexp
提出日時 2019-03-27 18:07:06
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,463 bytes
コンパイル時間 955 ms
コンパイル使用メモリ 89,844 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-15 19:16:58
合計ジャッジ時間 1,870 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

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