Submission #784324

#TimeUsernameProblemLanguageResultExecution timeMemory
784324Anthony_LiuMecho (IOI09_mecho)C++11
100 / 100
832 ms6696 KiB
#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("O3, unroll-loops")
//#pragma GCC target("avx2, bmi, bmi2, lzcnt, popcnt")
#define f first
#define s second
#define ll long long
#define pb push_back 
#define pi pair <int,int>
#define vi vector <int>
#define size(x) (int)(x).size()
#define all(x) x.begin(), x.end()
void setIO(string name = "") {
	cin.tie(0)->sync_with_stdio(0);
	if (size(name)) {
		freopen((name + ".in").c_str(), "r", stdin);
		freopen((name + ".out").c_str(), "w", stdout);
	}
}

int N, S;
pi mecho;
pi home;
	
char grid[801][801];
vector <pi> hives;

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

bool valid(pi v) {
	if (v.f < 0 || v.s < 0 || v.f >= N || v.s >= N) return false;
	if (grid[v.f][v.s] == 'T' || grid[v.f][v.s] == 'D' || grid[v.f][v.s] == 'H') return false;
	return true;
}

bool calc(int eat) {
	
	queue <pi> q;
	vector <vi> dist1(801, vi(801, INT_MAX)); //dist from hive
	vector <vi> dist2(801, vi(801, INT_MAX)); //dist from mecho
	
	//calc hive dist
	for (auto i : hives) {
		q.push(i);
		dist1[i.f][i.s] = 0;
	}
	
	while (!q.empty()) {
		pi u = q.front();
		q.pop();
		
		for (int i = 0; i < 4; i++) {
			pi v = {u.f + dx[i], u.s + dy[i]};
			if (!valid(v)) continue;
			
			if (dist1[v.f][v.s] > dist1[u.f][u.s] + 1) {
				dist1[v.f][v.s] = dist1[u.f][u.s] + 1;
				q.push(v);
			}
		}
	}
	
	//calc mecho dist
	q.push(mecho);
	dist2[mecho.f][mecho.s] = 0;
	if (dist1[mecho.f][mecho.s] <= eat) return false;
	
	while (!q.empty()) {
		pi u = q.front();
		q.pop();
		
		for (int i = 0; i < 4; i++) {
			pi v = {u.f + dx[i], u.s + dy[i]};
			if (!valid(v)) continue;
			
			if (dist2[v.f][v.s] > dist2[u.f][u.s] + 1) {
				int md = dist2[u.f][u.s] + 1;
				int bd = dist1[v.f][v.s] - eat;
				if (md / S >= bd) continue;
				
				dist2[v.f][v.s] = dist2[u.f][u.s] + 1;
				q.push(v);
			}
		}
	}
	
	for (int i = 0; i < 4; i++) 
	{
		int nx = home.f + dx[i], ny = home.s + dy[i];
		if (!valid({nx, ny})) continue;
		
		int md = dist2[nx][ny];
		int bd = dist1[nx][ny] - eat;
		if (md / S >= bd) continue;
		
		if (dist2[nx][ny] != INT_MAX) 
			return true;
	}
	return false;
}

int main()
{
	setIO();
	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') mecho = {i, j};
			else if (grid[i][j] == 'D') home = {i, j};
			else if (grid[i][j] == 'H') {
				hives.pb({i, j});
			}
		}
	}
		
	int l = 0, r = N * N;
	int ans = -1;
	
	while (l <= r) {
		int m = l + (r - l) / 2;
		if (calc(m)) {
			ans = m;
			l = m + 1;
		}
		else r = m - 1;
	}
	
	cout << ans << '\n'; 
	return 0;
}

Compilation message (stderr)

mecho.cpp: In function 'void setIO(std::string)':
mecho.cpp:16:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |   freopen((name + ".in").c_str(), "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:17:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |   freopen((name + ".out").c_str(), "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...