結果
| 問題 |
No.228 ゆきこちゃんの 15 パズル
|
| コンテスト | |
| ユーザー |
tkmst201
|
| 提出日時 | 2017-05-06 11:31:16 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 1,473 bytes |
| コンパイル時間 | 1,560 ms |
| コンパイル使用メモリ | 168,584 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-09-14 13:49:30 |
| 合計ジャッジ時間 | 2,633 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 17 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:25:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
25 | scanf("%d", &a);
| ~~~~~^~~~~~~~~~
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll INF = 1ll<<29;
const ll MOD = 1000000007;
const double EPS = 1e-10;
int main() {
vector<int> in;
REP(i, 16) {
int a;
scanf("%d", &a);
if (a == 0) in.push_back(15);
else in.push_back(a - 1);
}
vector<int> tmp;
REP(i, 16) tmp.push_back(i);
queue<pair<vector<int>, int> > que;
que.push(make_pair(tmp, 15));
bool ans = false;
while (!que.empty()) {
vector<int> now = que.front().first;
int pos = que.front().second;
que.pop();
if (now == in) ans = true;
int x = pos % 4, y = pos / 4;
int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
REP(i, 4) {
int nx = x + dx[i];
int ny = y + dy[i];
if (!(nx >= 0 && nx < 4 && ny >= 0 && ny < 4)) continue;
int npos = ny * 4 + nx;
int tx = now[npos] % 4, ty = now[npos] / 4;
if (abs(tx - nx) + abs(ty - ny) == 1) continue;
swap(now[pos], now[npos]);
que.push(make_pair(now, npos));
swap(now[pos], now[npos]);
}
}
puts(ans ? "Yes" : "No");
return 0;
}
tkmst201