結果
| 問題 |
No.13 囲みたい!
|
| コンテスト | |
| ユーザー |
jp_ste
|
| 提出日時 | 2020-05-15 19:55:11 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 241 ms / 5,000 ms |
| コード長 | 1,470 bytes |
| コンパイル時間 | 3,922 ms |
| コンパイル使用メモリ | 78,088 KB |
| 実行使用メモリ | 47,792 KB |
| 最終ジャッジ日時 | 2024-09-19 06:20:55 |
| 合計ジャッジ時間 | 6,014 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 16 |
ソースコード
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;
}
}
}
jp_ste