結果

問題 No.3 ビットすごろく
ユーザー denderaKawazudenderaKawazu
提出日時 2018-02-09 01:25:31
言語 Java21
(openjdk 21)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 3,435 bytes
コンパイル時間 3,670 ms
コンパイル使用メモリ 75,020 KB
実行使用メモリ 58,324 KB
最終ジャッジ日時 2023-09-14 00:55:01
合計ジャッジ時間 8,296 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
55,696 KB
testcase_01 AC 121 ms
55,840 KB
testcase_02 AC 120 ms
55,464 KB
testcase_03 AC 136 ms
55,556 KB
testcase_04 AC 125 ms
56,036 KB
testcase_05 AC 145 ms
57,512 KB
testcase_06 AC 134 ms
55,880 KB
testcase_07 AC 127 ms
55,868 KB
testcase_08 AC 141 ms
56,200 KB
testcase_09 AC 151 ms
57,796 KB
testcase_10 AC 160 ms
57,848 KB
testcase_11 AC 159 ms
57,700 KB
testcase_12 AC 149 ms
57,740 KB
testcase_13 AC 131 ms
55,844 KB
testcase_14 AC 165 ms
57,580 KB
testcase_15 AC 169 ms
58,096 KB
testcase_16 AC 169 ms
57,924 KB
testcase_17 AC 166 ms
57,832 KB
testcase_18 AC 133 ms
55,744 KB
testcase_19 AC 167 ms
57,976 KB
testcase_20 AC 131 ms
55,468 KB
testcase_21 WA -
testcase_22 AC 170 ms
57,996 KB
testcase_23 AC 166 ms
57,952 KB
testcase_24 AC 165 ms
58,216 KB
testcase_25 AC 162 ms
58,324 KB
testcase_26 AC 116 ms
55,452 KB
testcase_27 AC 132 ms
55,476 KB
testcase_28 AC 176 ms
58,228 KB
testcase_29 AC 146 ms
58,092 KB
testcase_30 AC 118 ms
55,788 KB
testcase_31 AC 119 ms
55,976 KB
testcase_32 AC 152 ms
55,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.ArrayDeque;
import java.util.ArrayList;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task();
        solver.solve(1, in, out);
        out.close();
    }

    static class Task {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            final int n = in.nextInt();
            if (n == 1) {
                out.println(1);
                return;
            }

            Task.Vertex[] v = new Task.Vertex[n];
            for (int i = 0; i < n; i++) v[i] = new Task.Vertex();

            for (int i = 0; i < n; i++) {
                String s = Integer.toBinaryString(i + 1);
                int len = 0;
                for (char c : s.toCharArray()) if (c == '1') len++;

                if (i + len < n) {
                    v[i].setEdge(v[i + len]);
                }
                if (i - len >= 0) {
                    v[i].setEdge(v[i - len]);
                }
            }

            ArrayDeque<Task.Vertex> vq = new ArrayDeque<>();
            v[0].setValue(1);
            vq.add(v[0]);
            int count = 1;

            while (count < n) {
                int m = vq.size();
                for (int j = 0; j < m; j++) {
                    Task.Vertex cv = vq.poll();
                    if (cv == v[n - 1]) {
                        out.println(count);
                        return;
                    }
                    for (int i = 0; i < cv.getDegree(); i++) {
                        Task.Vertex nv = cv.getEdge(i).getVertex();
                        if (!nv.hasValue()) {
                            vq.add(nv);
                            nv.setValue(1);
                        }
                    }
                }
                count++;
            }

            out.println(-1);
        }

        static class Vertex {
            private int value;
            private boolean hasValue = false;
            private ArrayList<Task.Edge> edges = new ArrayList<Task.Edge>();

            Vertex() {
            }

            void setValue(int v) {
                this.value = v;
                this.hasValue = true;
            }

            void setEdge(Task.Vertex v) {
                this.edges.add(new Task.Edge(v));
            }

            int getDegree() {
                return this.edges.size();
            }

            Task.Edge getEdge(int index) {
                return this.edges.get(index);
            }

            boolean hasValue() {
                return this.hasValue;
            }

        }

        static class Edge {
            private Task.Vertex v;
            private int cost;

            Edge() {
                this.v = v;
            }

            Edge(Task.Vertex v) {
                this.v = v;
            }

            Edge(Task.Vertex v, int c) {
                this.v = v;
                this.cost = c;
            }

            Task.Vertex getVertex() {
                return this.v;
            }

        }

    }
}

0