結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー なおなお
提出日時 2015-12-16 03:52:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,136 bytes
コンパイル時間 1,149 ms
コンパイル使用メモリ 144,812 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-14 10:34:32
合計ジャッジ時間 2,623 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
#define REP(i, n) for(int(i)=0;(i)<(n);++(i))
#define in(T,V) T V;cin>>V;
const int MOD = int(1e9+7);

const int dir[][2] = {{1,0},{0,1},{-1,0},{0,-1},{1,-1},{1,1},{-1,1},{-1,-1}};
int m[4][4];

int num(int x, int y){ return x==3&&y==3 ? 0 : y*4+x+1; }

bool check(){
    REP(y,4) REP(x,4) if(m[y][x] != num(x,y)) return false;
    return true;
}

int main(){
    int zx = -1, zy = -1;
    REP(y,4) REP(x,4){
        cin >> m[y][x];
        if(m[y][x] == 0) zx = x, zy = y;
    }
    bool res = true;
    while(1){
        if(check()) break;
        bool moveflag = false;
        REP(d,4){
            int mx = zx + dir[d][0];
            int my = zy + dir[d][1];
            if(mx < 0 || mx >= 4 || my < 0 || my >= 4) continue;
            if(m[my][mx] == num(zx,zy)){
                swap(m[my][mx], m[zy][zx]);
                zx = mx, zy = my;
                moveflag = true;
                break;
            }
        }
        if(!moveflag){
            res = false; break;
        }
    }
    cout << (res ? "Yes" : "No") << endl;
}
0