結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー bal4ubal4u
提出日時 2019-07-06 09:03:51
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 8 ms / 5,000 ms
コード長 1,707 bytes
コンパイル時間 348 ms
コンパイル使用メモリ 31,588 KB
実行使用メモリ 8,352 KB
最終ジャッジ日時 2023-10-24 22:59:24
合計ジャッジ時間 1,321 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
8,352 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 8 ms
8,196 KB
testcase_04 AC 4 ms
4,348 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 6 ms
6,252 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 4 ms
4,348 KB
testcase_09 AC 6 ms
5,940 KB
testcase_10 AC 6 ms
6,396 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 8 ms
8,312 KB
testcase_13 AC 8 ms
8,204 KB
testcase_14 AC 6 ms
6,744 KB
testcase_15 AC 7 ms
7,788 KB
testcase_16 AC 7 ms
7,396 KB
testcase_17 AC 6 ms
6,676 KB
testcase_18 AC 8 ms
8,088 KB
testcase_19 AC 6 ms
6,720 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'in':
main.c:9:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
    9 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:15:24: note: in expansion of macro 'gc'
   15 |         int n = 0, c = gc();
      |                        ^~

ソースコード

diff #

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

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

typedef unsigned long long ull;
#if 1
#define gc() getchar_unlocked()
#else
#define gc() getchar()
#endif

int in() {   // 非負整数の入力
	int n = 0, c = gc();
	do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0');
	return n;
}

#define HASHSIZ 2000081
ull hash[HASHSIZ+5], *hashend = hash + HASHSIZ;

int insert(char a[4][4])
{
	int r, c;
	ull s, *p;

	s = 0;
	for (r = 0; r < 4; r++) for (c = 0; c < 4; c++) {
		s <<= 4, s |= a[r][c];
	}
	p = hash + (int)(s % HASHSIZ);
	while (*p) {
		if (*p == s) return 0;
		if (++p == hashend) p = hash;
	}
	*p = s;
	return 1;
}

typedef struct { char a[4][4], r, c; int b; } Q;
Q q[1000000]; 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}};

void swap(char b[4][4], char a[4][4], int nr, int nc, int r, int c) {
	int t;
	memcpy(b, a, 16);
	t = b[r][c], b[r][c] = b[nr][nc], b[nr][nc] = t;
}

int main()
{
	int i, b, t, r, c, rr, cc;
	char a[4][4], aa[4][4];
	
	for (r = 0; r < 4; r++) for (c = 0; c < 4; c++) {
		q[0].a[r][c] = t = in();
		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; }
		if (!insert(a)) continue;
		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;
			swap(aa, a, rr, cc, r, c);
			q[top].r = rr, q[top].c = cc, memcpy(q[top].a, aa, 16), q[top++].b = b | (1 << a[rr][cc]);
		}
	}
	puts("No");
	return 0;
}
0