#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
typedef pair<pii,int> piii;
int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
int pre[2000][2000];
char mark[2000][2000];
int homex,homey;
int s,sx,sy,N;
bool test(int time)
{
bool reach[2005][2005];
memset(reach,0,sizeof(reach));
if(time * s >= pre[sx][sy]) return false;
queue<piii> q;
q.push(make_pair(make_pair(sx,sy),time*s));
reach[sx][sy]=1;
while(!q.empty())
{
int x=q.front().first.first;
int y=q.front().first.second;
int dis=q.front().second;
if(mark[x][y]=='D') return true;
q.pop();
for(int c=0;c<4;c++)
{
int cx=x+dx[c],cy=y+dy[c];
if(cx<0 || cx>=N || cy<0 || cy>=N || pre[cx][cy]<=dis+1 || mark[cx][cy]=='T' || reach[cx][cy]) continue;
reach[cx][cy]=true;
q.push(make_pair(make_pair(cx,cy),dis+1));
}
}
return false;
}
int main()
{
memset(pre,-1,sizeof(pre));
scanf("%d %d",&N,&s);
queue<pii> que;
for(int i=0;i<N;i++)scanf("%s",mark[i]);
for(int i=0;i<N;i++){
for(int j=0;j<N;j++)
{
if(mark[i][j]=='M') sx=i,sy=j,mark[i][j]='G';
if(mark[i][j]=='D') homex=i,homey=j;
if(mark[i][j]=='H') que.push(make_pair(i,j)),pre[i][j]=0;
}
}
pre[homex][homey]=N*N*s;
while(!que.empty())
{
int x=que.front().first;
int y=que.front().second;
int dis=pre[x][y];
que.pop();
for(int c=0;c<4;c++)
{
int cx=x+dx[c],cy=y+dy[c];
if(cx<0 || cx>=N || cy<0 || cy>=N || pre[cx][cy]!=-1 || mark[cx][cy]!='G') continue;
pre[cx][cy]=dis+s;
que.push(make_pair(cx,cy));
}
}
int low=0,high=N*N*2,ans=-1;
for(;low<high;)
{
int mid=(low+high)/2;
if(test(mid)) ans=max(ans,mid),low=mid+1;
else high=mid;
}
return !printf("%d",ans);
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&N,&s);
~~~~~^~~~~~~~~~~~~~~
mecho.cpp:40:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=0;i<N;i++)scanf("%s",mark[i]);
~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
19968 KB |
Output is correct |
2 |
Correct |
20 ms |
20080 KB |
Output is correct |
3 |
Correct |
18 ms |
20080 KB |
Output is correct |
4 |
Correct |
17 ms |
20080 KB |
Output is correct |
5 |
Correct |
18 ms |
20080 KB |
Output is correct |
6 |
Correct |
18 ms |
20216 KB |
Output is correct |
7 |
Correct |
143 ms |
22484 KB |
Output is correct |
8 |
Correct |
20 ms |
22484 KB |
Output is correct |
9 |
Correct |
21 ms |
22484 KB |
Output is correct |
10 |
Correct |
20 ms |
22484 KB |
Output is correct |
11 |
Correct |
19 ms |
22484 KB |
Output is correct |
12 |
Correct |
21 ms |
22484 KB |
Output is correct |
13 |
Correct |
22 ms |
22484 KB |
Output is correct |
14 |
Correct |
20 ms |
22484 KB |
Output is correct |
15 |
Correct |
22 ms |
22484 KB |
Output is correct |
16 |
Correct |
18 ms |
22484 KB |
Output is correct |
17 |
Correct |
19 ms |
22484 KB |
Output is correct |
18 |
Correct |
18 ms |
22484 KB |
Output is correct |
19 |
Correct |
18 ms |
22484 KB |
Output is correct |
20 |
Correct |
18 ms |
22484 KB |
Output is correct |
21 |
Correct |
19 ms |
22484 KB |
Output is correct |
22 |
Correct |
22 ms |
22484 KB |
Output is correct |
23 |
Correct |
21 ms |
22484 KB |
Output is correct |
24 |
Correct |
19 ms |
22484 KB |
Output is correct |
25 |
Correct |
21 ms |
22484 KB |
Output is correct |
26 |
Correct |
19 ms |
22484 KB |
Output is correct |
27 |
Correct |
20 ms |
22484 KB |
Output is correct |
28 |
Correct |
19 ms |
22484 KB |
Output is correct |
29 |
Correct |
19 ms |
22484 KB |
Output is correct |
30 |
Correct |
21 ms |
22484 KB |
Output is correct |
31 |
Correct |
19 ms |
22484 KB |
Output is correct |
32 |
Correct |
21 ms |
22484 KB |
Output is correct |
33 |
Correct |
27 ms |
22484 KB |
Output is correct |
34 |
Correct |
25 ms |
22484 KB |
Output is correct |
35 |
Correct |
52 ms |
22484 KB |
Output is correct |
36 |
Correct |
28 ms |
22484 KB |
Output is correct |
37 |
Correct |
26 ms |
22576 KB |
Output is correct |
38 |
Correct |
55 ms |
22692 KB |
Output is correct |
39 |
Correct |
25 ms |
23056 KB |
Output is correct |
40 |
Correct |
25 ms |
23328 KB |
Output is correct |
41 |
Correct |
82 ms |
23524 KB |
Output is correct |
42 |
Correct |
26 ms |
23752 KB |
Output is correct |
43 |
Correct |
30 ms |
24000 KB |
Output is correct |
44 |
Correct |
76 ms |
24436 KB |
Output is correct |
45 |
Correct |
29 ms |
24624 KB |
Output is correct |
46 |
Correct |
33 ms |
24956 KB |
Output is correct |
47 |
Correct |
133 ms |
25216 KB |
Output is correct |
48 |
Correct |
27 ms |
25668 KB |
Output is correct |
49 |
Correct |
28 ms |
26000 KB |
Output is correct |
50 |
Correct |
130 ms |
26364 KB |
Output is correct |
51 |
Correct |
33 ms |
26984 KB |
Output is correct |
52 |
Correct |
35 ms |
27376 KB |
Output is correct |
53 |
Correct |
124 ms |
27944 KB |
Output is correct |
54 |
Correct |
30 ms |
28360 KB |
Output is correct |
55 |
Correct |
33 ms |
28776 KB |
Output is correct |
56 |
Correct |
152 ms |
29328 KB |
Output is correct |
57 |
Correct |
32 ms |
29960 KB |
Output is correct |
58 |
Correct |
32 ms |
30512 KB |
Output is correct |
59 |
Correct |
174 ms |
31220 KB |
Output is correct |
60 |
Correct |
32 ms |
31872 KB |
Output is correct |
61 |
Correct |
33 ms |
32500 KB |
Output is correct |
62 |
Correct |
205 ms |
33128 KB |
Output is correct |
63 |
Correct |
154 ms |
33844 KB |
Output is correct |
64 |
Correct |
242 ms |
34396 KB |
Output is correct |
65 |
Correct |
231 ms |
35012 KB |
Output is correct |
66 |
Correct |
168 ms |
35640 KB |
Output is correct |
67 |
Correct |
169 ms |
36268 KB |
Output is correct |
68 |
Correct |
75 ms |
36900 KB |
Output is correct |
69 |
Correct |
62 ms |
37524 KB |
Output is correct |
70 |
Correct |
57 ms |
38152 KB |
Output is correct |
71 |
Correct |
63 ms |
38780 KB |
Output is correct |
72 |
Correct |
51 ms |
39408 KB |
Output is correct |
73 |
Correct |
61 ms |
40292 KB |
Output is correct |
74 |
Correct |
85 ms |
41028 KB |
Output is correct |
75 |
Correct |
117 ms |
41564 KB |
Output is correct |
76 |
Correct |
123 ms |
42208 KB |
Output is correct |
77 |
Correct |
119 ms |
42944 KB |
Output is correct |
78 |
Correct |
129 ms |
43436 KB |
Output is correct |
79 |
Correct |
75 ms |
44012 KB |
Output is correct |
80 |
Correct |
111 ms |
44720 KB |
Output is correct |
81 |
Correct |
108 ms |
45464 KB |
Output is correct |
82 |
Correct |
87 ms |
46000 KB |
Output is correct |
83 |
Correct |
128 ms |
46532 KB |
Output is correct |
84 |
Correct |
159 ms |
47124 KB |
Output is correct |
85 |
Correct |
106 ms |
47748 KB |
Output is correct |
86 |
Correct |
113 ms |
48404 KB |
Output is correct |
87 |
Correct |
109 ms |
49136 KB |
Output is correct |
88 |
Correct |
125 ms |
49760 KB |
Output is correct |
89 |
Correct |
118 ms |
50388 KB |
Output is correct |
90 |
Correct |
131 ms |
50840 KB |
Output is correct |
91 |
Correct |
135 ms |
51548 KB |
Output is correct |
92 |
Correct |
118 ms |
52292 KB |
Output is correct |