# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
499748 |
2021-12-29T11:56:35 Z |
tphuc2908 |
Mecho (IOI09_mecho) |
C++14 |
|
202 ms |
9524 KB |
#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
#define rep(i, x, y) for(int i = x; i <= y; ++i)
#define repi(i,x,y) for(int i = x; i >= y; --i)
#define ci(x) int x; cin>> x
#define TC(t) ci(t); while(t--)
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define cii(x, y) ci(x); ci(y)
#define ciii(x, y, z) ci(x); ci(y); ci(z)
#define mp make_pair
//#define int long long
typedef long long ll;
typedef vector<int> vi;
const int N = 8e2 + 5;
const int mod = 1e9 + 7;
const int mod1 = 1e9 + 9;
const int pi = 31;
const int inf = 1e9 + 5;
const int block = 330;
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
void readfile(){
#ifdef ONLINE_JUDGE
#else
freopen("text.inp", "r", stdin);
#endif // ONLINE_JUDGE
// freopen("gravity.in", "r", stdin);
// freopen("gravity.out", "w", stdout);
}
int n, s;
char a[N][N];
pair<int,int> st, e;
void inp(){
cin >> n >> s;
rep(i,1,n)
rep(j,1,n){
cin >> a[i][j];
if(a[i][j]=='M')
st = mp(i, j);
if(a[i][j]=='D')
e = mp(i, j);
}
}
int d[N][N], d1[N][N];
void bfs_multi_source(){
queue<pair<int,int> > q;
rep(i,1,n)
rep(j,1,n){
d1[i][j] = inf;
if(a[i][j]=='H')
q.push(mp(i ,j)), d1[i][j] = 0;
}
while(!q.empty()){
pair<int,int> top = q.front();
q.pop();
rep(i,0,3){
int x = top.fi + dx[i];
int y = top.se + dy[i];
if(1 <= x && x <= n && 1 <= y && y <= n && a[x][y]!='T' && a[x][y]!='D' && d1[x][y] > d1[top.fi][top.se] + 1){
d1[x][y] = d1[top.fi][top.se] + 1;
q.push(mp(x,y));
}
}
}
}
int vis[N][N];
void bfs(int x, int y, int ti){
rep(i,1,n)
rep(j,1,n)
d[i][j] = inf, vis[i][j] = 0;
queue<pair<int,int> > q;
q.push(mp(x, y));
d[x][y] = ti;
if(ti >= d1[x][y]) return;
while(!q.empty()){
auto top = q.front();
q.pop();
vis[top.fi][top.se] = 1;
rep(i,0,3){
int x = top.fi + dx[i];
int y = top.se + dy[i];
if(1 <= x && x <= n && 1 <= y && y <= n && a[x][y]!='T' && d[x][y] > d[top.fi][top.se] + 1){
d[x][y] = d[top.fi][top.se] + 1;
int diff = d[x][y] - ti - 1;
diff /= s;
diff += ti + 1;
if(diff < d1[x][y] || (diff==d1[x][y] && d[x][y] - d[top.fi][top.se] < s))
q.push(mp(x, y));
}
}
}
}
void process(){
bfs_multi_source();
int l = 0, r = n*n + 1, res = -1;
while(l <= r){
int mid = (l + r) >> 1;
bfs(st.fi, st.se, mid);
if(!vis[e.fi][e.se])
r = mid - 1;
else{
res = mid;
l = mid + 1;
}
}
cout << res;
}
int main() {
// readfile();
ios_base::sync_with_stdio(0);
cin.tie(0);
// TC(t){
inp();
process();
// }
return 0;
}
Compilation message
mecho.cpp: In function 'void readfile()':
mecho.cpp:33:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen("text.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
320 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
324 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
324 KB |
Output is correct |
7 |
Correct |
86 ms |
9256 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
0 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
960 KB |
Output is correct |
13 |
Correct |
1 ms |
844 KB |
Output is correct |
14 |
Correct |
1 ms |
940 KB |
Output is correct |
15 |
Correct |
0 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
0 ms |
576 KB |
Output is correct |
19 |
Correct |
1 ms |
572 KB |
Output is correct |
20 |
Correct |
1 ms |
588 KB |
Output is correct |
21 |
Correct |
1 ms |
716 KB |
Output is correct |
22 |
Correct |
1 ms |
716 KB |
Output is correct |
23 |
Correct |
1 ms |
844 KB |
Output is correct |
24 |
Correct |
1 ms |
844 KB |
Output is correct |
25 |
Correct |
1 ms |
844 KB |
Output is correct |
26 |
Correct |
1 ms |
844 KB |
Output is correct |
27 |
Correct |
1 ms |
972 KB |
Output is correct |
28 |
Correct |
1 ms |
960 KB |
Output is correct |
29 |
Correct |
1 ms |
972 KB |
Output is correct |
30 |
Correct |
1 ms |
972 KB |
Output is correct |
31 |
Correct |
1 ms |
1100 KB |
Output is correct |
32 |
Correct |
1 ms |
1100 KB |
Output is correct |
33 |
Correct |
8 ms |
4040 KB |
Output is correct |
34 |
Correct |
7 ms |
3916 KB |
Output is correct |
35 |
Correct |
23 ms |
4036 KB |
Output is correct |
36 |
Correct |
8 ms |
4548 KB |
Output is correct |
37 |
Correct |
8 ms |
4520 KB |
Output is correct |
38 |
Correct |
32 ms |
4556 KB |
Output is correct |
39 |
Correct |
10 ms |
5072 KB |
Output is correct |
40 |
Correct |
10 ms |
5048 KB |
Output is correct |
41 |
Correct |
40 ms |
5128 KB |
Output is correct |
42 |
Correct |
12 ms |
5580 KB |
Output is correct |
43 |
Correct |
12 ms |
5580 KB |
Output is correct |
44 |
Correct |
46 ms |
5620 KB |
Output is correct |
45 |
Correct |
17 ms |
6208 KB |
Output is correct |
46 |
Correct |
20 ms |
6236 KB |
Output is correct |
47 |
Correct |
59 ms |
6220 KB |
Output is correct |
48 |
Correct |
16 ms |
6732 KB |
Output is correct |
49 |
Correct |
17 ms |
6756 KB |
Output is correct |
50 |
Correct |
59 ms |
6712 KB |
Output is correct |
51 |
Correct |
20 ms |
7364 KB |
Output is correct |
52 |
Correct |
21 ms |
7372 KB |
Output is correct |
53 |
Correct |
115 ms |
7400 KB |
Output is correct |
54 |
Correct |
29 ms |
7876 KB |
Output is correct |
55 |
Correct |
28 ms |
7872 KB |
Output is correct |
56 |
Correct |
90 ms |
7976 KB |
Output is correct |
57 |
Correct |
26 ms |
8516 KB |
Output is correct |
58 |
Correct |
31 ms |
8544 KB |
Output is correct |
59 |
Correct |
155 ms |
8476 KB |
Output is correct |
60 |
Correct |
39 ms |
9032 KB |
Output is correct |
61 |
Correct |
31 ms |
9028 KB |
Output is correct |
62 |
Correct |
117 ms |
9140 KB |
Output is correct |
63 |
Correct |
101 ms |
9028 KB |
Output is correct |
64 |
Correct |
202 ms |
9140 KB |
Output is correct |
65 |
Correct |
137 ms |
9112 KB |
Output is correct |
66 |
Incorrect |
131 ms |
9144 KB |
Output isn't correct |
67 |
Correct |
112 ms |
9092 KB |
Output is correct |
68 |
Correct |
51 ms |
9096 KB |
Output is correct |
69 |
Correct |
49 ms |
9156 KB |
Output is correct |
70 |
Correct |
40 ms |
9156 KB |
Output is correct |
71 |
Correct |
45 ms |
9108 KB |
Output is correct |
72 |
Correct |
37 ms |
9160 KB |
Output is correct |
73 |
Correct |
45 ms |
9416 KB |
Output is correct |
74 |
Correct |
65 ms |
9524 KB |
Output is correct |
75 |
Correct |
75 ms |
9416 KB |
Output is correct |
76 |
Correct |
86 ms |
9368 KB |
Output is correct |
77 |
Correct |
74 ms |
9372 KB |
Output is correct |
78 |
Correct |
76 ms |
9328 KB |
Output is correct |
79 |
Correct |
53 ms |
9480 KB |
Output is correct |
80 |
Correct |
76 ms |
9392 KB |
Output is correct |
81 |
Correct |
77 ms |
9364 KB |
Output is correct |
82 |
Correct |
63 ms |
9336 KB |
Output is correct |
83 |
Correct |
78 ms |
9284 KB |
Output is correct |
84 |
Correct |
75 ms |
9276 KB |
Output is correct |
85 |
Correct |
106 ms |
9348 KB |
Output is correct |
86 |
Correct |
83 ms |
9344 KB |
Output is correct |
87 |
Correct |
73 ms |
9276 KB |
Output is correct |
88 |
Correct |
82 ms |
9284 KB |
Output is correct |
89 |
Correct |
80 ms |
9284 KB |
Output is correct |
90 |
Correct |
86 ms |
9284 KB |
Output is correct |
91 |
Correct |
80 ms |
9288 KB |
Output is correct |
92 |
Correct |
125 ms |
9240 KB |
Output is correct |