結果

問題 No.3 ビットすごろく
ユーザー mustonmuston
提出日時 2016-05-17 17:12:54
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,502 bytes
コンパイル時間 2,233 ms
コンパイル使用メモリ 78,052 KB
実行使用メモリ 54,384 KB
最終ジャッジ日時 2024-04-15 22:49:20
合計ジャッジ時間 7,460 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 131 ms
41,456 KB
testcase_01 AC 131 ms
42,088 KB
testcase_02 AC 133 ms
41,824 KB
testcase_03 AC 132 ms
41,508 KB
testcase_04 AC 133 ms
41,980 KB
testcase_05 AC 138 ms
41,660 KB
testcase_06 AC 134 ms
41,748 KB
testcase_07 AC 133 ms
42,016 KB
testcase_08 AC 133 ms
42,180 KB
testcase_09 AC 133 ms
41,708 KB
testcase_10 AC 132 ms
41,784 KB
testcase_11 AC 132 ms
41,672 KB
testcase_12 AC 134 ms
41,732 KB
testcase_13 AC 130 ms
41,888 KB
testcase_14 AC 133 ms
41,556 KB
testcase_15 AC 137 ms
41,772 KB
testcase_16 AC 121 ms
40,448 KB
testcase_17 AC 133 ms
42,100 KB
testcase_18 AC 130 ms
41,520 KB
testcase_19 AC 133 ms
41,500 KB
testcase_20 AC 132 ms
41,380 KB
testcase_21 AC 131 ms
41,752 KB
testcase_22 AC 135 ms
41,896 KB
testcase_23 AC 131 ms
41,736 KB
testcase_24 AC 134 ms
41,808 KB
testcase_25 AC 133 ms
41,640 KB
testcase_26 WA -
testcase_27 AC 132 ms
41,976 KB
testcase_28 AC 132 ms
42,016 KB
testcase_29 AC 134 ms
41,888 KB
testcase_30 AC 131 ms
41,888 KB
testcase_31 AC 132 ms
41,796 KB
testcase_32 AC 120 ms
40,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
/*yukicoder No.3 ビットすごろく*/
class No3{
  public static void main(String[] args){
    final int NUMBER = 0;
    final int COST =1;
    Scanner scan = new Scanner(System.in);
    //ゴールまでのマスnを入力
    int n = scan.nextInt();
    int[] log = new int[10000];
    int[][] route = new int[10000][2];  //道を記憶
    
    int i=0;	//
    int t=0;
    route[i][NUMBER] =1;
    route[i][COST] = 1;
    log[i] = 1;
    
//System.out.printf("DBG>> Star\n");
    boolean isEnd = false;
    
    while (!isEnd){
      //移動量を出す(今のマス数を2進数にしたときの1の数)
      int move = Integer.bitCount(route[i][NUMBER]);
      //後退するマスを出す
      int val = route[i][NUMBER] - move;
      if(0 < val&&log[val-1]==0){
        route[t+1][COST] = route[i][COST]+1;
        route[t+1][NUMBER] = val;
        log[route[t+1][NUMBER]-1] =1;
        t++;
        if(route[t][NUMBER] == n)
          isEnd = true;
      }
      //前進するマスを出す
      val = route[i][NUMBER] + move;
      if(val<=n&&log[val-1]==0){
        route[t+1][COST] = route[i][COST]+1;
        route[t+1][NUMBER] = val;
        log[route[t+1][NUMBER]-1] =1;
        t++;
        if(route[t][NUMBER] == n)
          isEnd = true;
        
      }
      
      i++;
      
      if(t<i)
        isEnd = true;
    }
    
//System.out.printf("DBG>> Ref\n");
//    for(int k=0;k<2;k++){
//      for(int l=0;l<=t;l++) {
//        System.out.printf(" %2d", route[l][k]);
//    }
//    System.out.println("");
//    }
//    for(int k=0;k<n;k++)
//      System.out.printf("  %d",log[k]);
//System.out.printf("\nDBG>> End\n");
    if(t<i)
      System.out.println(-1);
    else
      System.out.println(route[t][COST]);
    /*
    for(int i = 0; i < n; i++)
      route[i] = i+1;
    while(n!=here){
      move = Integer.bitCount(here);  //今のマス数を2進数にしたときの1の数
      if(log[here-1]-move==0&&here>1){
        here = route[here-1] - move;
        log[here-1] = 1;
        System.out.println(here);
      }else if(here+move<n){
        here = route[here-1] + move;
        log[here-1] = 1;
        System.out.println(here);
      }else{
        count = -1;
        break;
      }
      count++;
      //ゴールマスに止まったらループから抜ける
      if(log[n-1] == 1)
        break;
    }
    
    
    //ゴールまでの移動回数を出力
    System.out.println(count);
    /* */
  }
}
0