結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2016-04-25 15:27:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 958 bytes
コンパイル時間 1,213 ms
コンパイル使用メモリ 164,988 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-15 04:26:46
合計ジャッジ時間 1,793 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 1 ms
6,944 KB
testcase_18 AC 1 ms
6,940 KB
testcase_19 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘std::string solve(vvi)’:
main.cpp:36:29: warning: ‘y’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   36 |                         int yy = y + dy[i];
      |                             ^~
main.cpp:35:29: warning: ‘x’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   35 |                         int xx = x + dx[i];
      |                             ^~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)

typedef vector<int> vi;
typedef vector<vi> vvi;

string solve(vvi a)
{
	vvi done(4, vi(4, 0));

	int x, y;

	rep(yy, 0, 4) rep(xx, 0, 4) if (a[yy][xx] == 0)
	{
		x = xx;
		y = yy;
	}

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

	while (1)
	{
		bool ok = true;
		rep(yy, 0, 4) rep(xx, 0, 4)
		{
			if ((yy * 4 + xx + 1) % 16 != a[yy][xx]) ok = false;
		}
		if (ok) return "Yes";

		ok = false;
		rep(i, 0, 4)
		{
			int xx = x + dx[i];
			int yy = y + dy[i];
			if (xx < 0 || 4 <= xx) continue;
			if (yy < 0 || 4 <= yy) continue;
			if (done[yy][xx] == 1) continue;

			if (a[yy][xx] == (y * 4 + x + 1) % 16)
			{
				swap(a[y][x], a[yy][xx]);
				ok = true;
				done[y][x] = 1;
				y = yy;
				x = xx;
				break;
			}
		}
		if (!ok) return "No";
	}
}

int main()
{
	vvi a(4, vi(4, 0));

	rep(y, 0, 4) rep(x, 0, 4) cin >> a[y][x];

	cout << solve(a) << endl;
}
0