#include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mp make_pair
#define FOR(i,n) for(i=0;i<(n);++i)
#define FORe(i,n) for(i=1;i<=(n);++i)
#define FORr(i,a,b) for(i=(a);i<(b);++i)
#define FORrev(i,n) for(i=(n);i>=0;--i)
#define F0R(i,n) for(int i=0;i<(n);++i)
#define F0Re(i,n) for(int i=1;i<=(n);++i)
#define F0Rr(i,a,b) for(ll i=(a);i<(b);++i)
#define F0Rrev(i,n) for(int i=(n);i>=0;--i)
#define ii pair<ll,ll>
#define vi vector<ll>
#define vii vector<ii>
#define ff first
#define ss second
#define cd complex<double>
#define vcd vector<cd>
#define ldd long double
#define dbgLine cout<<"Line : "<<__LINE__<<'\n'
#define all(x) (x).begin(),(x).end()
using namespace std;
const short int __PRECISION = 10;
const ll MOD = 1e9+7;
const ll INF = 1e17 + 1101;
const ll LOGN = 17;
const ll MAXN = 2e5+5;
const ll ROOTN = 320;
const int inf = 1e9+2;
const ldd PI = acos(-1);
const ldd EPS = 1e-7;
int N, S, disB[900][900], disM[900][900], X, Y, H, K;
char grid[900][900];
bool vis[900][900];
inline bool inside(int i, int j)
{
return ((i < N) && (i >= 0) && (j < N) && (j >= 0));
}
int main()
{
/*
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
*/
cin>>N>>S;
queue<pair<int, int>> Q;
F0R(i, N)
F0R(j ,N)
{
cin>>grid[i][j];
vis[i][j] = false;
disB[i][j] = inf;
if(grid[i][j] == 'H')
Q.push(mp(i, j)), disB[i][j] = 0, vis[i][j] = true;
else if(grid[i][j] == 'M')
X = i, Y = j;
else if(grid[i][j] == 'D')
H = i, K = j;
}
while(!Q.empty())
{
int x = Q.front().ff, y = Q.front().ss;
Q.pop();
for(pair<int, int> p : {mp(0, -1), mp(0, 1), mp(1, 0), mp(-1, 0)})
if(inside(x+p.ff, y+p.ss) && (!vis[x+p.ff][y+p.ss]) && (grid[x+p.ff][y+p.ss] == 'G' or grid[x+p.ff][y+p.ss] == 'D'))
{
disB[x+p.ff][y+p.ss] = disB[x][y] + 1;
vis[x+p.ff][y+p.ss] = true;
Q.push(mp(x+p.ff, y+p.ss));
}
}
int lo = -1, hi = 4000, mid;
while(lo < hi - 1)
{
mid = (lo + hi)/2;
F0R(i, N)
F0R(j, N)
vis[i][j] = false, disM[i][j] = inf;
vis[X][Y] = true;
disM[X][Y] = 0;
Q.push(mp(X, Y));
while(!Q.empty())
{
int x = Q.front().ff, y = Q.front().ss;
Q.pop();
if (disB[x][y] < mid + 1 + (disM[x][y])/S)
{
disM[x][y] = inf;
continue;
}
for(pair<int, int> p : {mp(0, -1), mp(0, 1), mp(1, 0), mp(-1, 0)})
if(inside(x+p.ff, y+p.ss)&&(!vis[x+p.ff][y+p.ss])&&(grid[x+p.ff][y+p.ss] == 'G' or grid[x+p.ff][y+p.ss] == 'D'))
{
disM[x+p.ff][y+p.ss] = disM[x][y] + 1;
vis[x+p.ff][y+p.ss] = true;
Q.push(mp(x+p.ff, y+p.ss));
}
}
if(disM[H][K] == inf)
hi = mid;
else
lo = mid;
}
cout<<-1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
2 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
3 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
4 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
5 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
6 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
7 |
Incorrect |
148 ms |
7416 KB |
Output isn't correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Incorrect |
5 ms |
512 KB |
Output isn't correct |
10 |
Incorrect |
5 ms |
512 KB |
Output isn't correct |
11 |
Incorrect |
5 ms |
512 KB |
Output isn't correct |
12 |
Incorrect |
5 ms |
896 KB |
Output isn't correct |
13 |
Incorrect |
5 ms |
768 KB |
Output isn't correct |
14 |
Correct |
5 ms |
896 KB |
Output is correct |
15 |
Incorrect |
5 ms |
512 KB |
Output isn't correct |
16 |
Incorrect |
5 ms |
512 KB |
Output isn't correct |
17 |
Incorrect |
5 ms |
512 KB |
Output isn't correct |
18 |
Incorrect |
5 ms |
512 KB |
Output isn't correct |
19 |
Incorrect |
5 ms |
640 KB |
Output isn't correct |
20 |
Incorrect |
5 ms |
640 KB |
Output isn't correct |
21 |
Incorrect |
5 ms |
640 KB |
Output isn't correct |
22 |
Incorrect |
5 ms |
640 KB |
Output isn't correct |
23 |
Incorrect |
5 ms |
768 KB |
Output isn't correct |
24 |
Incorrect |
5 ms |
768 KB |
Output isn't correct |
25 |
Incorrect |
5 ms |
896 KB |
Output isn't correct |
26 |
Incorrect |
5 ms |
896 KB |
Output isn't correct |
27 |
Incorrect |
5 ms |
896 KB |
Output isn't correct |
28 |
Incorrect |
5 ms |
896 KB |
Output isn't correct |
29 |
Incorrect |
5 ms |
1024 KB |
Output isn't correct |
30 |
Incorrect |
5 ms |
896 KB |
Output isn't correct |
31 |
Incorrect |
5 ms |
1024 KB |
Output isn't correct |
32 |
Incorrect |
6 ms |
1024 KB |
Output isn't correct |
33 |
Incorrect |
28 ms |
3456 KB |
Output isn't correct |
34 |
Incorrect |
33 ms |
3456 KB |
Output isn't correct |
35 |
Incorrect |
36 ms |
3456 KB |
Output isn't correct |
36 |
Incorrect |
36 ms |
3840 KB |
Output isn't correct |
37 |
Incorrect |
45 ms |
3832 KB |
Output isn't correct |
38 |
Incorrect |
47 ms |
3840 KB |
Output isn't correct |
39 |
Incorrect |
44 ms |
4352 KB |
Output isn't correct |
40 |
Incorrect |
52 ms |
4344 KB |
Output isn't correct |
41 |
Incorrect |
57 ms |
4348 KB |
Output isn't correct |
42 |
Incorrect |
53 ms |
4736 KB |
Output isn't correct |
43 |
Incorrect |
66 ms |
4736 KB |
Output isn't correct |
44 |
Incorrect |
71 ms |
4736 KB |
Output isn't correct |
45 |
Incorrect |
62 ms |
5112 KB |
Output isn't correct |
46 |
Incorrect |
76 ms |
5112 KB |
Output isn't correct |
47 |
Incorrect |
91 ms |
5308 KB |
Output isn't correct |
48 |
Incorrect |
74 ms |
5624 KB |
Output isn't correct |
49 |
Incorrect |
91 ms |
5624 KB |
Output isn't correct |
50 |
Incorrect |
107 ms |
5624 KB |
Output isn't correct |
51 |
Incorrect |
87 ms |
6008 KB |
Output isn't correct |
52 |
Incorrect |
106 ms |
6112 KB |
Output isn't correct |
53 |
Incorrect |
121 ms |
6008 KB |
Output isn't correct |
54 |
Incorrect |
98 ms |
6520 KB |
Output isn't correct |
55 |
Incorrect |
123 ms |
6520 KB |
Output isn't correct |
56 |
Incorrect |
142 ms |
6524 KB |
Output isn't correct |
57 |
Incorrect |
112 ms |
6904 KB |
Output isn't correct |
58 |
Incorrect |
141 ms |
7032 KB |
Output isn't correct |
59 |
Incorrect |
172 ms |
7032 KB |
Output isn't correct |
60 |
Incorrect |
127 ms |
7416 KB |
Output isn't correct |
61 |
Incorrect |
159 ms |
7416 KB |
Output isn't correct |
62 |
Incorrect |
181 ms |
7416 KB |
Output isn't correct |
63 |
Incorrect |
176 ms |
7416 KB |
Output isn't correct |
64 |
Incorrect |
258 ms |
7288 KB |
Output isn't correct |
65 |
Incorrect |
236 ms |
7416 KB |
Output isn't correct |
66 |
Incorrect |
203 ms |
7416 KB |
Output isn't correct |
67 |
Correct |
171 ms |
7416 KB |
Output is correct |
68 |
Incorrect |
114 ms |
7544 KB |
Output isn't correct |
69 |
Incorrect |
106 ms |
7348 KB |
Output isn't correct |
70 |
Incorrect |
104 ms |
7544 KB |
Output isn't correct |
71 |
Incorrect |
99 ms |
7416 KB |
Output isn't correct |
72 |
Incorrect |
94 ms |
7420 KB |
Output isn't correct |
73 |
Incorrect |
127 ms |
7672 KB |
Output isn't correct |
74 |
Incorrect |
123 ms |
7672 KB |
Output isn't correct |
75 |
Incorrect |
134 ms |
7700 KB |
Output isn't correct |
76 |
Incorrect |
122 ms |
7672 KB |
Output isn't correct |
77 |
Incorrect |
119 ms |
7672 KB |
Output isn't correct |
78 |
Correct |
133 ms |
7800 KB |
Output is correct |
79 |
Incorrect |
117 ms |
7800 KB |
Output isn't correct |
80 |
Incorrect |
124 ms |
7672 KB |
Output isn't correct |
81 |
Incorrect |
133 ms |
7716 KB |
Output isn't correct |
82 |
Incorrect |
128 ms |
7676 KB |
Output isn't correct |
83 |
Incorrect |
135 ms |
7636 KB |
Output isn't correct |
84 |
Incorrect |
132 ms |
7544 KB |
Output isn't correct |
85 |
Incorrect |
131 ms |
7544 KB |
Output isn't correct |
86 |
Incorrect |
143 ms |
7544 KB |
Output isn't correct |
87 |
Incorrect |
137 ms |
7672 KB |
Output isn't correct |
88 |
Incorrect |
143 ms |
7544 KB |
Output isn't correct |
89 |
Incorrect |
142 ms |
7672 KB |
Output isn't correct |
90 |
Incorrect |
149 ms |
7588 KB |
Output isn't correct |
91 |
Incorrect |
142 ms |
7544 KB |
Output isn't correct |
92 |
Incorrect |
138 ms |
7548 KB |
Output isn't correct |