結果

問題 No.13 囲みたい!
ユーザー tentententen
提出日時 2023-01-26 08:32:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 116 ms / 5,000 ms
コード長 3,272 bytes
コンパイル時間 2,886 ms
コンパイル使用メモリ 91,728 KB
実行使用メモリ 56,204 KB
最終ジャッジ日時 2023-09-09 12:48:00
合計ジャッジ時間 5,500 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,400 KB
testcase_01 AC 43 ms
49,508 KB
testcase_02 AC 42 ms
49,348 KB
testcase_03 AC 109 ms
53,440 KB
testcase_04 AC 90 ms
51,328 KB
testcase_05 AC 116 ms
56,204 KB
testcase_06 AC 95 ms
36,268 KB
testcase_07 AC 109 ms
38,352 KB
testcase_08 AC 114 ms
39,568 KB
testcase_09 AC 115 ms
39,748 KB
testcase_10 AC 67 ms
34,956 KB
testcase_11 AC 109 ms
38,408 KB
testcase_12 AC 54 ms
34,664 KB
testcase_13 AC 100 ms
37,356 KB
testcase_14 AC 101 ms
53,236 KB
testcase_15 AC 43 ms
49,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static int[][] field;
    static boolean[][] visited;
    static int h;
    static int w;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        w = sc.nextInt();
        h = sc.nextInt();
        field = new int[h][w];
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                field[i][j] = sc.nextInt();
            }
        }
        visited = new boolean[h][w];
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (check(i, j, -1, -1, new HashSet<Integer>(), field[i][j])) {
                    System.out.println("possible");
                    return;
                }
            }
        }
        System.out.println("impossible");
    }
    
    static boolean check(int r, int c, int pr, int pc, HashSet<Integer> used, int value) {
        if (used.contains(r * w + c)) {
            return true;
        }
        if (visited[r][c] || value != field[r][c]) {
            return false;
        }
        visited[r][c] = true;
        used.add(r * w + c);
        if (r > 0) {
            if (r - 1 != pr || c != pc) {
                if (check(r - 1, c, r, c, used, value)) {
                    return true;
                }
            }
        }
        if (r < h - 1) {
            if (r + 1 != pr || c != pc) {
                if (check(r + 1, c, r, c, used, value)) {
                    return true;
                }
            }
        }
        if (c > 0) {
            if (r != pr || c - 1 != pc) {
                if (check(r, c - 1, r, c, used, value)) {
                    return true;
                }
            }
        }
        if (c < w - 1) {
            if (r != pr || c + 1 != pc) {
                if (check(r, c + 1, r, c, used, value)) {
                    return true;
                }
            }
        }
        used.remove(r * w + c);
        return false;
    }
}

class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0