結果
問題 |
No.228 ゆきこちゃんの 15 パズル
|
ユーザー |
![]() |
提出日時 | 2016-09-20 13:08:12 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 883 bytes |
コンパイル時間 | 1,503 ms |
コンパイル使用メモリ | 158,456 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-17 10:08:37 |
合計ジャッジ時間 | 2,211 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 17 |
ソースコード
#include<bits/stdc++.h> using namespace std; const int n=4; int A[4][4]; bool vis[4][4]; const int dy[]={0, 1, 0,-1}; const int dx[]={1, 0,-1, 0}; bool dfs(int i, int j){ if(vis[i][j]) return false; vis[i][j]=true; bool ok=true; for(int k=0;k<n;k++) for(int l=0;l<n;l++){ ok &= A[k][l] == (k*4 + l+1) %16; } if(ok) return true; for(int d=0;d<4;d++){ int yy= i + dy[d], xx= j + dx[d]; if(yy<0 || yy>=n || xx<0 || xx>=n) continue; swap(A[i][j], A[yy][xx]); if(dfs(yy, xx)) return true; swap(A[i][j], A[yy][xx]); } vis[i][j]=false; return false; } int main(){ while(1){ for(int i=0;i<n;i++) for(int j=0;j<n;j++){ if(!(cin >> A[i][j])) return 0; } memset(vis,0,sizeof(vis)); for(int i=0;i<n;i++) for(int j=0;j<n;j++){ if(A[i][j]==0){ bool ans=dfs(i,j); cout << (ans? "Yes":"No") << endl; goto aa; } } aa:; } return 0; }