結果

問題 No.3 ビットすごろく
ユーザー iwashi31iwashi31
提出日時 2014-11-20 18:06:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 11 ms / 5,000 ms
コード長 1,021 bytes
コンパイル時間 714 ms
コンパイル使用メモリ 74,692 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-13 22:55:24
合計ジャッジ時間 2,131 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <deque>
#include <list>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <algorithm>

#define INF 100000000

using namespace std;

int N;
int dist[10010];
bool inflag[10010];
queue<int> ready;

int main() {
	int i;

	cin >> N;

	dist[1] = 1;
	for (i = 2; i <= 10000; i++) dist[i] = INF;
	ready.push(1);

	while (!ready.empty()) {
		int now = ready.front(); ready.pop();
		int now_c = now;
		int bits = 0;
		for (i = 13; i >= 0; i--) {
			if (now_c >= pow(2, i)) {
				bits++;
				now_c -= pow(2, i);
			}
		}
		if (now - bits >= 2 && dist[now - bits] > dist[now] + 1) {
			dist[now - bits] = dist[now] + 1;
			ready.push(now - bits);
		}
		if (now + bits <= N && dist[now + bits] > dist[now] + 1) {
			dist[now + bits] = dist[now] + 1;
			ready.push(now + bits);
		}
	}

	if (dist[N] == INF) cout << "-1" << endl;
	else cout << dist[N] << endl;

	return 0;
}
0