結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー MitI_7MitI_7
提出日時 2015-11-03 08:55:54
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 2,423 bytes
コンパイル時間 828 ms
コンパイル使用メモリ 98,260 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-11 13:09:08
合計ジャッジ時間 1,703 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <numeric>
#include <functional>
#include <set>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <fstream>
#include <bitset>
#include <time.h>

#define ll long long
#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0L)
#define EPS 1e-14

using namespace std;

vector<vector<int>> target = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, {13, 14, 15, 0} };


// 右, 下, 左, 上(, 右上, 右下, 左下, 左上)
vector<int> dy = { 0, -1, 0, 1 }; //, 1, -1, -1, 1 };
vector<int> dx = { 1, 0, -1, 0 }; // , 1, 1, -1, -1 };

bool dfs(vector<vector<int>> v, vector<bool> used) {
    if (v == target) {
        return true;
    }
    if (count(ALL(used), false) == 0) {
        return false;
    }

    FOR(i, 0, v.size()) {
        FOR(j, 0, v[0].size()) {
            if (v[i][j] != 0) {
                continue;
            }

            int now_y = i, now_x = j;
            for (int i = 0; i < dy.size(); i++) {
                int next_y = now_y + dy[i];
                int next_x = now_x + dx[i];
                
                if (0 <= next_y && next_y < v.size() && 0 <= next_x && next_x < v[0].size()) {
                    
                    int n = v[next_y][next_x];
                    if (used[n]) {
                        continue;
                    }
                    used[n] = true;
                    swap(v[next_y][next_x], v[now_y][now_x]);
                    
                    if (dfs(v, used)) {
                        return true;
                    }
                    used[n] = false;
                    swap(v[next_y][next_x], v[now_y][now_x]);
                }
            }
        }
    }
    return false;

}


int main(int argc, char *argv[]) {
    vector<vector<int>> v;
    int a, b, c, d;
    FOR(i, 0, 4) {
        cin >> a >> b >> c >> d;
        v.push_back({a, b, c, d});
    }

    vector<bool> used(16, false);
    if (dfs(v, used)) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }

}
0