# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
442065 |
2021-07-07T01:19:40 Z |
dawangk |
Mecho (IOI09_mecho) |
C++14 |
|
1000 ms |
16552 KB |
#include <bits/stdc++.h>
using namespace std;
#define inputJunk ios_base::sync_with_stdio(0); cin.tie(0);
#define pb push_back
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define debug cout<<"HERE"<<endl;
#define ell "\n"
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef pair<pi, int> trip;
typedef pair<pll, ll> tripll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
typedef vector<trip> vtrip;
const int INF = 1e9+1212;
const ll P = 9973, M = 1e9+9;
const int MM = 8e2+5; int mod = 1e9+7;//998244353
int n, k, sx, sy, ex, ey, dist[MM][MM]; char grid[MM][MM];
queue<pi> q; pi dir[4] {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
bool v[MM][MM];
bool vv(int a){
return a>=0&&a<n;
}
bool valid(int x, int y){
return vv(x)&&vv(y)&&(grid[x][y]=='G'||grid[x][y]=='M')&dist[x][y]==-1;
}
bool dfs(int x, int y, int t, int used){
//for(int i= 0;i<n;i++){
// for(int j = 0;j<n;j++){
// cout<<v[i][j]<<" ";
// }cout<<ell;
//}cout<<t<<" "<<used<<ell;
int newt = (used+1==k)?t+1:t;
int newused = (used+1)%k;
if(x==ex&&y==ey)return true;
for(int i = 0;i<4;i++){
int newx = x+dir[i].f, newy = y+dir[i].s;
//cout<<newx<<" "<<newy<<endl;
if(vv(newx)&&vv(newy)&&(grid[newx][newy]=='G'||grid[newx][newy]=='D')&&newt<dist[newx][newy]&&!v[newx][newy]){
v[newx][newy] = true;
if(dfs(newx, newy, newt, newused))return true;
v[newx][newy] = false;
}
}
return false;
}
bool dfss(int t){
for(int i = 0;i<n;i++){
for(int j = 0;j<n;j++){
v[i][j] = false;
}
}
v[sx][sy] = true;
return dfs(sx, sy, t-1, k-1);
}
int main(){
inputJunk
cin>>n>>k;
for(int i = 0;i<n;i++){
for(int j = 0;j<n;j++){
cin>>grid[i][j];
dist[i][j] = -1;
if(grid[i][j]=='M'){sx = i; sy = j;}
else if(grid[i][j]=='D'){ex = i;ey = j;}
else if(grid[i][j]=='H'){q.push({i, j}); dist[i][j] = 0;}
}
}
while(!q.empty()){
int curx = q.front().f, cury = q.front().s;q.pop();
//cout<<curx<<" "<<cury<<" "<<endl;
for(int i = 0;i<4;i++){
int newx = curx+dir[i].f, newy = cury+dir[i].s;
if(valid(newx, newy)){
dist[newx][newy] = dist[curx][cury]+1;
q.push({newx, newy});
}
}
}
/*
for(int i = 0;i<n;i++){
for(int j = 0;j<n;j++){
cout<<dist[i][j]<<" ";
}cout<<ell;
}*/
dist[ex][ey] = INF;
int lo = 0, hi = dist[sx][sy];
if(!dfss(0)){
cout<<-1<<endl;
return 0;
}
while(lo<hi){
//cout<<lo<<" "<<hi<<endl;
int mid = (lo+hi+1)/2;
if(dfss(mid)){
lo = mid;
}else{
hi = mid-1;
}
}
cout<<lo<<endl;
}
/*CUSTOM TEST CASE 1
*/
/*CUSTOM TEST CASE 2
*/
/*CUSTOM TEST CASE 3
*/
/*Comments of Shame
- floating error (use integer division instead)
- cin vs getline
- upperbound and lowerbound returns iteratorsf
- use long long when number is big enough
- for dp invalid cases needs to be skipped
- some base cases won't work (review cow poetry)
- always check bounds, some TLE are due to incorrect bounds!
- dont mess up return types
= RESET ARRAYS!!!!!!!!!!
- USE UR BRAIN
- INF setting problems
*/
/*
freopen("time.in","r", stdin);
freopen("time.out","w", stdout);
*/
Compilation message
mecho.cpp: In function 'bool valid(int, int)':
mecho.cpp:36:71: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
36 | return vv(x)&&vv(y)&&(grid[x][y]=='G'||grid[x][y]=='M')&dist[x][y]==-1;
| ~~~~~~~~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Execution timed out |
1094 ms |
4612 KB |
Time limit exceeded |
8 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
13 |
Execution timed out |
1087 ms |
460 KB |
Time limit exceeded |
14 |
Execution timed out |
1089 ms |
588 KB |
Time limit exceeded |
15 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
25 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
26 |
Correct |
1 ms |
588 KB |
Output is correct |
27 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
28 |
Correct |
1 ms |
588 KB |
Output is correct |
29 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
30 |
Correct |
1 ms |
716 KB |
Output is correct |
31 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
32 |
Correct |
1 ms |
716 KB |
Output is correct |
33 |
Incorrect |
5 ms |
1996 KB |
Output isn't correct |
34 |
Correct |
8 ms |
4300 KB |
Output is correct |
35 |
Execution timed out |
1089 ms |
3660 KB |
Time limit exceeded |
36 |
Incorrect |
7 ms |
2252 KB |
Output isn't correct |
37 |
Correct |
9 ms |
5248 KB |
Output is correct |
38 |
Execution timed out |
1098 ms |
4300 KB |
Time limit exceeded |
39 |
Incorrect |
8 ms |
2508 KB |
Output isn't correct |
40 |
Correct |
13 ms |
6256 KB |
Output is correct |
41 |
Execution timed out |
1091 ms |
5176 KB |
Time limit exceeded |
42 |
Incorrect |
10 ms |
2680 KB |
Output isn't correct |
43 |
Correct |
14 ms |
7548 KB |
Output is correct |
44 |
Execution timed out |
1078 ms |
6020 KB |
Time limit exceeded |
45 |
Incorrect |
12 ms |
2892 KB |
Output isn't correct |
46 |
Correct |
19 ms |
8760 KB |
Output is correct |
47 |
Execution timed out |
1092 ms |
6980 KB |
Time limit exceeded |
48 |
Incorrect |
14 ms |
3176 KB |
Output isn't correct |
49 |
Correct |
20 ms |
10124 KB |
Output is correct |
50 |
Execution timed out |
1091 ms |
7952 KB |
Time limit exceeded |
51 |
Incorrect |
16 ms |
3404 KB |
Output isn't correct |
52 |
Correct |
28 ms |
11596 KB |
Output is correct |
53 |
Execution timed out |
1089 ms |
9044 KB |
Time limit exceeded |
54 |
Incorrect |
18 ms |
3660 KB |
Output isn't correct |
55 |
Correct |
28 ms |
13096 KB |
Output is correct |
56 |
Execution timed out |
1086 ms |
10132 KB |
Time limit exceeded |
57 |
Incorrect |
21 ms |
3916 KB |
Output isn't correct |
58 |
Correct |
33 ms |
14776 KB |
Output is correct |
59 |
Execution timed out |
1093 ms |
11340 KB |
Time limit exceeded |
60 |
Incorrect |
23 ms |
4108 KB |
Output isn't correct |
61 |
Correct |
36 ms |
16552 KB |
Output is correct |
62 |
Execution timed out |
1082 ms |
12564 KB |
Time limit exceeded |
63 |
Correct |
85 ms |
4292 KB |
Output is correct |
64 |
Correct |
88 ms |
4236 KB |
Output is correct |
65 |
Correct |
98 ms |
4368 KB |
Output is correct |
66 |
Incorrect |
81 ms |
4344 KB |
Output isn't correct |
67 |
Correct |
37 ms |
4244 KB |
Output is correct |
68 |
Correct |
49 ms |
4328 KB |
Output is correct |
69 |
Correct |
40 ms |
4328 KB |
Output is correct |
70 |
Correct |
40 ms |
4312 KB |
Output is correct |
71 |
Correct |
38 ms |
4276 KB |
Output is correct |
72 |
Incorrect |
33 ms |
4140 KB |
Output isn't correct |
73 |
Execution timed out |
1094 ms |
4628 KB |
Time limit exceeded |
74 |
Execution timed out |
1094 ms |
6564 KB |
Time limit exceeded |
75 |
Execution timed out |
1078 ms |
4676 KB |
Time limit exceeded |
76 |
Execution timed out |
1087 ms |
4524 KB |
Time limit exceeded |
77 |
Execution timed out |
1078 ms |
4956 KB |
Time limit exceeded |
78 |
Execution timed out |
1090 ms |
4548 KB |
Time limit exceeded |
79 |
Execution timed out |
1094 ms |
4676 KB |
Time limit exceeded |
80 |
Execution timed out |
1095 ms |
4420 KB |
Time limit exceeded |
81 |
Execution timed out |
1093 ms |
4608 KB |
Time limit exceeded |
82 |
Execution timed out |
1097 ms |
4624 KB |
Time limit exceeded |
83 |
Execution timed out |
1093 ms |
4320 KB |
Time limit exceeded |
84 |
Execution timed out |
1095 ms |
4292 KB |
Time limit exceeded |
85 |
Execution timed out |
1084 ms |
4468 KB |
Time limit exceeded |
86 |
Execution timed out |
1092 ms |
4420 KB |
Time limit exceeded |
87 |
Execution timed out |
1087 ms |
4388 KB |
Time limit exceeded |
88 |
Execution timed out |
1092 ms |
4272 KB |
Time limit exceeded |
89 |
Execution timed out |
1098 ms |
4420 KB |
Time limit exceeded |
90 |
Execution timed out |
1070 ms |
4256 KB |
Time limit exceeded |
91 |
Execution timed out |
1082 ms |
4292 KB |
Time limit exceeded |
92 |
Execution timed out |
1069 ms |
4292 KB |
Time limit exceeded |