結果

問題 No.402 最も海から遠い場所
ユーザー pekempeypekempey
提出日時 2016-07-22 22:34:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 579 ms / 3,000 ms
コード長 1,041 bytes
コンパイル時間 1,582 ms
コンパイル使用メモリ 171,100 KB
実行使用メモリ 121,356 KB
最終ジャッジ日時 2024-04-24 05:40:54
合計ジャッジ時間 5,080 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 4 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 15 ms
5,632 KB
testcase_16 AC 19 ms
6,400 KB
testcase_17 AC 253 ms
42,540 KB
testcase_18 AC 579 ms
51,584 KB
testcase_19 AC 443 ms
121,356 KB
testcase_20 AC 500 ms
47,744 KB
testcase_21 AC 423 ms
84,580 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
	int h, w;
	cin >> h >> w;

	vector<string> g(h + 2, string(w + 2, '.'));
	for (int i = 0; i < h; i++) {
		string s;
		cin >> s;
		for (int j = 0; j < w; j++) {
			g[i + 1][j + 1] = s[j];
		}
	}
	h += 2;
	w += 2;

	vector<vector<int>> dist(h, vector<int>(w, -1));

	queue<pair<int, int>> q;
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			if (g[i][j] != '#') {
				dist[i][j] = 0;
				q.emplace(i, j);
			}
		}
	}

	while (!q.empty()) {
		int y, x;
		tie(y, x) = q.front(); q.pop();

		const int dy[] = { 0, 0, 1, 1, 1, -1, -1, -1 };
		const int dx[] = { 1, -1, 1, 0, -1, 1, 0, -1 };

		for (int k = 0; k < 8; k++) {
			int ny = y + dy[k];
			int nx = x + dx[k];

			if (ny < 0 || nx < 0 || ny >= h || nx >= w) continue;
			if (dist[ny][nx] == -1) {
				dist[ny][nx] = dist[y][x] + 1;
				q.emplace(ny, nx);
			}
		}
	}

	int maxi = 0;
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			maxi = max(maxi, dist[i][j]);
		}
	}
	cout << maxi << endl;
}
0