#include <bits/stdc++.h>
using namespace std;
int n,s;
bool is[801][801];
int tim[801][801];
bool vis[801][801];
struct field
{
int x,y;
};
vector<field> hives;
field start;
field home;
bool check(int m)
{
bool visited[801][801];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
visited[i][j]=0;
}
}
if(m+1>=tim[start.x][start.y])return 0;
queue<pair<field,int> > q;
q.push(make_pair(start,0));
while(!q.empty())
{
auto x=q.front();
q.pop();
field cur=x.first;
if(cur.x==home.x && cur.y==home.y)return 1;
int d=x.second;
visited[cur.x][cur.y]=1;
if(cur.x+1<n && !visited[cur.x+1][cur.y] && is[cur.x+1][cur.y] && m+(d+1)/s<tim[cur.x+1][cur.y])
{
cur.x++;
visited[cur.x][cur.y]=1;
q.push(make_pair(cur,d+1));
cur.x--;
}
if(cur.x-1>=0 && !visited[cur.x-1][cur.y] && is[cur.x-1][cur.y] && m+(d+1)/s<tim[cur.x-1][cur.y])
{
cur.x--;
visited[cur.x][cur.y]=1;
q.push(make_pair(cur,d+1));
cur.x++;
}
if(cur.y+1<n && !visited[cur.x][cur.y+1] && is[cur.x][cur.y+1] && m+(d+1)/s<tim[cur.x][cur.y+1])
{
cur.y++;
visited[cur.x][cur.y]=1;
q.push(make_pair(cur,d+1));
cur.y--;
}
if(cur.y-1>=0 && !visited[cur.x][cur.y-1] && is[cur.x][cur.y-1] && m+(d+1)/s<tim[cur.x][cur.y-1])
{
cur.y--;
visited[cur.x][cur.y]=1;
q.push(make_pair(cur,d+1));
cur.y++;
}
}
return 0;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> s;
char c;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin >> c;
if(c=='T')
{
is[i][j]=0;
}
else if(c=='M')
{
is[i][j]=1;
start.x=i;
start.y=j;
}
else if(c=='D')
{
is[i][j]=1;
home.x=i;
home.y=j;
}
else if(c=='H')
{
is[i][j]=1;
field temp;
temp.x=i;
temp.y=j;
hives.push_back(temp);
}
else
{
is[i][j]=1;
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
tim[i][j]=1000000000;
}
}
queue<pair<field,int> > q;
for(int i=0;i<hives.size();i++)
{
q.push(make_pair(hives[i],0));
vis[hives[i].x][hives[i].y]=1;
}
while(!q.empty())
{
auto x=q.front();
q.pop();
field cur=x.first;
int d=x.second;
//if(vis[cur.x][cur.y])continue;
vis[cur.x][cur.y]=1;
tim[cur.x][cur.y]=min(tim[cur.x][cur.y],d);
if(cur.x+1<n && !vis[cur.x+1][cur.y] && is[cur.x+1][cur.y])
{
cur.x++;
vis[cur.x][cur.y]=1;
q.push(make_pair(cur,d+1));
cur.x--;
}
if(cur.x-1>=0 && !vis[cur.x-1][cur.y] && is[cur.x-1][cur.y])
{
cur.x--;
vis[cur.x][cur.y]=1;
q.push(make_pair(cur,d+1));
cur.x++;
}
if(cur.y+1<n && !vis[cur.x][cur.y+1] && is[cur.x][cur.y+1])
{
cur.y++;
vis[cur.x][cur.y]=1;
q.push(make_pair(cur,d+1));
cur.y--;
}
if(cur.y-1>=0 && !vis[cur.x][cur.y-1] && is[cur.x][cur.y-1])
{
cur.y--;
vis[cur.x][cur.y]=1;
q.push(make_pair(cur,d+1));
cur.y++;
}
}
tim[home.x][home.y]=1000000000;
int l=0;
int r=1000000;
int m;
int rez=-1;
while(l<r)
{
m=(l+r)/2;
if(check(m))
{
l=m+1;
rez=max(rez,m);
}
else
{
r=m;
}
}
cout << rez;
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:117:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<hives.size();i++)
~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
74 ms |
4856 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
12 |
Incorrect |
5 ms |
640 KB |
Output isn't correct |
13 |
Incorrect |
5 ms |
640 KB |
Output isn't correct |
14 |
Correct |
5 ms |
640 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
4 ms |
512 KB |
Output is correct |
17 |
Correct |
5 ms |
512 KB |
Output is correct |
18 |
Correct |
5 ms |
512 KB |
Output is correct |
19 |
Correct |
5 ms |
512 KB |
Output is correct |
20 |
Correct |
5 ms |
512 KB |
Output is correct |
21 |
Correct |
5 ms |
512 KB |
Output is correct |
22 |
Correct |
5 ms |
512 KB |
Output is correct |
23 |
Correct |
5 ms |
640 KB |
Output is correct |
24 |
Correct |
5 ms |
640 KB |
Output is correct |
25 |
Correct |
5 ms |
640 KB |
Output is correct |
26 |
Correct |
5 ms |
640 KB |
Output is correct |
27 |
Correct |
5 ms |
640 KB |
Output is correct |
28 |
Correct |
5 ms |
640 KB |
Output is correct |
29 |
Correct |
5 ms |
768 KB |
Output is correct |
30 |
Correct |
5 ms |
768 KB |
Output is correct |
31 |
Correct |
5 ms |
768 KB |
Output is correct |
32 |
Correct |
5 ms |
768 KB |
Output is correct |
33 |
Correct |
10 ms |
2304 KB |
Output is correct |
34 |
Correct |
9 ms |
2304 KB |
Output is correct |
35 |
Correct |
21 ms |
2304 KB |
Output is correct |
36 |
Correct |
11 ms |
2560 KB |
Output is correct |
37 |
Correct |
11 ms |
2560 KB |
Output is correct |
38 |
Correct |
25 ms |
2560 KB |
Output is correct |
39 |
Correct |
13 ms |
2816 KB |
Output is correct |
40 |
Correct |
12 ms |
2816 KB |
Output is correct |
41 |
Correct |
33 ms |
2816 KB |
Output is correct |
42 |
Correct |
14 ms |
3072 KB |
Output is correct |
43 |
Correct |
14 ms |
3072 KB |
Output is correct |
44 |
Correct |
38 ms |
3072 KB |
Output is correct |
45 |
Correct |
16 ms |
3328 KB |
Output is correct |
46 |
Correct |
15 ms |
3328 KB |
Output is correct |
47 |
Correct |
47 ms |
3328 KB |
Output is correct |
48 |
Correct |
18 ms |
3584 KB |
Output is correct |
49 |
Correct |
17 ms |
3584 KB |
Output is correct |
50 |
Correct |
66 ms |
3584 KB |
Output is correct |
51 |
Correct |
21 ms |
3968 KB |
Output is correct |
52 |
Correct |
19 ms |
3968 KB |
Output is correct |
53 |
Correct |
64 ms |
4088 KB |
Output is correct |
54 |
Correct |
25 ms |
4224 KB |
Output is correct |
55 |
Correct |
22 ms |
4224 KB |
Output is correct |
56 |
Correct |
71 ms |
4224 KB |
Output is correct |
57 |
Correct |
25 ms |
4480 KB |
Output is correct |
58 |
Correct |
23 ms |
4480 KB |
Output is correct |
59 |
Correct |
85 ms |
4600 KB |
Output is correct |
60 |
Correct |
29 ms |
4736 KB |
Output is correct |
61 |
Correct |
27 ms |
4736 KB |
Output is correct |
62 |
Correct |
94 ms |
4736 KB |
Output is correct |
63 |
Correct |
99 ms |
4736 KB |
Output is correct |
64 |
Correct |
148 ms |
4856 KB |
Output is correct |
65 |
Correct |
137 ms |
4728 KB |
Output is correct |
66 |
Correct |
124 ms |
4728 KB |
Output is correct |
67 |
Correct |
105 ms |
4728 KB |
Output is correct |
68 |
Correct |
54 ms |
4728 KB |
Output is correct |
69 |
Correct |
54 ms |
4728 KB |
Output is correct |
70 |
Correct |
51 ms |
4984 KB |
Output is correct |
71 |
Correct |
44 ms |
4728 KB |
Output is correct |
72 |
Incorrect |
42 ms |
4728 KB |
Output isn't correct |
73 |
Incorrect |
41 ms |
5112 KB |
Output isn't correct |
74 |
Correct |
55 ms |
5112 KB |
Output is correct |
75 |
Correct |
60 ms |
5120 KB |
Output is correct |
76 |
Correct |
55 ms |
5240 KB |
Output is correct |
77 |
Correct |
58 ms |
5120 KB |
Output is correct |
78 |
Correct |
67 ms |
5112 KB |
Output is correct |
79 |
Correct |
54 ms |
5120 KB |
Output is correct |
80 |
Correct |
56 ms |
5112 KB |
Output is correct |
81 |
Correct |
61 ms |
5120 KB |
Output is correct |
82 |
Correct |
56 ms |
5112 KB |
Output is correct |
83 |
Correct |
69 ms |
4976 KB |
Output is correct |
84 |
Correct |
61 ms |
4984 KB |
Output is correct |
85 |
Correct |
64 ms |
4976 KB |
Output is correct |
86 |
Correct |
68 ms |
5112 KB |
Output is correct |
87 |
Correct |
70 ms |
5104 KB |
Output is correct |
88 |
Correct |
70 ms |
4984 KB |
Output is correct |
89 |
Correct |
71 ms |
4984 KB |
Output is correct |
90 |
Correct |
83 ms |
4984 KB |
Output is correct |
91 |
Correct |
75 ms |
4984 KB |
Output is correct |
92 |
Correct |
72 ms |
4984 KB |
Output is correct |