結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー hogeover30
提出日時 2015-10-31 04:10:56
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,018 bytes
コンパイル時間 620 ms
コンパイル使用メモリ 70,100 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-13 06:14:49
合計ジャッジ時間 1,438 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
#include <array>
#include <string>
#include <algorithm>
using namespace std;

const int n=16;
int main()
{
    array<int, n> a, goal, vis;
    for(int i=0;i<n;++i) {
        cin>>a[i];
        goal[i]=(i+1)%n;
    }
    vis={};

    queue<pair<array<int, n>, array<int, n>>> q;
    q.push(make_pair(a, vis));
    string res="No";
    while(!q.empty()) {
        auto p=q.front();
        auto b=p.first;
        auto v=p.second;
        q.pop();
        if (b==goal) {
            res="Yes";
            break;
        }
        int da[]={-1, 1, -4, 4};
        for(int i=0;i<n;++i) {
            if (b[i]==0) continue;
            for(auto d: da) {
                if (0<=i+d and i+d<n and b[i+d]==0 and !v[b[i]-1]) {
                    v[b[i]-1]=1;
                    swap(b[i], b[i+d]);
                    q.push(make_pair(b, v));
                    swap(b[i], b[i+d]);
                    v[b[i]-1]=0;
                }
            }
        }
    }
    cout<<res<<endl;
}
0