結果

問題 No.3 ビットすごろく
ユーザー muston
提出日時 2016-05-17 17:16:01
言語 Java
(openjdk 23)
結果
AC  
実行時間 136 ms / 5,000 ms
コード長 2,507 bytes
コンパイル時間 2,036 ms
コンパイル使用メモリ 77,740 KB
実行使用メモリ 54,456 KB
最終ジャッジ日時 2024-07-01 07:54:58
合計ジャッジ時間 7,516 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

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&&1<n)
      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