結果

問題 No.13 囲みたい!
ユーザー t8m8⛄️t8m8⛄️
提出日時 2016-02-12 20:37:16
言語 Java21
(openjdk 21)
結果
AC  
実行時間 239 ms / 5,000 ms
コード長 1,534 bytes
コンパイル時間 3,457 ms
コンパイル使用メモリ 81,980 KB
実行使用メモリ 47,928 KB
最終ジャッジ日時 2024-04-21 12:22:37
合計ジャッジ時間 7,130 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
41,424 KB
testcase_01 AC 122 ms
40,404 KB
testcase_02 AC 126 ms
41,264 KB
testcase_03 AC 239 ms
47,908 KB
testcase_04 AC 226 ms
47,008 KB
testcase_05 AC 238 ms
47,928 KB
testcase_06 AC 229 ms
46,876 KB
testcase_07 AC 225 ms
46,860 KB
testcase_08 AC 235 ms
47,472 KB
testcase_09 AC 238 ms
47,776 KB
testcase_10 AC 182 ms
43,836 KB
testcase_11 AC 233 ms
47,180 KB
testcase_12 AC 163 ms
42,908 KB
testcase_13 AC 192 ms
44,848 KB
testcase_14 AC 194 ms
45,336 KB
testcase_15 AC 137 ms
41,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;
import java.awt.geom.*;
import java.math.*;

public class No0013 {

	static final Scanner in = new Scanner(System.in);
	static final PrintWriter out = new PrintWriter(System.out,false);
	static boolean debug = false;

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

	static int[][] t;
	static boolean[][] visited;
	static int h, w;

	static boolean rec(int cx, int cy, int px, int py) {
		if (visited[cx][cy]) {
			return true;
		}
		visited[cx][cy] = true;
		for (int i=0; i<4; i++) {
			int nx = cx + dx[i];
			int ny = cy + dy[i];
			if (nx == px && ny == py) continue;
			if (0 <= nx && 0 <= ny && nx < h && ny < w && t[nx][ny] == t[cx][cy]) {
				if (rec(nx, ny, cx, cy)) return true;
			}
		}

		return false;
	}

	static void solve() {
		w = in.nextInt();
		h = in.nextInt();
		t = new int[h][w];
		for (int i=0; i<h; i++) {
			for (int j=0; j<w; j++) {
				t[i][j] = in.nextInt();
			}
		}

		visited = new boolean[h][w];

		for (int i=0; i<h; i++) {
			for (int j=0; j<w; j++) {
				if (visited[i][j]) continue;
				if (rec(i, j, -1, -1)) {
					out.println("possible");
					return;
				}
			}
		}

		out.println("impossible");
	}

	public static void main(String[] args) {
		debug = args.length > 0;
		long start = System.currentTimeMillis();

		solve();
		out.flush();

		long end = System.currentTimeMillis();
		dump((end-start) + "ms");
		in.close();
		out.close();
	}

	static void dump(Object... o) { if (debug) System.err.println(Arrays.deepToString(o)); }
}
0