結果
| 問題 |
No.13 囲みたい!
|
| コンテスト | |
| ユーザー |
mastersatoshi
|
| 提出日時 | 2015-08-04 15:40:11 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 419 ms / 5,000 ms |
| コード長 | 2,904 bytes |
| コンパイル時間 | 2,391 ms |
| コンパイル使用メモリ | 80,108 KB |
| 実行使用メモリ | 49,852 KB |
| 最終ジャッジ日時 | 2024-10-13 10:46:31 |
| 合計ジャッジ時間 | 7,786 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 16 |
ソースコード
import java.io.*;
import java.util.*;
public class Main {
static HashMap<String, String> searched = new HashMap();
static int[][] m;
static int w;
static int h;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Scanner sc = new Scanner(System.in);
w = sc.nextInt();
h = sc.nextInt();
m = new int[w][h];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
m[j][i] = sc.nextInt();
}
}
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
if (searched.containsKey(i + "," + j)) {
continue;
}
if (search(i, j)) {
System.out.println("possible");
return;
}
}
}
System.out.println("impossible");
}
private static boolean search(int tmpW, int tmpH) {
int baseNum = m[tmpW][tmpH];
HashMap<String, String> seaching = new HashMap();
HashMap<String, String> tmpSearched = new HashMap();
seaching.put(tmpW + "," + tmpH, "");
String b = null;
while (!seaching.isEmpty()) {
String now = seaching.keySet().iterator().next();
b = seaching.get(now);
seaching.remove(now);
if (tmpSearched.containsKey(now)) {
return true;
}
tmpSearched.put(now, "");
searched.put(now, "");
for (int i = 0; i < 4; i++) {
String[] wh = now.split(",");
int nowW = Integer.parseInt(wh[0]);
int nowH = Integer.parseInt(wh[1]);
if (i == 0) {
if (nowH + 1 < h) {
nowH = nowH + 1;
} else {
continue;
}
} else if (i == 1) {
if (nowW + 1 < w) {
nowW = nowW + 1;
} else {
continue;
}
} else if (i == 2) {
if (nowH - 1 >= 0) {
nowH = nowH - 1;
} else {
continue;
}
} else if (i == 3) {
if (nowW - 1 >= 0) {
nowW = nowW - 1;
} else {
continue;
}
}
String tmpPlace = nowW + "," + nowH;
if (baseNum == m[nowW][nowH]
&& !tmpPlace.equals(b)) {
seaching.put(tmpPlace, now);
}
}
}
return false;
}
}
mastersatoshi