結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー hogeover30hogeover30
提出日時 2015-10-31 04:10:56
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,018 bytes
コンパイル時間 536 ms
コンパイル使用メモリ 70,828 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-11 07:04:01
合計ジャッジ時間 1,868 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

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