結果
| 問題 | No.228 ゆきこちゃんの 15 パズル | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-08-08 23:46:17 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 5,000 ms | 
| コード長 | 1,225 bytes | 
| コンパイル時間 | 2,402 ms | 
| コンパイル使用メモリ | 203,648 KB | 
| 最終ジャッジ日時 | 2025-01-12 19:04:07 | 
| ジャッジサーバーID (参考情報) | judge1 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 17 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0; i<(int)(n); i++)
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
bool check(vector<vector<int> > &s, vector<vector<int> > &t, vector<vector<bool> > &moved) {
  if (s == t)
    return true;
  REP (y, 4) REP (x, 4) {
    if (s[y][x] == 0) {
      REP (k, 4) {
        int y2 = y + dy[k];
        int x2 = x + dx[k];
        if (y2 < 0 || y2 >= 4 || x2 < 0 || x2 >= 4) continue;
        if (moved[y2][x2]) continue;
        swap(s[y][x], s[y2][x2]);
        moved[y][x] = true;
        if (check(s, t, moved))
          return true;
        moved[y][x] = false;
        swap(s[y][x], s[y2][x2]);        
      }
    }
  }
  
  return false;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  vector<vector<int> > s {
                          {1, 2, 3, 4},
                          {5, 6, 7, 8},
                          {9, 10, 11, 12},
                          {13, 14, 15, 0}};
  vector<vector<int> > t(4, vector<int>(4));
  REP (i, 4) REP (j, 4) cin >> t[i][j];
  vector<vector<bool> > moved(4, vector<bool>(4));
  
  if (check(s, t, moved))
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  
  return 0;
}
            
            
            
        