結果

問題 No.228 ゆきこちゃんの 15 パズル
コンテスト
ユーザー y_mazun
提出日時 2015-06-19 22:30:25
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 979 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 458 ms
コンパイル使用メモリ 70,144 KB
実行使用メモリ 6,144 KB
最終ジャッジ日時 2026-03-28 18:31:34
合計ジャッジ時間 1,336 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <map>
#include <queue>
#include <cstdio>
#include <set>

#define REP(i,n) for(int i=0; i<(int)(n); i++)

using namespace std;

inline int getInt(){ int s; scanf("%d", &s); return s; }

vector<vector<int> > ans(4, vector<int>(4));
vector<vector<int> > s(4, vector<int>(4));

#define IN(x,s,g) ((x) >= (s) && (x) < (g))
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))

const int _dx[] = {0,1,0,-1};
const int _dy[] = {-1,0,1,0};

bool check(int x, int y, int f){
  if(ans == s) return true;
  REP(i,4){
    const int xx = x + _dx[i];
    const int yy = y + _dy[i];
    if(ISIN(xx, yy, 4, 4)){
      const int t = s[yy][xx];

      if(f & (1 << t)){
	swap(s[y][x], s[yy][xx]);
	if(check(xx, yy, f ^ (1 << t))) return true;
	swap(s[y][x], s[yy][xx]);
      }
    }
  }
  return false;
}

int main(){
  REP(i,4) REP(j,4) ans[i][j] = getInt();
  REP(i,4) REP(j,4) s[i][j] = i * 4 + j + 1;
  s[3][3] = 0;

  puts(check(3, 3, (1 << 16) - 1) ? "Yes" : "No");
  return 0;
}
0