結果

問題 No.3 ビットすごろく
ユーザー bal4ubal4u
提出日時 2019-04-07 18:04:09
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 1,000 bytes
コンパイル時間 731 ms
コンパイル使用メモリ 30,044 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-14 01:15:47
合計ジャッジ時間 1,969 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 0 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 0 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 0 ms
4,380 KB
testcase_15 AC 0 ms
4,376 KB
testcase_16 AC 0 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 0 ms
4,376 KB
testcase_24 AC 0 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 0 ms
4,376 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 0 ms
4,376 KB
testcase_32 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// yukicoder: No.3 ビットすごろく
// 2019.4.7 bal4u
// popcount + bfs

#include <stdio.h>

/* 2^16までのビット数を数える */
int bitcount(int x)
{
	x = ((x & 0xAAAAAA) >> 1) + (x & 0x555555);
	x = ((x & 0xCCCCCC) >> 2) + (x & 0x333333);
	x = ((x & 0xF0F0F0) >> 4) + (x & 0x0F0F0F);
	x = ((x & 0x00FF00) >> 8) + (x & 0xFF00FF);
	return x;
}

#define MAX 10005
int b[MAX];

typedef struct { int n, s; } Q;
Q q[MAX]; int top, end;
char vis[MAX];

int bfs(int start, int goal)
{
	int n, s, nx;

	q[0].n = start, q[0].s = 1, top = 0, end = 1;
	while (top != end) {
		n = q[top].n, s = q[top++].s;
		if (n == goal) return s;
		if (vis[n]) continue;
		vis[n] = 1;
		nx = n + b[n];
		if (nx <= goal && !vis[nx]) q[end].n = nx, q[end++].s = s + 1;
		nx = n - b[n];
		if (nx >= start && !vis[nx]) q[end].n = nx, q[end++].s = s + 1;
	}
	return -1;
}

int main()
{
	int i, N;

	for (i = 1; i < MAX; i++) b[i] = bitcount(i); // 前計算
	scanf("%d", &N);
	printf("%d\n", bfs(1, N));
	return 0;
}
0