結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー pekempeypekempey
提出日時 2015-06-19 22:39:48
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,268 bytes
コンパイル時間 1,109 ms
コンパイル使用メモリ 144,704 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-21 09:59:56
合計ジャッジ時間 2,151 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:58:12: warning: ‘sx’ may be used uninitialized in this function [-Wmaybe-uninitialized]
     if (dfs(sy, sx)) {
         ~~~^~~~~~~~
main.cpp:58:12: warning: ‘sy’ may be used uninitialized in this function [-Wmaybe-uninitialized]

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (a) - 1; i >= (b); i--)
#define all(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
const ll inf = 1e9;
const ll mod = 1e9 + 7;

bool used[16];
int G[4][4];

int dy[4] = {1, 0, -1, 0};
int dx[4] = {0, 1, 0, -1};

bool dfs(int y, int x) {
    bool ok = true;

    rep (k, 15) {
        int i = k / 4;
        int j = k % 4;

        if (G[i][j] != k + 1) ok = false;
    }

    if (ok) return true;

    bool res = false;

    rep (k, 4) {
        int ny = y + dy[k];
        int nx = x + dx[k];

        if (ny < 0 || ny >= 4 || nx < 0 || nx >= 4) continue;
        if (used[G[ny][nx]]) continue;

        used[G[ny][nx]] = true;
        swap(G[ny][nx], G[y][x]);

        res |= dfs(ny, nx);

        swap(G[ny][nx], G[y][x]);
        used[G[ny][nx]] = false;
    }

    return res;
}

int main() {
    rep (i, 4) rep (j, 4) cin >> G[i][j];    

    int sy, sx;

    rep (i, 4) rep (j, 4) if (G[i][j] == 0) sy = i, sx = j;

    if (dfs(sy, sx)) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
}
0