結果

問題 No.3 ビットすごろく
ユーザー 158b158b
提出日時 2015-05-12 11:35:34
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,040 bytes
コンパイル時間 426 ms
コンパイル使用メモリ 62,840 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-13 23:09:05
合計ジャッジ時間 1,680 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
using namespace std;

void calc(int step, int go[], int min[], int from, int to){
	if(step < min[from]){
		min[from] = step;
		
		//前に進む
		if(from + go[from] <= to){
			calc(step + 1, go, min, from + go[from], to);
		}
		//後ろに進む
		if(from - go[from] >= 0){
			calc(step + 1, go, min, from - go[from], to);
		}
	}
}
int main(){
	//始まりの数値は1
	const int Max = 10000;
	int go[Max] = {0};
	int min[Max];
	int n;
	int least;
	
	cin >> n;
	
	//移動可能数算出
	for(int i=0; i<n; i++){
		least = i + 1;
		while(least > 0){
			if(least % 2 == 1){
				go[i] ++;
			}
			least /= 2;
		}
	}
	
	//min表初期化
	for(int i=0; i<n; i++){
		min[i] = 99999;
	}
	
	//移動最低数算出(再帰)
	calc(1, go, min, 0, n-1);
	
	//結果出力
	if(min[n-1] == 99999){
		cout << -1 << endl;
	}else{
		cout << min[n-1] << endl;
	}
	
	//全て出力
	/*
	cout << endl;
	for(int i=0; i<n; i++){
		cout << min[i] << endl;
	}
	*/
	
	return 0;
}
0