結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー karinohito
提出日時 2024-09-19 12:21:56
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,126 bytes
コンパイル時間 1,969 ms
コンパイル使用メモリ 195,428 KB
最終ジャッジ日時 2025-02-24 09:31:50
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {

    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int A[4][4];
    bool D[4][4];
    
    for(int i=0;i<4;i++)for(int j=0;j<4;j++){
        cin>>A[i][j];
        D[i][j]=0;
    }
    vector<int> dx={0,1,0,-1},dy={1,0,-1,0};
    for(int k=0;k<16;k++){
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                if(A[i][j]==0){
                    for(int d=0;d<4;d++){
                        int y=dy[d]+i;
                        int x=dx[d]+j;
                        if(min(y,x)<0||max(y,x)>=4)continue;
                        if(D[y][x])continue;
                        if(A[y][x]==i*4+j+1){
                            swap(A[y][x],A[i][j]);
                            D[i][j]=1;
                        }
                    }
                }
            }
        }
    }
    for(int i=0;i<4;i++)for(int j=0;j<4;j++){
            if(A[i][j]==0)continue;
            if(A[i][j]!=i*4+j+1){
                cout<<"No"<<endl;
                return 0;
            }
        }
        cout<<"Yes"<<endl;
}
0