結果

問題 No.340 雪の足跡
ユーザー KilisameKilisame
提出日時 2016-05-24 12:27:17
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 4,319 bytes
コンパイル時間 2,326 ms
コンパイル使用メモリ 79,112 KB
実行使用メモリ 92,364 KB
最終ジャッジ日時 2024-04-16 10:44:38
合計ジャッジ時間 10,541 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 114 ms
45,368 KB
testcase_01 AC 112 ms
40,100 KB
testcase_02 AC 125 ms
41,376 KB
testcase_03 AC 114 ms
39,888 KB
testcase_04 AC 115 ms
40,252 KB
testcase_05 WA -
testcase_06 AC 115 ms
40,584 KB
testcase_07 AC 125 ms
41,328 KB
testcase_08 AC 127 ms
41,144 KB
testcase_09 AC 131 ms
41,060 KB
testcase_10 AC 132 ms
41,388 KB
testcase_11 AC 184 ms
42,148 KB
testcase_12 AC 181 ms
42,156 KB
testcase_13 AC 183 ms
42,152 KB
testcase_14 AC 428 ms
50,152 KB
testcase_15 AC 503 ms
50,480 KB
testcase_16 AC 385 ms
48,596 KB
testcase_17 AC 506 ms
50,692 KB
testcase_18 AC 498 ms
50,284 KB
testcase_19 AC 515 ms
50,344 KB
testcase_20 TLE -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Main{

    private int w;

    private int h;

    /* 4bitを上下左右へ移動可能かどうかのフラグとして扱う。 */
    private boolean[][] blocks;

    private static final String ODEKAKEDEKINAI = "Odekakedekinai..";

    public Main(int w, int h, int[][] b) {
        this.w = w;
        this.h = h;
        blocks = new boolean[w * h][5];
        for (int i = 0; i < b.length; i++) {
            for (int j = 0; j < b[i].length - 1; j++) {
                footprint(b[i][j], b[i][j + 1]);
            }
        }
    }

    private void execute() {
        /* block[0]を起点として、移動開始 */
        Set<Integer> next = new HashSet<Integer>();
        next.add(0);
        int stepCount = 0;
        while (true) {
            next = step(next);
            stepCount++;
            if (next.isEmpty()) {
                System.out.println(ODEKAKEDEKINAI);
                break;
            }
            if (next.contains(w * h - 1)) {
                System.out.println(stepCount);
                break;
            }
        }
    }

    /**
     * StepCount歩数目にはじめて踏破するblcoksのNumberを記録して返す
     * 
     * @param stepCount
     */
    private Set<Integer> step(Set<Integer> beforeBlocks) {
        Set<Integer> nextBlocks = new HashSet<Integer>();
        for (int number : beforeBlocks) {
            /* 現在numberに踏破済みのフラグ(blocks[number][4]=true)を立てる */
            blocks[number][4] = true;
            /* 上移動チェック */
            if (blocks[number][0] && !blocks[number + w][4]) {
                nextBlocks.add(number + w);
            }
            /* 下移動チェック */
            if (blocks[number][1] && !blocks[number - w][4]) {
                nextBlocks.add(number - w);
            }
            /* 左移動チェック */
            if (blocks[number][2] && !blocks[number - 1][4]) {
                nextBlocks.add(number - 1);
            }
            /* 右移動チェック */
            if (blocks[number][3] && !blocks[number + 1][4]) {
                nextBlocks.add(number + 1);
            }
        }
        return nextBlocks;
    }

    private void footprint(int b1, int b2) {
        int min = Math.min(b1, b2);
        int max = Math.max(b1, b2);
        if (min / w == max / w) {
            /* 横の移動 = minとmaxをwで割ったときの値が等しい */
            for (int i = min; i < max; i++) {
                /* 左側(数が小さいほうのblock)から右へと移動できるので、blocks[i][3]をtrue */
                blocks[i][3] = true;
                /* 右側(数が大きいほうのblock)から左へと移動できるので、blocks[i+1][2]をtrue */
                blocks[i + 1][2] = true;
            }
        } else {
            /* 縦の移動 */
            for (int i = min; i < max; i += w) {
                /* 下側(数が小さいほうのblock)から上へと移動できるので、blocks[i][0]をtrue */
                blocks[i][0] = true;
                /* 上側(数が大きいほうのblock)から下へと移動できるので、blocks[i+w][1]をtrue */
                blocks[i + w][1] = true;
            }
        }
    }

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        List<String> lines = new ArrayList<String>();
        while (cin.hasNext()) {
            lines.add(cin.nextLine());
        }
        cin.close();
        String[] blockStr = lines.get(0).split(" ");
        int w = Integer.parseInt(blockStr[0]);
        int h = Integer.parseInt(blockStr[1]);
        int[][] blockInfo = new int[Integer.parseInt(blockStr[2])][];
        for (int i = 0; i < blockInfo.length; i++) {
            int stepCount = Integer.parseInt(lines.get(2 * i + 1));
            blockInfo[i] = new int[stepCount + 1];
            String[] blockInfoStr = lines.get(2 * i + 2).split(" ");
            for (int j = 0; j < blockInfo[i].length; j++) {
                blockInfo[i][j] = Integer.parseInt(blockInfoStr[j]);
            }
        }
        Main snow = new Main(w, h, blockInfo);
        snow.execute();
    }

}
0