結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー fura
提出日時 2020-04-25 22:12:36
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 630 bytes
コンパイル時間 2,296 ms
コンパイル使用メモリ 194,940 KB
最終ジャッジ日時 2025-01-10 01:42:05
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:30:24: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   30 |         rep(i,16) scanf("%d",&a[i]);
      |                   ~~~~~^~~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>

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

using namespace std;

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

bool dfs(vector<int> a,int used){
	bool end=true;
	rep(i,15) if(a[i]!=i+1) end=false;
	if(end) return true;

	int i=find(a.begin(),a.end(),0)-a.begin();
	int y=i/4,x=i%4;
	rep(k,4){
		int yy=y+dy[k],xx=x+dx[k];
		int ii=yy*4+xx;
		if(0<=yy && yy<4 && 0<=xx && xx<4 && !(used>>a[ii]&1)){
			swap(a[i],a[ii]);
			if(dfs(a,used|1<<a[i])) return true;
			swap(a[i],a[ii]);
		}
	}
	return false;
}

int main(){
	vector<int> a(16);
	rep(i,16) scanf("%d",&a[i]);
	puts(dfs(a,0)?"Yes":"No");
	return 0;
}
0