結果

問題 No.13 囲みたい!
ユーザー jp_stejp_ste
提出日時 2020-05-15 19:55:11
言語 Java21
(openjdk 21)
結果
AC  
実行時間 287 ms / 5,000 ms
コード長 1,470 bytes
コンパイル時間 3,428 ms
コンパイル使用メモリ 77,912 KB
実行使用メモリ 62,120 KB
最終ジャッジ日時 2023-10-19 10:09:24
合計ジャッジ時間 7,934 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 147 ms
57,596 KB
testcase_01 AC 152 ms
57,776 KB
testcase_02 AC 142 ms
57,312 KB
testcase_03 AC 277 ms
61,892 KB
testcase_04 AC 260 ms
61,504 KB
testcase_05 AC 284 ms
61,780 KB
testcase_06 AC 257 ms
61,116 KB
testcase_07 AC 259 ms
61,140 KB
testcase_08 AC 267 ms
61,556 KB
testcase_09 AC 287 ms
62,120 KB
testcase_10 AC 212 ms
59,920 KB
testcase_11 AC 259 ms
61,248 KB
testcase_12 AC 192 ms
57,784 KB
testcase_13 AC 220 ms
60,292 KB
testcase_14 AC 226 ms
60,324 KB
testcase_15 AC 147 ms
57,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.LinkedList;
import java.util.Scanner;

public class Main {
	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};
	static LinkedList<Node> q = new LinkedList<>();
	
	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);
			}
		}
		System.out.println("impossible");
	}
	
	static void solve(int y, int x) {
		q.clear();
		q.add(new Node(x, y, -1));
		
		while(!q.isEmpty()) {
			Node now = q.poll();
			if(F[now.y][now.x]) {
				System.out.println("possible");
				System.exit(0);
			}
			int number = M[now.y][now.x];
			F[now.y][now.x] = true;
			
			for(int i=0; i<4; i++) {
				if(now.d == 0 && i == 1) continue;
				if(now.d == 1 && i == 0) continue;
				if(now.d == 2 && i == 3) continue;
				if(now.d == 3 && i == 2) continue;
				int nx = now.x + dx[i];
				int ny = now.y + dy[i];
				if(nx < 0 || nx >= W || ny < 0 || ny >= H) continue;
				if(F[ny][nx]) continue;
				if(M[ny][nx] != number) continue;
				q.add(new Node(nx, ny, i));
			}
		}
	}
	
	static class Node {
		int x, y, d;
		Node(int x, int y, int d) {
			this.x = x;
			this.y = y;
			this.d = d;
		}
	}
}
0