結果

問題 No.13 囲みたい!
ユーザー t8m8⛄️
提出日時 2016-02-12 20:37:16
言語 Java
(openjdk 23)
結果
AC  
実行時間 263 ms / 5,000 ms
コード長 1,534 bytes
コンパイル時間 3,460 ms
コンパイル使用メモリ 83,780 KB
実行使用メモリ 47,476 KB
最終ジャッジ日時 2024-10-13 10:48:32
合計ジャッジ時間 7,577 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

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