結果

問題 No.402 最も海から遠い場所
ユーザー startcppstartcpp
提出日時 2016-07-24 12:33:57
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 519 ms / 3,000 ms
コード長 1,566 bytes
コンパイル時間 1,432 ms
コンパイル使用メモリ 72,396 KB
実行使用メモリ 156,288 KB
最終ジャッジ日時 2023-08-06 12:41:57
合計ジャッジ時間 3,993 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,496 KB
testcase_01 AC 2 ms
5,608 KB
testcase_02 AC 2 ms
5,816 KB
testcase_03 AC 2 ms
5,640 KB
testcase_04 AC 2 ms
5,516 KB
testcase_05 AC 2 ms
5,500 KB
testcase_06 AC 2 ms
5,412 KB
testcase_07 AC 2 ms
5,776 KB
testcase_08 AC 2 ms
5,568 KB
testcase_09 AC 2 ms
5,444 KB
testcase_10 AC 2 ms
5,508 KB
testcase_11 AC 2 ms
5,604 KB
testcase_12 AC 2 ms
5,588 KB
testcase_13 AC 5 ms
6,600 KB
testcase_14 AC 3 ms
8,432 KB
testcase_15 AC 14 ms
12,052 KB
testcase_16 AC 19 ms
16,744 KB
testcase_17 AC 205 ms
58,588 KB
testcase_18 AC 519 ms
53,280 KB
testcase_19 AC 338 ms
156,288 KB
testcase_20 AC 304 ms
47,620 KB
testcase_21 AC 295 ms
102,852 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//サンプルがやさしい(な阪関)
#include <iostream>
#include <algorithm>
#include <tuple>
#include <queue>
#include <cstdio>
using namespace std;

int h, w;
char s[3002][3002];

int dy[8] = {-1, -1, -1,  0,  0,  1,  1,  1};
int dx[8] = {-1,  0,  1, -1,  1, -1,  0,  1};
int cost[3002][3002];

void input() {
	//番兵する
	cin >> h >> w;
	h += 2;
	w += 2;
	
	for (int i = 0; i < h; i++) { 
		for (int j = 0; j < w; j++) {
			s[i][j] = '.';
		}
	}
	
	for (int i = 1; i < h - 1; i++) { scanf("%s", s[i] + 1); s[i][w-1] = '.'; }
	
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			cost[i][j] = 1145141919;
		}
	}
}

inline bool is_range(int y, int x) {
	return (0 <= y && y < h && 0 <= x && x < w);
}

void bfs() {
	typedef tuple<int, int, int> T;	//cost, y, x
	static queue<T> que;
	
	//全ての海を始点にする
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			if (s[i][j] == '.') {
				que.push(T(0, i, j));
				cost[i][j] = 0;
			}
		}
	}

	while (!que.empty()) {
		T now = que.front();
		que.pop();
		
		int cst = get<0>(now);
		int y = get<1>(now);
		int x = get<2>(now);
		
		for (int dir = 0; dir < 8; dir++) {
			int ny = y + dy[dir];
			int nx = x + dx[dir];
			if (is_range(ny, nx) && cost[ny][nx] > cst + 1) {
				que.push(T(cst + 1, ny, nx));
				cost[ny][nx] = cst + 1;
			}
		}
	}
}

int answer() {
	int ans = -1;
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			ans = max(ans, cost[i][j]);
		}
	}
	return ans;
}

int main() {
	input();
	bfs();
	cout << answer() << endl;
	return 0;
}
0