結果
| 問題 |
No.402 最も海から遠い場所
|
| コンテスト | |
| ユーザー |
sekiya9311
|
| 提出日時 | 2016-07-28 15:22:59 |
| 言語 | Java (openjdk 23) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 1,894 bytes |
| コンパイル時間 | 5,705 ms |
| コンパイル使用メモリ | 83,192 KB |
| 実行使用メモリ | 725,788 KB |
| 最終ジャッジ日時 | 2024-11-06 18:10:02 |
| 合計ジャッジ時間 | 13,949 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 14 MLE * 1 -- * 4 |
ソースコード
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Yuki402 {
static int H, W;
static String[] S;
static int[][] mp;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
H = sc.nextInt();
W = sc.nextInt();
S = new String[H + 2];
mp = new int[H + 2][W + 2];
sc.nextLine();
for (int i = 0; i < H + 2; i++) {
if (i == 0 || i == H + 1) {
S[i] = str(W + 2, '.');
} else {
S[i] = sc.nextLine();
S[i] = '.' + S[i] + '.';
}
}
System.out.println(Search());
sc.close();
}
static int Search() {
int res = 0;
int[] dh = new int[] { 0, 0, -1, -1, -1, 1, 1, 1 };
int[] dw = new int[] { 1, -1, 1, -1, 0, 1, -1, 0 };
Queue<pair> q = new LinkedList<pair>();
for (int i = 0; i < H + 2; i++) {
for (int j = 0; j < W + 2; j++) {
mp[i][j] = 0;
if (S[i].charAt(j) == '.') {
mp[i][j] = -1;
q.add(new pair(i, j));
}
}
}
while (!q.isEmpty()) {
pair buf = q.poll();
int h = buf.getFirst();
int w = buf.getSecond();
for (int i = 0; i < 8; i++) {
int nh = h + dh[i];
int nw = w + dw[i];
if (nh < 0 || nh >= H + 2)
continue;
if (nw < 0 || nw >= W + 2)
continue;
if (mp[nh][nw] == -1)
continue;
if (mp[h][w] == -1) {
mp[nh][nw] = 1;
q.add(new pair(nh, nw));
} else if (mp[nh][nw] > mp[h][w] + 1 || mp[nh][nw] == 0) {
mp[nh][nw] = mp[h][w] + 1;
q.add(new pair(nh, nw));
}
res = Math.max(res, mp[nh][nw]);
}
}
return res;
}
static String str(int n, char c) {
String res = "";
for (int i = 0; i < n; i++) {
res += c;
}
return res;
}
static class pair {
private int first;
private int second;
pair(int f, int s) {
this.first = f;
this.second = s;
}
int getFirst() {
return this.first;
}
int getSecond() {
return this.second;
}
};
}
sekiya9311