結果

問題 No.13 囲みたい!
ユーザー mastersatoshimastersatoshi
提出日時 2015-08-04 15:40:11
言語 Java21
(openjdk 21)
結果
AC  
実行時間 405 ms / 5,000 ms
コード長 2,904 bytes
コンパイル時間 3,450 ms
コンパイル使用メモリ 86,996 KB
実行使用メモリ 50,232 KB
最終ジャッジ日時 2024-04-21 12:20:54
合計ジャッジ時間 7,265 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 135 ms
42,020 KB
testcase_01 AC 134 ms
42,136 KB
testcase_02 AC 137 ms
42,364 KB
testcase_03 AC 340 ms
48,984 KB
testcase_04 AC 249 ms
47,744 KB
testcase_05 AC 405 ms
50,232 KB
testcase_06 AC 286 ms
48,372 KB
testcase_07 AC 360 ms
49,612 KB
testcase_08 AC 364 ms
49,548 KB
testcase_09 AC 383 ms
49,056 KB
testcase_10 AC 249 ms
46,688 KB
testcase_11 AC 358 ms
48,932 KB
testcase_12 AC 202 ms
44,308 KB
testcase_13 AC 294 ms
47,512 KB
testcase_14 AC 295 ms
47,408 KB
testcase_15 AC 162 ms
42,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {

    static HashMap<String, String> searched = new HashMap();
    static int[][] m;
    static int w;
    static int h;

    public static void main(String[] args) throws Exception {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        Scanner sc = new Scanner(System.in);

        w = sc.nextInt();
        h = sc.nextInt();

        m = new int[w][h];

        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                m[j][i] = sc.nextInt();
            }
        }

        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {

                if (searched.containsKey(i + "," + j)) {
                    continue;
                }

                if (search(i, j)) {
                    System.out.println("possible");
                    return;
                }
            }
        }

        System.out.println("impossible");

    }

    private static boolean search(int tmpW, int tmpH) {
        int baseNum = m[tmpW][tmpH];

        HashMap<String, String> seaching = new HashMap();
        HashMap<String, String> tmpSearched = new HashMap();

        seaching.put(tmpW + "," + tmpH, "");
        String b = null;

        while (!seaching.isEmpty()) {
            String now = seaching.keySet().iterator().next();
            b = seaching.get(now);
            seaching.remove(now);
            if (tmpSearched.containsKey(now)) {
                return true;
            }
            tmpSearched.put(now, "");
            searched.put(now, "");

            for (int i = 0; i < 4; i++) {

                String[] wh = now.split(",");
                int nowW = Integer.parseInt(wh[0]);
                int nowH = Integer.parseInt(wh[1]);

                if (i == 0) {
                    if (nowH + 1 < h) {
                        nowH = nowH + 1;
                    } else {
                        continue;
                    }
                } else if (i == 1) {
                    if (nowW + 1 < w) {
                        nowW = nowW + 1;
                    } else {
                        continue;
                    }
                } else if (i == 2) {
                    if (nowH - 1 >= 0) {
                        nowH = nowH - 1;
                    } else {
                        continue;
                    }
                } else if (i == 3) {
                    if (nowW - 1 >= 0) {
                        nowW = nowW - 1;
                    } else {
                        continue;
                    }
                }

                String tmpPlace = nowW + "," + nowH;
                if (baseNum == m[nowW][nowH]
                        && !tmpPlace.equals(b)) {
                    seaching.put(tmpPlace, now);
                }
            }
        }

        return false;
    }
}
0