結果
問題 | No.3 ビットすごろく |
ユーザー |
![]() |
提出日時 | 2015-05-05 03:50:39 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 1 ms / 5,000 ms |
コード長 | 791 bytes |
コンパイル時間 | 152 ms |
コンパイル使用メモリ | 22,528 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-01 07:20:16 |
合計ジャッジ時間 | 1,107 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:11:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 11 | scanf("%d", &n); | ~~~~~^~~~~~~~~~
ソースコード
#include <stdio.h>#define MAX 10000int bit_count(int);void search();int n, memo[MAX + 1], queue[MAX + 1]; // 例によって0初期化int main(void) {scanf("%d", &n);search();printf("%d\n", memo[n] ? memo[n] : -1); // 到達できない時は0と考えた方が自然な気がするreturn 0;}int bit_count(int n) {int cnt = 0;while(n) {cnt += n % 2;n /= 2;}return cnt;}void search() {int get = 0, set = 0;memo[1] = 1;queue[set] = 1;set++;while(get != set) {int c = queue[get], b = bit_count(c);int l = c - b, r = c + b;if(c == n) { break; }if(0 <= l && memo[l] == 0) {memo[l] = memo[c] + 1;queue[set] = l;set++;}if(r <= n && memo[r] == 0) {memo[r] = memo[c] + 1;queue[set] = r;set++;}get++;}}