結果

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

テストケース

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

ソースコード

diff #

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

void calc(int go[], int min[], int from, int to){
	if(from != to){
		//先へ移動
		if(from + go[from] <= to){
			if(min[from] + 1 < min[ from + go[from] ]){
				min[from + go[from]] = min[from] + 1;
				calc(go, min, from + go[from], to);
			}
		}
		
		//前へ移動
		if(from - go[from] >= 0){
			if(min[from] + 1 < min[ from - go[from] ]){
				min[from - go[from]] = min[from] + 1;
				calc(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;
	}
	
	//移動最低数算出
	min[0] = 1;
	calc(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