Submission #733217

# Submission time Handle Problem Language Result Execution time Memory
733217 2023-04-30T09:14:16 Z penguin133 Mecho (IOI09_mecho) C++17
100 / 100
205 ms 14924 KB
#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

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 time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 97 ms 14540 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 456 KB Output is correct
12 Correct 1 ms 852 KB Output is correct
13 Correct 1 ms 852 KB Output is correct
14 Correct 1 ms 844 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
20 Correct 1 ms 596 KB Output is correct
21 Correct 1 ms 724 KB Output is correct
22 Correct 1 ms 724 KB Output is correct
23 Correct 1 ms 724 KB Output is correct
24 Correct 1 ms 712 KB Output is correct
25 Correct 2 ms 852 KB Output is correct
26 Correct 1 ms 852 KB Output is correct
27 Correct 1 ms 980 KB Output is correct
28 Correct 1 ms 980 KB Output is correct
29 Correct 1 ms 980 KB Output is correct
30 Correct 1 ms 980 KB Output is correct
31 Correct 1 ms 1108 KB Output is correct
32 Correct 1 ms 1108 KB Output is correct
33 Correct 7 ms 5464 KB Output is correct
34 Correct 8 ms 5484 KB Output is correct
35 Correct 20 ms 5460 KB Output is correct
36 Correct 8 ms 6516 KB Output is correct
37 Correct 9 ms 6616 KB Output is correct
38 Correct 27 ms 6612 KB Output is correct
39 Correct 11 ms 7636 KB Output is correct
40 Correct 11 ms 7636 KB Output is correct
41 Correct 33 ms 7636 KB Output is correct
42 Correct 13 ms 9000 KB Output is correct
43 Correct 13 ms 8880 KB Output is correct
44 Correct 47 ms 8916 KB Output is correct
45 Correct 20 ms 9812 KB Output is correct
46 Correct 14 ms 9812 KB Output is correct
47 Correct 60 ms 9824 KB Output is correct
48 Correct 16 ms 10708 KB Output is correct
49 Correct 18 ms 10616 KB Output is correct
50 Correct 71 ms 10620 KB Output is correct
51 Correct 19 ms 11528 KB Output is correct
52 Correct 20 ms 11624 KB Output is correct
53 Correct 108 ms 11620 KB Output is correct
54 Correct 24 ms 12500 KB Output is correct
55 Correct 24 ms 12380 KB Output is correct
56 Correct 89 ms 12496 KB Output is correct
57 Correct 23 ms 13388 KB Output is correct
58 Correct 22 ms 13384 KB Output is correct
59 Correct 135 ms 13412 KB Output is correct
60 Correct 30 ms 14268 KB Output is correct
61 Correct 31 ms 14300 KB Output is correct
62 Correct 130 ms 14344 KB Output is correct
63 Correct 124 ms 14332 KB Output is correct
64 Correct 201 ms 14448 KB Output is correct
65 Correct 205 ms 14324 KB Output is correct
66 Correct 141 ms 14332 KB Output is correct
67 Correct 111 ms 14328 KB Output is correct
68 Correct 59 ms 14336 KB Output is correct
69 Correct 60 ms 14284 KB Output is correct
70 Correct 52 ms 14268 KB Output is correct
71 Correct 57 ms 14368 KB Output is correct
72 Correct 44 ms 14364 KB Output is correct
73 Correct 49 ms 14884 KB Output is correct
74 Correct 80 ms 14836 KB Output is correct
75 Correct 76 ms 14864 KB Output is correct
76 Correct 68 ms 14924 KB Output is correct
77 Correct 74 ms 14864 KB Output is correct
78 Correct 81 ms 14708 KB Output is correct
79 Correct 61 ms 14812 KB Output is correct
80 Correct 67 ms 14860 KB Output is correct
81 Correct 76 ms 14828 KB Output is correct
82 Correct 59 ms 14820 KB Output is correct
83 Correct 96 ms 14668 KB Output is correct
84 Correct 81 ms 14748 KB Output is correct
85 Correct 78 ms 14668 KB Output is correct
86 Correct 92 ms 14720 KB Output is correct
87 Correct 101 ms 14748 KB Output is correct
88 Correct 97 ms 14636 KB Output is correct
89 Correct 88 ms 14644 KB Output is correct
90 Correct 105 ms 14644 KB Output is correct
91 Correct 89 ms 14648 KB Output is correct
92 Correct 106 ms 14776 KB Output is correct