# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
442068 |
2021-07-07T01:32:07 Z |
dawangk |
Mecho (IOI09_mecho) |
C++14 |
|
1000 ms |
16572 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;
//cout<<x<<" "<<y<<" "<<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, 0);
}
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 = 100000;
if(!dfss(0)){
cout<<-1<<endl;
return 0;
}
//cout<<"BOB"<<dfss(2)<<ell;
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 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Execution timed out |
1085 ms |
4596 KB |
Time limit exceeded |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
13 |
Execution timed out |
1091 ms |
460 KB |
Time limit exceeded |
14 |
Execution timed out |
1094 ms |
588 KB |
Time limit exceeded |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
0 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
25 |
Correct |
1 ms |
588 KB |
Output is correct |
26 |
Correct |
1 ms |
588 KB |
Output is correct |
27 |
Correct |
1 ms |
588 KB |
Output is correct |
28 |
Correct |
1 ms |
716 KB |
Output is correct |
29 |
Correct |
1 ms |
588 KB |
Output is correct |
30 |
Correct |
1 ms |
716 KB |
Output is correct |
31 |
Correct |
1 ms |
588 KB |
Output is correct |
32 |
Correct |
1 ms |
716 KB |
Output is correct |
33 |
Correct |
6 ms |
1996 KB |
Output is correct |
34 |
Correct |
7 ms |
4296 KB |
Output is correct |
35 |
Execution timed out |
1085 ms |
3660 KB |
Time limit exceeded |
36 |
Correct |
6 ms |
2252 KB |
Output is correct |
37 |
Correct |
9 ms |
5232 KB |
Output is correct |
38 |
Execution timed out |
1095 ms |
2252 KB |
Time limit exceeded |
39 |
Incorrect |
8 ms |
2508 KB |
Output isn't correct |
40 |
Correct |
12 ms |
6380 KB |
Output is correct |
41 |
Execution timed out |
1093 ms |
3916 KB |
Time limit exceeded |
42 |
Incorrect |
10 ms |
2764 KB |
Output isn't correct |
43 |
Correct |
14 ms |
7500 KB |
Output is correct |
44 |
Execution timed out |
1086 ms |
3968 KB |
Time limit exceeded |
45 |
Incorrect |
12 ms |
3000 KB |
Output isn't correct |
46 |
Correct |
21 ms |
8780 KB |
Output is correct |
47 |
Execution timed out |
1096 ms |
3828 KB |
Time limit exceeded |
48 |
Incorrect |
13 ms |
3148 KB |
Output isn't correct |
49 |
Correct |
22 ms |
10176 KB |
Output is correct |
50 |
Execution timed out |
1086 ms |
8528 KB |
Time limit exceeded |
51 |
Incorrect |
16 ms |
3404 KB |
Output isn't correct |
52 |
Incorrect |
27 ms |
11592 KB |
Output isn't correct |
53 |
Execution timed out |
1093 ms |
9440 KB |
Time limit exceeded |
54 |
Incorrect |
18 ms |
3724 KB |
Output isn't correct |
55 |
Incorrect |
37 ms |
13092 KB |
Output isn't correct |
56 |
Execution timed out |
1092 ms |
10520 KB |
Time limit exceeded |
57 |
Incorrect |
20 ms |
3916 KB |
Output isn't correct |
58 |
Incorrect |
48 ms |
14800 KB |
Output isn't correct |
59 |
Execution timed out |
1090 ms |
11460 KB |
Time limit exceeded |
60 |
Incorrect |
23 ms |
4108 KB |
Output isn't correct |
61 |
Incorrect |
63 ms |
16572 KB |
Output isn't correct |
62 |
Execution timed out |
1088 ms |
9676 KB |
Time limit exceeded |
63 |
Correct |
82 ms |
4292 KB |
Output is correct |
64 |
Correct |
88 ms |
4248 KB |
Output is correct |
65 |
Correct |
88 ms |
4240 KB |
Output is correct |
66 |
Correct |
76 ms |
4292 KB |
Output is correct |
67 |
Correct |
37 ms |
4248 KB |
Output is correct |
68 |
Correct |
48 ms |
4260 KB |
Output is correct |
69 |
Correct |
41 ms |
4256 KB |
Output is correct |
70 |
Correct |
37 ms |
4336 KB |
Output is correct |
71 |
Correct |
44 ms |
4268 KB |
Output is correct |
72 |
Incorrect |
32 ms |
4132 KB |
Output isn't correct |
73 |
Execution timed out |
1091 ms |
4516 KB |
Time limit exceeded |
74 |
Execution timed out |
1094 ms |
6596 KB |
Time limit exceeded |
75 |
Execution timed out |
1084 ms |
4644 KB |
Time limit exceeded |
76 |
Execution timed out |
1097 ms |
4548 KB |
Time limit exceeded |
77 |
Execution timed out |
1094 ms |
4804 KB |
Time limit exceeded |
78 |
Execution timed out |
1086 ms |
4548 KB |
Time limit exceeded |
79 |
Execution timed out |
1093 ms |
4608 KB |
Time limit exceeded |
80 |
Execution timed out |
1092 ms |
4500 KB |
Time limit exceeded |
81 |
Execution timed out |
1098 ms |
4548 KB |
Time limit exceeded |
82 |
Execution timed out |
1086 ms |
4676 KB |
Time limit exceeded |
83 |
Execution timed out |
1093 ms |
4308 KB |
Time limit exceeded |
84 |
Execution timed out |
1090 ms |
4364 KB |
Time limit exceeded |
85 |
Execution timed out |
1083 ms |
4420 KB |
Time limit exceeded |
86 |
Execution timed out |
1085 ms |
4420 KB |
Time limit exceeded |
87 |
Execution timed out |
1088 ms |
4420 KB |
Time limit exceeded |
88 |
Execution timed out |
1087 ms |
4264 KB |
Time limit exceeded |
89 |
Execution timed out |
1090 ms |
4428 KB |
Time limit exceeded |
90 |
Execution timed out |
1083 ms |
4272 KB |
Time limit exceeded |
91 |
Execution timed out |
1091 ms |
4316 KB |
Time limit exceeded |
92 |
Execution timed out |
1091 ms |
4316 KB |
Time limit exceeded |