結果

問題 No.13 囲みたい!
ユーザー GrenacheGrenache
提出日時 2016-05-28 18:40:50
言語 Java21
(openjdk 21)
結果
AC  
実行時間 254 ms / 5,000 ms
コード長 2,015 bytes
コンパイル時間 3,892 ms
コンパイル使用メモリ 78,964 KB
実行使用メモリ 48,624 KB
最終ジャッジ日時 2024-04-21 12:26:45
合計ジャッジ時間 6,905 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 110 ms
41,184 KB
testcase_01 AC 101 ms
40,140 KB
testcase_02 AC 103 ms
40,160 KB
testcase_03 AC 235 ms
46,460 KB
testcase_04 AC 214 ms
46,164 KB
testcase_05 AC 243 ms
48,028 KB
testcase_06 AC 219 ms
46,544 KB
testcase_07 AC 240 ms
48,604 KB
testcase_08 AC 252 ms
48,624 KB
testcase_09 AC 254 ms
48,308 KB
testcase_10 AC 183 ms
43,728 KB
testcase_11 AC 233 ms
46,432 KB
testcase_12 AC 155 ms
42,284 KB
testcase_13 AC 191 ms
45,528 KB
testcase_14 AC 197 ms
45,904 KB
testcase_15 AC 122 ms
41,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;


public class Main_yukicoder13 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int[] dx = {1, -1, 0, 0};
        int[] dy = {0, 0, 1, -1};

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

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

        boolean[][] used = new boolean[h][w];
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
            	if (used[i][j]) {
            		continue;
            	}

            	Deque<Integer> qx = new ArrayDeque<>();
            	Deque<Integer> qy = new ArrayDeque<>();
            	Deque<Integer> qpx = new ArrayDeque<>();
            	Deque<Integer> qpy = new ArrayDeque<>();
            	qy.push(i);
            	qx.push(j);
            	qpy.push(-1);
            	qpx.push(-1);
            	used[i][j] = true;

            	while (!qx.isEmpty()) {
            		int x = qx.pop();
            		int y = qy.pop();
            		int px = qpx.pop();
            		int py = qpy.pop();

            		for (int k = 0; k < dx.length; k++) {
            			int nx = x + dx[k];
            			int ny = y + dy[k];
            			if (nx < 0 || nx >= w || ny < 0 || ny >= h) {
            				continue;
            			}
            			if (nx == px && ny == py) {
            				continue;
            			}
            			if (field[ny][nx] != field[i][j]) {
            				continue;
            			}

            			if (used[ny][nx]) {
            				System.out.println("possible");

            				sc.close();
            				return;
            			}

            			qx.push(nx);
            			qy.push(ny);
            			qpx.push(x);
            			qpy.push(y);
            			used[ny][nx] = true;
            		}
            	}
            }
        }

        System.out.println("impossible");

        sc.close();
    }
}
0