結果

問題 No.3 ビットすごろく
ユーザー denderaKawazudenderaKawazu
提出日時 2018-02-09 00:35:52
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,846 bytes
コンパイル時間 1,951 ms
コンパイル使用メモリ 78,916 KB
実行使用メモリ 61,460 KB
最終ジャッジ日時 2024-04-09 20:21:41
合計ジャッジ時間 8,990 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
54,200 KB
testcase_01 AC 110 ms
54,404 KB
testcase_02 AC 120 ms
52,848 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
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 {
        static int dfs(Task.Vertex[] v, Task.Vertex cv, int count, int min) {
            int cCount = count + 1;
            if (cCount > min || cCount > v.length + 1) return min;

            int cMin = min;
            for (int i = 0; i < cv.getDegree(); i++) {
                if (cv.getEdge(i).getVertex() == v[v.length - 1]) {
                    return cCount + 1;
                } else cMin = Math.min(dfs(v, cv.getEdge(i).getVertex(), cCount, cMin), cMin);
            }

            return cMin;
        }

        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]);
                }
            }

            int min = dfs(v, v[0], 0, Integer.MAX_VALUE);
            min = min == Integer.MAX_VALUE ? -1 : min;

            out.println(min);
        }

        static class Vertex {
            private ArrayList<Task.Edge> edges = new ArrayList<Task.Edge>();

            Vertex() {
            }

            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);
            }

        }

        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