結果
| 問題 |
No.228 ゆきこちゃんの 15 パズル
|
| コンテスト | |
| ユーザー |
koyumeishi
|
| 提出日時 | 2015-06-19 22:51:16 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 1,375 bytes |
| コンパイル時間 | 998 ms |
| コンパイル使用メモリ | 82,616 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-07 04:09:32 |
| 合計ジャッジ時間 | 1,577 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 17 |
ソースコード
#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
using namespace std;
vector<vector<int>> goal = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 0}
};
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
int main(){
vector<vector<int>> v(4, vector<int>(4));
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
cin >> v[i][j];
}
}
int zero = -1;
auto x = v;
vector<vector<int>> e(16);
bool ok = true;
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
if(v[i][j] == 0){
zero = i*4 + j;
}
if(i==3 && j == 3) continue;
if(v[i][j] == goal[i][j]) continue;
bool tmp = false;
for(int k=0; k<4; k++){
int yy = i+dy[k];
int xx = j+dx[k];
if(xx<0 || xx>=4 || yy<0 || yy>=4) continue;
if(v[yy][xx] == goal[i][j]){
e[i*4 + j].push_back(yy*4 + xx);
tmp = true;
}
}
if(tmp == false){
ok = false;
}
}
}
for(int i=0; i<16; i++){
if(e[i].size() > 1) ok = false;
}
if(ok == false){
cout << "No" << endl;
return 0;
}
int pos = zero;
while(pos != 15){
if(e[pos].size() == 0) break;
int next = e[pos][0];
swap(v[pos/4][pos%4], v[next/4][next%4]);
pos = next;
}
if(v == goal){
cout << "Yes" << endl;
}else{
cout << "No" << endl;
}
return 0;
}
koyumeishi