結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー bal4ubal4u
提出日時 2019-07-06 09:42:05
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 994 bytes
コンパイル時間 758 ms
コンパイル使用メモリ 30,888 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 18:05:14
合計ジャッジ時間 1,677 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// yukicoder: No.228 ゆきこちゃんの15パズル
// 2019.7.6 bal4u

#include <stdio.h>
#include <string.h>

typedef struct { char a[4][4], r, c; int b; } Q;
Q q[1000]; int top;
char s[4][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0}};
int mv[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};

int main()
{
	int i, b, r, c, rr, cc;
	char a[4][4];
	
	for (r = 0; r < 4; r++) for (c = 0; c < 4; c++) {
		int t;
		scanf("%d", &t);
		q[0].a[r][c] = t;
		if (t == 0) q[0].r = r, q[0].c = c;
	}
	top = 1; while (top) {
		r = q[--top].r, c = q[top].c, memcpy(a, q[top].a, 16), b = q[top].b;
		if (memcmp(a, s, 16) == 0) { puts("Yes"); return 0; }
		for (i = 0; i < 4; i++) {
			rr = r + mv[i][0], cc = c + mv[i][1];
			if (rr < 0 || rr >= 4 || cc < 0 || cc >= 4) continue;
			if ((b >> a[rr][cc]) & 1) continue;
			q[top].r = rr, q[top].c = cc, q[top].b = b | (1 << a[rr][cc]);
			memcpy(q[top].a, a, 16), q[top].a[rr][cc] = a[r][c], q[top].a[r][c] = a[rr][cc];
			top++;
		}
	}
	puts("No");
	return 0;
}
0