結果

問題 No.13 囲みたい!
ユーザー jp_stejp_ste
提出日時 2020-05-15 20:20:25
言語 Java21
(openjdk 21)
結果
AC  
実行時間 265 ms / 5,000 ms
コード長 1,113 bytes
コンパイル時間 4,514 ms
コンパイル使用メモリ 77,600 KB
実行使用メモリ 65,268 KB
最終ジャッジ日時 2023-10-19 10:42:32
合計ジャッジ時間 8,815 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 138 ms
57,496 KB
testcase_01 AC 140 ms
57,680 KB
testcase_02 AC 135 ms
57,476 KB
testcase_03 AC 257 ms
62,216 KB
testcase_04 AC 257 ms
65,268 KB
testcase_05 AC 255 ms
61,684 KB
testcase_06 AC 249 ms
61,528 KB
testcase_07 AC 242 ms
60,788 KB
testcase_08 AC 256 ms
61,204 KB
testcase_09 AC 265 ms
61,876 KB
testcase_10 AC 206 ms
59,872 KB
testcase_11 AC 245 ms
60,700 KB
testcase_12 AC 182 ms
57,712 KB
testcase_13 AC 209 ms
60,084 KB
testcase_14 AC 219 ms
59,940 KB
testcase_15 AC 139 ms
57,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main2 {
	static Scanner scan = new Scanner(System.in);
	static int W, H;
	static int[][] M;
	static boolean[][] F;
	static int[] dx = {0, 0, 1, -1};
	static int[] dy = {1, -1, 0, 0};
	
	public static void main(String[] args) {
		W = scan.nextInt();
		H = scan.nextInt();
		M = new int[H][W];
		F = new boolean[H][W];
		for(int i=0; i<H; i++) {
			for(int j=0; j<W; j++) {
				M[i][j] = scan.nextInt();
			}
		}
		for(int i=0; i<H; i++) {
			for(int j=0; j<W; j++) {
				if(!F[i][j]) solve(i, j, -1);
			}
		}
		System.out.println("impossible");
	}
	
	static void solve(int y, int x, int d) {
		int number = M[y][x];
		F[y][x] = true;
		for(int i=0; i<4; i++) {
			if(d == 0 && i == 1) continue;
			if(d == 1 && i == 0) continue;
			if(d == 2 && i == 3) continue;
			if(d == 3 && i == 2) continue;
			int nx = x + dx[i];
			int ny = y + dy[i];
			if(nx < 0 || nx >= W || ny < 0 || ny >= H) continue;
			if(M[ny][nx] != number) continue;
			if(F[ny][nx]) {
				System.out.println("possible");
				System.exit(0);
			}
			if(F[ny][nx]) continue;
			solve(ny, nx, i);
		}
	}
}
0