結果
| 問題 | No.13 囲みたい! | 
| コンテスト | |
| ユーザー |  tenten | 
| 提出日時 | 2023-01-26 08:32:28 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 129 ms / 5,000 ms | 
| コード長 | 3,272 bytes | 
| コンパイル時間 | 2,681 ms | 
| コンパイル使用メモリ | 84,496 KB | 
| 実行使用メモリ | 42,740 KB | 
| 最終ジャッジ日時 | 2024-06-27 05:50:47 | 
| 合計ジャッジ時間 | 5,027 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 16 | 
ソースコード
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();
    }
    
}
            
            
            
        