結果
| 問題 |
No.3 ビットすごろく
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-06-19 22:07:24 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 1,491 bytes |
| コンパイル時間 | 660 ms |
| コンパイル使用メモリ | 73,236 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-06-30 17:18:32 |
| 合計ジャッジ時間 | 5,500 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 1 RE * 32 |
コンパイルメッセージ
main.cpp: In function 'int GetMinCost(int, std::vector<int>&, std::vector<bool>&, std::vector<int>&)':
main.cpp:75:1: warning: control reaches end of non-void function [-Wreturn-type]
75 | }
| ^
ソースコード
#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;
}