結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー hadrorihadrori
提出日時 2015-07-28 14:55:57
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 882 bytes
コンパイル時間 1,305 ms
コンパイル使用メモリ 143,904 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-23 04:40:58
合計ジャッジ時間 2,063 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repi(i,0,n)

const int di[] = {0,1,0,-1}, dj[] = {1,0,-1,0};

int f[4][4], g[4][4];
inline bool in(int i, int j) { return 0 <= i and i < 4 and 0 <= j and j < 4; }

int solve(int i, int j, int used) {
    int ok = 1;
    rep(i,4) rep(j,4) if(f[i][j] != g[i][j]) ok = 0;
    if(ok) return 1;
    rep(d,4) {
        const int ni = i+di[d], nj = j+dj[d];
        if(in(ni,nj) and g[ni][nj] == f[i][j]) {
            swap(g[i][j], g[ni][nj]);
            if(solve(ni,nj,1<<f[i][j]|used)) return 1;
            swap(g[i][j], g[ni][nj]);
        }
    }
    return 0;
}

void input() {
    rep(i,4) rep(j,4) {
        scanf("%d", f[i]+j);
        g[i][j] = i*4+j+1;
    }
    g[3][3] = 0;
}

int main() {
    input();
    puts(solve(3,3,0)? "Yes": "No");
    return 0;
}
0