結果

問題 No.402 最も海から遠い場所
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2016-07-22 23:55:58
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,555 ms / 3,000 ms
コード長 1,598 bytes
コンパイル時間 1,708 ms
コンパイル使用メモリ 170,844 KB
実行使用メモリ 225,340 KB
最終ジャッジ日時 2024-04-24 06:23:03
合計ジャッジ時間 9,662 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 8 ms
5,632 KB
testcase_14 AC 5 ms
5,504 KB
testcase_15 AC 37 ms
11,020 KB
testcase_16 AC 55 ms
13,180 KB
testcase_17 AC 744 ms
123,108 KB
testcase_18 AC 1,444 ms
96,348 KB
testcase_19 AC 1,392 ms
159,836 KB
testcase_20 AC 1,390 ms
94,804 KB
testcase_21 AC 1,555 ms
225,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)

typedef long long ll;
int H, W;
string S[3010];
//-----------------------------------------------------------------
struct Comp {
	bool operator() (pair<int,ll> a, pair<int,ll> b) {
		return a.second > b.second;
	}
};
#define INF INT_MAX/2
int dx[8] = { 0,1,1,1,0,-1,-1,-1 };
int dy[8] = { -1,-1,0,1,1,1,0,-1 };
bool done[3010][3010];
ll dist[3010][3010];
ll solve() {
	priority_queue<pair<int, ll>, vector<pair<int, ll> >, Comp> que;

	rep(y, 0, H) rep(x, 0, W) dist[y][x] = INF;

	rep(y, 0, H) rep(x, 0, W) {
		if ((x != 0 && x != W - 1) && (y != 0 && y != H - 1)) continue;

		if (S[y][x] == '#') {
			dist[y][x] = 1;
			done[y][x] = true;
			que.push(make_pair(y * 10000 + x, 1));
		}
		else {
			dist[y][x] = 0;
			done[y][x] = true;
			que.push(make_pair(y * 10000 + x, 0));
		}
	}

	while (!que.empty()) {
		auto q = que.top(); que.pop();

		int x = q.first % 10000;
		int y = q.first / 10000;
		ll c = q.second;

		rep(i, 0, 8) {
			int yy = y + dy[i];
			int xx = x + dx[i];
			if (yy < 0 || H <= yy) continue;
			if (xx < 0 || W <= xx) continue;
			if (done[yy][xx]) continue;

			int cc = c + 1;

			if (S[yy][xx] == '.') cc = 0;

			if (cc < dist[yy][xx]) {
				dist[yy][xx] = cc;
				done[yy][xx] = true;
				que.push(make_pair(yy * 10000 + xx, cc));
			}
		}
	}

	ll ans = 0;
	rep(y, 0, H) rep(x, 0, W) ans = max(ans, dist[y][x]);
	return ans;
}
//-----------------------------------------------------------------
int main()
{
	cin >> H >> W;
	rep(y, 0, H) cin >> S[y];

	cout << solve() << endl;
}
0