제출 #742730

#제출 시각아이디문제언어결과실행 시간메모리
742730CSQ31Mecho (IOI09_mecho)C++17
38 / 100
464 ms11536 KiB
#pragma GCC optimize("Ofast") 
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
int n,s,dx[4] = {0,0,1,-1},dy[4] = {1,-1,0,0};
int dist[1000][1000],bdist[1000][1000],blk[1000][1000];
char grid[1000][1000];
pii st,ed;
bool check(int tim){
	queue<pii>bee;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			dist[i][j] = bdist[i][j] = 1e9;
			blk[i][j] = 1;
			if(grid[i][j] == 'H'){
				bee.push({i,j});
				bdist[i][j] = 0;
			}
			if(grid[i][j] != 'H' && grid[i][j] != 'T')blk[i][j] = 0;
		}
	}
	while(!bee.empty()){
		auto v = bee.front();
		if(bdist[v.fi][v.se] == tim+1)break;
		bee.pop();
		blk[v.fi][v.se] = 1;
		for(int i=0;i<4;i++){
			int x = v.fi+dx[i];
			int y = v.se+dy[i];
			if(x>=0 && y>=0 && x<n && y<n && bdist[x][y] == 1e9 && grid[x][y] == 'G'){
				bee.push({x,y});
				bdist[x][y] = bdist[v.fi][v.se]+1;
			}
		}
	}
	
	queue<pii>q;
	q.push(st);
	dist[st.fi][st.se] = 0;
	int cnt = 1;
	while(!q.empty()){
		pii v = q.front();
		q.pop();
		if((dist[v.fi][v.se]+s-1)/s > cnt){ //move the bees
			cnt++;
			tim++;
			while(!bee.empty()){
				auto v = bee.front();
				if(bdist[v.fi][v.se] == tim+1)break;
				bee.pop();
				blk[v.fi][v.se] = 1;
				for(int i=0;i<4;i++){
					int x = v.fi+dx[i];
					int y = v.se+dy[i];
					if(x>=0 && y>=0 && x<n && y<n && bdist[x][y] == 1e9 && grid[x][y] == 'G'){
						bee.push({x,y});
						bdist[x][y] = bdist[v.fi][v.se]+1;
					}
				}
			}
		}
		if(blk[v.fi][v.se])continue;
		for(int i=0;i<4;i++){
            int x = v.fi+dx[i];
			int y = v.se+dy[i];
			if(x>=0 && y>=0 && x<n && y<n && !blk[x][y] && dist[x][y] == 1e9){
				q.push({x,y});
				dist[x][y] = dist[v.fi][v.se]+1;
			}
		}
	}
	
	return dist[ed.fi][ed.se] != 1e9;
}
int main()
{
	owo
	cin>>n>>s;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin>>grid[i][j];	
			if(grid[i][j] == 'M')st = {i,j};
			if(grid[i][j] == 'D')ed = {i,j};
		}
	}
	if(!check(0)){
		cout<<-1;
		return 0;
	}
	int l = 0,r = n*n;
	while(r>=l){
		int mid = (l+r)/2;
		if(check(mid))l = mid+1;
		else r = mid-1;
	}
	cout<<r<<'\n';
	
}
#Verdict Execution timeMemoryGrader output
Fetching results...