結果

問題 No.3 ビットすごろく
ユーザー DAZYDAZY
提出日時 2017-05-21 20:43:21
言語 Java21
(openjdk 21)
結果
AC  
実行時間 291 ms / 5,000 ms
コード長 1,910 bytes
コンパイル時間 3,367 ms
コンパイル使用メモリ 74,256 KB
実行使用メモリ 192,192 KB
最終ジャッジ日時 2023-09-14 00:43:30
合計ジャッジ時間 11,388 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
55,560 KB
testcase_01 AC 126 ms
55,808 KB
testcase_02 AC 126 ms
55,956 KB
testcase_03 AC 160 ms
60,468 KB
testcase_04 AC 142 ms
56,000 KB
testcase_05 AC 196 ms
87,596 KB
testcase_06 AC 162 ms
65,348 KB
testcase_07 AC 152 ms
58,356 KB
testcase_08 AC 176 ms
69,840 KB
testcase_09 AC 213 ms
115,364 KB
testcase_10 AC 248 ms
131,292 KB
testcase_11 AC 203 ms
93,972 KB
testcase_12 AC 195 ms
87,440 KB
testcase_13 AC 155 ms
61,024 KB
testcase_14 AC 238 ms
132,292 KB
testcase_15 AC 285 ms
188,756 KB
testcase_16 AC 255 ms
136,716 KB
testcase_17 AC 262 ms
147,588 KB
testcase_18 AC 154 ms
60,352 KB
testcase_19 AC 290 ms
189,804 KB
testcase_20 AC 145 ms
55,832 KB
testcase_21 AC 125 ms
56,092 KB
testcase_22 AC 240 ms
132,096 KB
testcase_23 AC 291 ms
192,192 KB
testcase_24 AC 288 ms
191,852 KB
testcase_25 AC 287 ms
189,312 KB
testcase_26 AC 124 ms
55,824 KB
testcase_27 AC 159 ms
60,968 KB
testcase_28 AC 258 ms
132,124 KB
testcase_29 AC 203 ms
94,268 KB
testcase_30 AC 126 ms
55,812 KB
testcase_31 AC 126 ms
56,044 KB
testcase_32 AC 201 ms
87,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class No3 {
    public static void main (String[] args) {
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        //tmp:今いるマスの目のコピー
        //mov:動けるマス数 loc:今いるマス目
        //flag:一度も動けない事があるかを監視
        int tmp;
        int ans = -1;
        int mov;
        boolean flag;

        //すでに行ったことのあるマスを記憶
        boolean[] went = new boolean[N+1];
        //次に行くマスを記憶
        boolean[][] next = new boolean[N+1][N+1];

        for (int i = 0; i < N+1; i++) {
            went[i] = false;
            for (int j = 0; j < N+1; j++) {
                next[i][j] = false;
            }
        }
        next[1][1] = true;
        went[1] = true;
        for (int i = 1; i < N+1; i++) {
            flag = false;
            for (int j = 1; j < N+1; j++) {
                if (next[i][j]) {
                    flag = true;
                    if (j == N) {
                        ans = i;
                        break;
                    }
                    mov = 0;
                    tmp = j;
                    //2進数のときbitが1の数を数える
                    while (tmp != 0) {
                        mov += tmp & 1;
                        tmp = tmp >> 1;
                    }
                    //次に行けるマスを追加
                    if (j + mov < N + 1 && !went[j + mov]) {
                        next[i + 1][j + mov] = true;
                        went[j + mov] = true;
                    }
                    if (j - mov > 0 && !went[j - mov]) {
                        next[i + 1][j - mov] = true;
                        went[j - mov] = true;
                    }
                }
            }
            if (!flag) break;
        }
        System.out.print(ans);
    }
}
0