結果
| 問題 | No.228 ゆきこちゃんの 15 パズル | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2015-06-19 22:39:48 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 5,000 ms | 
| コード長 | 1,268 bytes | 
| コンパイル時間 | 1,323 ms | 
| コンパイル使用メモリ | 159,412 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-07-07 04:07:19 | 
| 合計ジャッジ時間 | 1,599 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 17 | 
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:58:12: warning: ‘sx’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   58 |     if (dfs(sy, sx)) {
      |         ~~~^~~~~~~~
main.cpp:58:12: warning: ‘sy’ may be used uninitialized in this function [-Wmaybe-uninitialized]
            
            ソースコード
#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;
    }
}
            
            
            
        