結果

問題 No.3 ビットすごろく
ユーザー ohreitetsuohreitetsu
提出日時 2018-06-19 22:07:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,491 bytes
コンパイル時間 671 ms
コンパイル使用メモリ 72,996 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-13 07:12:31
合計ジャッジ時間 5,441 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 AC 1 ms
4,376 KB
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int GetMinCost(int, std::vector<int>&, std::vector<bool>&, std::vector<int>&)’ 内:
main.cpp:75:1: 警告: 制御が非 void 関数の終りに到達しました [-Wreturn-type]
   75 | }
      | ^

ソースコード

diff #

#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
int N;
int get1Nums(int n)
{
	char S[100];
	memset(S,'\0',100);
	sprintf(S,"%x",n);
	int num=0;
	char *p=S;
	while (*p!='\0'){
		switch(*p){
			case '1'://0001
			case '2'://0010
			case '4'://0100
			case '8'://1000
				num+=1;
				break;
			case '3'://0011
			case '5'://0101
			case '6'://0110
			case '9'://1001
			case 'a'://1010
			case 'c'://1100
				num+=2;
				break;
			case '7'://0111
			case 'b'://1011
			case 'd'://1101
			case 'e'://1110
				num+=3;
				break;
			case 'f':
				num+=4;
				break;
		}
		p++;
	}
	return num;
}
int GetMinCost(int n,vector<int> &A,vector<bool> &visited,vector<int> &cost)
{
	if (cost[N]!=0){
		return cost[N];
	}

	bool left=false;
	bool right=false;
	visited[n]=true;
	int nRight=n+A[n];
	if (nRight<=N){
		if (!visited[nRight]){
			visited[nRight]=true;
			cost[nRight]=cost[n]+1;
			GetMinCost(nRight,A,visited,cost);
			right=true;
		}
	}	
	int nLeft=n-A[n];
	if (nLeft>0){
		if (!visited[nLeft]){
			visited[nLeft]=true;
			cost[nLeft]=cost[n]+1;
			GetMinCost(nLeft,A,visited,cost);
			left=true;
		}
	}
	
	if (!left && !right){
		return -1;
	}

}
int main(int argc, char* argv[])
{

	cin>>N;
	vector<int> A(N+1);
	vector<bool> visited(N+1);
	vector<int> cost(N+1,0);

	for (int i=1;i<=N;i++){
		A[i]=get1Nums(i);
	}
	visited[1]=true;
	cost[1]=1;
	GetMinCost(1,A,visited,cost);
	if (cost[N]!=0){
		cout<<cost[N]<<endl;
	}else{
		cout<<-1<<endl;
	}
	return 0;
}
0