Submission #733217

#TimeUsernameProblemLanguageResultExecution timeMemory
733217penguin133Mecho (IOI09_mecho)C++17
100 / 100
205 ms14924 KiB
#include <bits/stdc++.h>
using namespace std;
 
#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
 
queue <pi> q;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
char G[1005][1005];
int dist[1005][1005], dist2[1005][1005];
void solve(){
	int n, s; cin >> n >> s;
	for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)cin >> G[i][j];
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			dist[i][j] = 1e9;
			if(G[i][j] == 'H'){
				dist[i][j] = 0;
				q.push({i, j});
			}
		}
	}
	while(!q.empty()){
		int x = q.front().fi, y = q.front().se;
		q.pop();
		for(int i=0;i<4;i++){
			int x1 = x + dx[i], y1 = y + dy[i];
			if(x1 < 1 || x1 > n || y1 < 1 || y1 > n)continue;
			if(G[x1][y1] == 'T' || G[x1][y1] == 'D')continue;
			if(dist[x1][y1] > dist[x][y] + 1)dist[x1][y1] = dist[x][y] + 1, q.push({x1, y1});
		}
	}
	int s1, s2, e1, e2;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(G[i][j] == 'M')s1 = i, s2 = j;
			if(G[i][j] == 'D')e1 = i, e2 = j;
		}
	}
	int lo = 0, hi = (int)2e6, ans = -1;
	while(lo <= hi){
		int mid = (lo + hi) >> 1;
		for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)dist2[i][j] = 1e15;
		dist2[s1][s2] = mid * s;
      	if(dist2[s1][s2] >= s * dist[s1][s2]){
			hi = mid - 1;
          continue;
        }
		q.push({s1, s2});
		while(!q.empty()){
			int x = q.front().fi, y = q.front().se;
			q.pop();
			for(int i=0;i<4;i++){
				int x1 = x + dx[i], y1 = y + dy[i];
				if(x1 < 1 || x1 > n || y1 < 1 || y1 > n)continue;
				if(G[x1][y1] == 'T')continue;
				if(dist2[x1][y1] > dist2[x][y] + 1 && dist2[x][y] + 1 <= s * dist[x][y])dist2[x1][y1] = dist2[x][y] + 1, q.push({x1, y1});
			}
		}
		if(dist2[e1][e2] == 1e15)hi = mid - 1;
		else lo = mid + 1, ans = mid;
	}
	cout << ans;
}
 
main(){
	ios::sync_with_stdio(0);cin.tie(0);
	int tc = 1;
	//cin >> tc;
	for(int tc1=1;tc1<=tc;tc1++){
		// cout << "Case #" << tc1 << ": ";
		solve();
	}
}

Compilation message (stderr)

mecho.cpp:73:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   73 | main(){
      | ^~~~
mecho.cpp: In function 'void solve()':
mecho.cpp:52:43: warning: 's2' may be used uninitialized in this function [-Wmaybe-uninitialized]
   52 |        if(dist2[s1][s2] >= s * dist[s1][s2]){
      |                                ~~~~~~~~~~~^
mecho.cpp:52:43: warning: 's1' may be used uninitialized in this function [-Wmaybe-uninitialized]
mecho.cpp:67:18: warning: 'e2' may be used uninitialized in this function [-Wmaybe-uninitialized]
   67 |   if(dist2[e1][e2] == 1e15)hi = mid - 1;
      |      ~~~~~~~~~~~~^
mecho.cpp:67:18: warning: 'e1' may be used uninitialized in this function [-Wmaybe-uninitialized]
#Verdict Execution timeMemoryGrader output
Fetching results...