結果

問題 No.402 最も海から遠い場所
ユーザー sekiya9311sekiya9311
提出日時 2016-07-28 15:22:59
言語 Java21
(openjdk 21)
結果
MLE  
実行時間 -
コード長 1,894 bytes
コンパイル時間 3,645 ms
コンパイル使用メモリ 80,168 KB
実行使用メモリ 727,320 KB
最終ジャッジ日時 2024-04-24 10:33:04
合計ジャッジ時間 12,703 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 144 ms
47,580 KB
testcase_01 AC 152 ms
42,768 KB
testcase_02 AC 165 ms
42,960 KB
testcase_03 AC 148 ms
42,952 KB
testcase_04 AC 161 ms
42,552 KB
testcase_05 AC 144 ms
42,752 KB
testcase_06 AC 146 ms
43,048 KB
testcase_07 AC 149 ms
42,568 KB
testcase_08 AC 153 ms
42,520 KB
testcase_09 AC 164 ms
42,572 KB
testcase_10 AC 163 ms
42,820 KB
testcase_11 AC 167 ms
42,876 KB
testcase_12 AC 168 ms
42,824 KB
testcase_13 AC 319 ms
48,452 KB
testcase_14 AC 212 ms
44,436 KB
testcase_15 AC 544 ms
76,004 KB
testcase_16 AC 486 ms
67,808 KB
testcase_17 MLE -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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;
		}
	};
}
0