結果

問題 No.402 最も海から遠い場所
ユーザー IL_mstaIL_msta
提出日時 2017-01-28 12:56:23
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 3,060 bytes
コンパイル時間 1,392 ms
コンパイル使用メモリ 115,252 KB
実行使用メモリ 150,516 KB
最終ジャッジ日時 2023-08-25 13:06:58
合計ジャッジ時間 7,622 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
11,560 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 6 ms
5,116 KB
testcase_16 AC 13 ms
9,992 KB
testcase_17 AC 94 ms
56,224 KB
testcase_18 AC 1,043 ms
150,516 KB
testcase_19 AC 35 ms
4,796 KB
testcase_20 TLE -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifdef __GNUC__
#pragma GCC optimize ("O3")
#pragma GCC target ("avx")
#endif

#define _USE_MATH_DEFINES

#include <iostream>
#include <iomanip>
#include <stdio.h>

#include <sstream>
#include <algorithm>
#include <cmath>

#include <string>
#include <cstring>
#include <vector>
#include <valarray>
#include <array>

#include <queue>
#include <complex>
#include <set>
#include <map>
#include <stack>
#include <list>

#include <cassert>//assert();
#include <fstream>
/////////
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)

/////////
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
#define PII pair<int,int>
/////////
using namespace::std;

// 最大公約数
template<class T>
inline T gcd(T a, T b){return b == 0 ? a : gcd(b, a % b);}
// 最小公倍数
template<class T>
inline T lcm(T a, T b){return a * b / gcd(a, b);}
////////////////////////////////
struct data{
	int x;
	int y;
	int turn;
	int kind;//0 kNi, 1 bisyo
};
inline void solve(){
	int H,W;
	cin >> H>>W;
	scanf("%d%d",&H,&W);
	vector<vector<bool>> rikuFlag(H,vector<bool>(W,false) );
	vector<pair<int,int>> riku;
	vector<pair<int,int>> next;
	vector<pair<int,int>> mizu;
	riku.reserve(H*W);
	next.reserve(H*W);
	mizu.reserve(2*H+2*W-4);
	pair<int,int> temp;
	char str[3001];
	for(int i=0;i<H;++i){
		scanf(" %s",str);
		for(int j=0;j<W;++j){
			if( str[j] == '#' ){
				rikuFlag[i][j] = true;
				temp.first = i;
				temp.second = j;
				riku.push_back( temp );
			}
		}
	}
	//1回目
	int rikuSize = riku.size();
	const int dSize = 8;
	int dh[] ={-1,-1,-1, 0,0, 1,1,1};
	int dw[] ={-1, 0,+1,-1,1,-1,0,1};
	for(int i=0;i<rikuSize;++i){
		temp = riku[i];
		if( temp.first-1 < 0 || H <= temp.first+1 ||
			temp.second-1<0 || W <= temp.second+1 ){
			//外は海
			mizu.push_back( temp );
		}else{
			int dn;
			for(dn=0;dn<dSize;++dn){
				if( rikuFlag[ temp.first+dh[dn] ][ temp.second+dw[dn] ] == false ){
					mizu.push_back( temp );
					break;
				}
			}
			if( dn == dSize ){
				next.push_back( temp );
			}
		}
	}
	if( riku.size() == mizu.size() ){
		printf("1");return;
	}
	int mizuSize = mizu.size();
	for(int i=0;i<mizuSize;++i){
		rikuFlag[mizu[i].first][mizu[i].second] = false;
	}
	riku = next;
	next.resize(0);
	mizu.resize(0);

	int turn = 1;
	int dn;
	for(;;){
		++turn;
		rikuSize = riku.size();
		for(int i=0;i<rikuSize;++i){
			temp = riku[i];
			for(dn=0;dn<dSize;++dn){
				if( rikuFlag[ temp.first+dh[dn] ][ temp.second+dw[dn] ] == false ){
					mizu.push_back( temp );
					break;
				}
			}
			if( dn == dSize ){
				next.push_back( temp );
			}
		}
		if( riku.size() == mizu.size() ){
			printf("%d",turn);return;
		}
		mizuSize = mizu.size();
		for(int i=0;i<mizuSize;++i){
			rikuFlag[mizu[i].first][mizu[i].second] = false;
		}
		riku = next;
		next.resize(0);
		mizu.resize(0);
	}
}

int main(void){
	//std::cin.tie(0);
	//std::ios::sync_with_stdio(false);
	//std::cout << std::fixed;//小数を10進数表示
	//cout << setprecision(16);//小数をいっぱい表示する。16?

	solve();

	return 0;
}
0