結果
| 問題 |
No.228 ゆきこちゃんの 15 パズル
|
| コンテスト | |
| ユーザー |
tetsuzuki1115
|
| 提出日時 | 2017-09-28 02:44:14 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 1,789 bytes |
| コンパイル時間 | 646 ms |
| コンパイル使用メモリ | 79,328 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-15 17:54:37 |
| 合計ジャッジ時間 | 1,377 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 17 |
ソースコード
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <math.h>
#include <cmath>
#include <limits.h>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <functional>
#include <stdio.h>
using namespace std;
long long MOD = 1000000007;
int Board[4][4];
int Board_Goal[4][4];
int Board_Used[4][4];
int direc[4][2] = { {1,0}, {0,1}, {-1,0}, {0,-1} };
int dfs() {
pair<int,int> index_zero;
bool is_all_ok = true;
for ( int i = 0; i < 4; i++ ) {
for ( int j = 0; j < 4; j++ ) {
if ( Board_Used[i][j] && Board_Used[i][j] != Board_Goal[i][j] ) {
return 0;
}
if ( Board[i][j] != Board_Goal[i][j] ) { is_all_ok = false; }
if ( !Board[i][j] ) { index_zero.first = i; index_zero.second = j; }
}
}
if ( is_all_ok ) { return 1; }
int ret = 0;
for ( int i = 0; i < 4; i++ ) {
int x0 = index_zero.first;
int y0 = index_zero.second;
int x1 = index_zero.first + direc[i][0];
int y1 = index_zero.second + direc[i][1];
if ( x1 >= 0 && y1 >= 0 && x1 < 4 && y1 < 4 && !Board_Used[x1][y1] ) {
Board_Used[x0][y0] = Board[x1][y1];
swap( Board[x0][y0], Board[x1][y1] );
ret = max( ret, dfs() );
Board_Used[x0][y0] = 0;
swap( Board[x0][y0], Board[x1][y1] );
}
}
return ret;
}
int main() {
for ( int i = 0; i < 4; i++ ) {
for ( int j = 0; j < 4; j++ ) {
int a;
cin >> a;
Board_Goal[i][j] = a;
Board[i][j] = (i*4+j+1)%16;
Board_Used[i][j] = 0;
}
}
cout << ( dfs() ? "Yes" : "No" ) << endl;
return 0;
}
tetsuzuki1115