//Nguyen Huu Hoang Minh
#include <bits/stdc++.h>
#define sz(x) int(x.size())
#define all(x) x.begin(),x.end()
#define reset(x) memset(x, 0,sizeof(x))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define N 805
#define remain(x) if (x > MOD) x -= MOD
#define ii pair<int, int>
#define iiii pair< ii , ii >
#define viiii vector< iiii >
#define vi vector<int>
#define vii vector< ii >
#define bit(x, i) (((x) >> (i)) & 1)
#define Task "test"
#define int long long
using namespace std;
typedef long double ld;
const int inf = 1e10;
const int minf = -1e10;
int n, s;
char a[N][N];
ii M, D;
vector<ii> bee;
void readfile()
{
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
if (fopen(Task".inp","r"))
{
freopen(Task".inp","r",stdin);
//freopen(Task".out","w",stdout);
}
cin >> n >> s;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
cin >> a[i][j];
if (a[i][j]=='M') M = ii(i,j);
if (a[i][j]=='D') D = ii(i,j);
if (a[i][j]=='H') bee.pb(ii(i,j));
}
}
}
int d[N][N];
int dm[N][N];
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
bool ok(int x, int y){
if (1 <= x && x <= n && 1 <= y && y <= n) return true;
return false;
}
void build_bee(){
queue<ii> q;
memset(d, -1, sizeof d);
for(ii x : bee){
d[x.fi][x.se]=0;
q.push(ii(x.fi,x.se));
}
while (q.size()){
int u = q.front().fi;
int v = q.front().se;
//cout << u << ' ' << v << ' ' << d[u][v] << endl;
q.pop();
for(int i=0; i<4; i++){
int x = u + dx[i];
int y = v + dy[i];
if (d[x][y]==-1 && ok(x,y) && (a[x][y]=='G'||a[x][y]=='M')){
d[x][y] = d[u][v] + s;
q.push(ii(x,y));
}
}
}
d[D.fi][D.se] = 1e18;
}
bool can_eat(int ti){
if (ti*s >= d[M.fi][M.se]) return false;
queue<ii> q;
memset(dm, -1, sizeof dm);
dm[M.fi][M.se] = ti*s;
q.push(ii(M.fi,M.se));
while (q.size()){
int u = q.front().fi;
int v = q.front().se;
q.pop();
for(int i=0; i<4; i++){
int x = u + dx[i];
int y = v + dy[i];
if (!ok(x,y) || a[x][y]=='T' || a[x][y]=='H') continue;
if (dm[x][y]==-1){
dm[x][y] = dm[u][v] + 1;
if (dm[x][y] < d[x][y]){
q.push(ii(x,y));
}
}
}
}
return (dm[D.fi][D.se]!=-1);
}
void proc()
{
build_bee();
int l = 0, r = 1e9, ans=-1;
while (r-l > 0){
int mid = (l+r)/2;
if (can_eat(mid)){
l = mid+1;
ans = mid;
}
else r = mid;
}
cout << ans;
}
signed main()
{
readfile();
proc();
return 0;
}
Compilation message
mecho.cpp: In function 'void readfile()':
mecho.cpp:38:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | freopen(Task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
10444 KB |
Output is correct |
2 |
Correct |
6 ms |
10464 KB |
Output is correct |
3 |
Correct |
6 ms |
10464 KB |
Output is correct |
4 |
Correct |
6 ms |
10444 KB |
Output is correct |
5 |
Correct |
6 ms |
10444 KB |
Output is correct |
6 |
Correct |
7 ms |
10444 KB |
Output is correct |
7 |
Correct |
119 ms |
11248 KB |
Output is correct |
8 |
Correct |
6 ms |
10444 KB |
Output is correct |
9 |
Correct |
6 ms |
10444 KB |
Output is correct |
10 |
Correct |
7 ms |
10444 KB |
Output is correct |
11 |
Correct |
6 ms |
10444 KB |
Output is correct |
12 |
Correct |
7 ms |
10444 KB |
Output is correct |
13 |
Correct |
6 ms |
10444 KB |
Output is correct |
14 |
Correct |
8 ms |
10444 KB |
Output is correct |
15 |
Correct |
7 ms |
10364 KB |
Output is correct |
16 |
Correct |
7 ms |
10480 KB |
Output is correct |
17 |
Correct |
6 ms |
10444 KB |
Output is correct |
18 |
Correct |
6 ms |
10444 KB |
Output is correct |
19 |
Correct |
7 ms |
10444 KB |
Output is correct |
20 |
Correct |
7 ms |
10444 KB |
Output is correct |
21 |
Correct |
7 ms |
10444 KB |
Output is correct |
22 |
Correct |
6 ms |
10444 KB |
Output is correct |
23 |
Correct |
7 ms |
10440 KB |
Output is correct |
24 |
Correct |
6 ms |
10444 KB |
Output is correct |
25 |
Correct |
7 ms |
10516 KB |
Output is correct |
26 |
Correct |
7 ms |
10480 KB |
Output is correct |
27 |
Correct |
7 ms |
10444 KB |
Output is correct |
28 |
Correct |
7 ms |
10444 KB |
Output is correct |
29 |
Correct |
8 ms |
10444 KB |
Output is correct |
30 |
Correct |
8 ms |
10444 KB |
Output is correct |
31 |
Correct |
8 ms |
10444 KB |
Output is correct |
32 |
Correct |
7 ms |
10532 KB |
Output is correct |
33 |
Correct |
13 ms |
10700 KB |
Output is correct |
34 |
Correct |
13 ms |
10744 KB |
Output is correct |
35 |
Correct |
33 ms |
10748 KB |
Output is correct |
36 |
Correct |
13 ms |
10700 KB |
Output is correct |
37 |
Correct |
13 ms |
10708 KB |
Output is correct |
38 |
Correct |
42 ms |
10712 KB |
Output is correct |
39 |
Correct |
14 ms |
10700 KB |
Output is correct |
40 |
Correct |
14 ms |
10800 KB |
Output is correct |
41 |
Correct |
50 ms |
10824 KB |
Output is correct |
42 |
Correct |
14 ms |
10840 KB |
Output is correct |
43 |
Correct |
13 ms |
10848 KB |
Output is correct |
44 |
Correct |
66 ms |
10828 KB |
Output is correct |
45 |
Correct |
18 ms |
10828 KB |
Output is correct |
46 |
Correct |
18 ms |
10820 KB |
Output is correct |
47 |
Correct |
78 ms |
10828 KB |
Output is correct |
48 |
Correct |
20 ms |
10828 KB |
Output is correct |
49 |
Correct |
21 ms |
10936 KB |
Output is correct |
50 |
Correct |
94 ms |
10932 KB |
Output is correct |
51 |
Correct |
22 ms |
10976 KB |
Output is correct |
52 |
Correct |
24 ms |
10908 KB |
Output is correct |
53 |
Correct |
111 ms |
10920 KB |
Output is correct |
54 |
Correct |
26 ms |
10956 KB |
Output is correct |
55 |
Correct |
28 ms |
10936 KB |
Output is correct |
56 |
Correct |
138 ms |
11012 KB |
Output is correct |
57 |
Correct |
25 ms |
10944 KB |
Output is correct |
58 |
Correct |
23 ms |
10956 KB |
Output is correct |
59 |
Correct |
157 ms |
11060 KB |
Output is correct |
60 |
Correct |
27 ms |
11096 KB |
Output is correct |
61 |
Correct |
28 ms |
10980 KB |
Output is correct |
62 |
Correct |
175 ms |
11084 KB |
Output is correct |
63 |
Correct |
130 ms |
11084 KB |
Output is correct |
64 |
Correct |
197 ms |
11044 KB |
Output is correct |
65 |
Correct |
192 ms |
11076 KB |
Output is correct |
66 |
Correct |
143 ms |
11092 KB |
Output is correct |
67 |
Correct |
145 ms |
11084 KB |
Output is correct |
68 |
Correct |
64 ms |
11132 KB |
Output is correct |
69 |
Correct |
59 ms |
11148 KB |
Output is correct |
70 |
Correct |
53 ms |
11088 KB |
Output is correct |
71 |
Correct |
47 ms |
11080 KB |
Output is correct |
72 |
Correct |
43 ms |
11116 KB |
Output is correct |
73 |
Correct |
43 ms |
11624 KB |
Output is correct |
74 |
Correct |
84 ms |
11556 KB |
Output is correct |
75 |
Correct |
91 ms |
11600 KB |
Output is correct |
76 |
Correct |
79 ms |
11600 KB |
Output is correct |
77 |
Correct |
84 ms |
11588 KB |
Output is correct |
78 |
Correct |
101 ms |
11528 KB |
Output is correct |
79 |
Correct |
71 ms |
11552 KB |
Output is correct |
80 |
Correct |
76 ms |
11548 KB |
Output is correct |
81 |
Correct |
84 ms |
11592 KB |
Output is correct |
82 |
Correct |
71 ms |
11512 KB |
Output is correct |
83 |
Correct |
101 ms |
11536 KB |
Output is correct |
84 |
Correct |
97 ms |
11460 KB |
Output is correct |
85 |
Correct |
88 ms |
11456 KB |
Output is correct |
86 |
Correct |
99 ms |
11516 KB |
Output is correct |
87 |
Correct |
95 ms |
11464 KB |
Output is correct |
88 |
Correct |
104 ms |
11420 KB |
Output is correct |
89 |
Correct |
116 ms |
11424 KB |
Output is correct |
90 |
Correct |
109 ms |
11332 KB |
Output is correct |
91 |
Correct |
105 ms |
11364 KB |
Output is correct |
92 |
Correct |
107 ms |
11388 KB |
Output is correct |