結果

問題 No.3 ビットすごろく
ユーザー denderaKawazudenderaKawazu
提出日時 2018-02-09 01:21:06
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,440 bytes
コンパイル時間 2,043 ms
コンパイル使用メモリ 78,844 KB
実行使用メモリ 56,592 KB
最終ジャッジ日時 2024-04-09 20:46:55
合計ジャッジ時間 7,693 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 109 ms
53,292 KB
testcase_01 AC 117 ms
53,984 KB
testcase_02 AC 116 ms
54,024 KB
testcase_03 AC 124 ms
53,144 KB
testcase_04 AC 122 ms
52,820 KB
testcase_05 AC 153 ms
56,592 KB
testcase_06 AC 123 ms
53,124 KB
testcase_07 AC 124 ms
54,232 KB
testcase_08 AC 140 ms
54,356 KB
testcase_09 AC 152 ms
56,408 KB
testcase_10 AC 165 ms
55,924 KB
testcase_11 AC 141 ms
56,444 KB
testcase_12 AC 153 ms
56,272 KB
testcase_13 AC 131 ms
54,196 KB
testcase_14 AC 142 ms
56,072 KB
testcase_15 AC 164 ms
56,592 KB
testcase_16 AC 148 ms
56,424 KB
testcase_17 AC 154 ms
56,352 KB
testcase_18 AC 122 ms
54,360 KB
testcase_19 AC 152 ms
56,500 KB
testcase_20 AC 126 ms
54,120 KB
testcase_21 AC 108 ms
53,080 KB
testcase_22 AC 156 ms
56,244 KB
testcase_23 AC 170 ms
56,348 KB
testcase_24 AC 160 ms
56,400 KB
testcase_25 AC 159 ms
56,424 KB
testcase_26 WA -
testcase_27 AC 128 ms
53,100 KB
testcase_28 AC 142 ms
56,156 KB
testcase_29 AC 148 ms
56,344 KB
testcase_30 AC 113 ms
53,800 KB
testcase_31 AC 117 ms
53,852 KB
testcase_32 AC 138 ms
56,220 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();
            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();
                    for (int i = 0; i < cv.getDegree(); i++) {
                        Task.Vertex nv = cv.getEdge(i).getVertex();
                        if (nv == v[n - 1]) {
                            count++;
                            out.println(count);
                            return;
                        } else {
                            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