#include <bits/stdc++.h>
using namespace std;
const int MAX=810;
const int INF=(1<<30);
int di[4]={0,0,-1,1};
int dj[4]={1,-1,0,0};
int n,s;
char mat[MAX][MAX];
int timebees[MAX][MAX];
bool visbees[MAX][MAX];
bool vis[MAX][MAX];
vector<pair<int,int> > hiveinitial;
vector<pair<int,int> > homeinitial;
int mi,mj;
int homei,homej;
bool validBees(int i, int j)
{
if(i<0 || i>=n || j<0 || j>=n || mat[i][j]=='T'||mat[i][j]=='D'|| mat[i][j]=='H'|| visbees[i][j])return false;
return true;
}
bool validBear(int i, int j)
{
if(i<0 || i>=n || j<0 || j>=n || mat[i][j]!='G' || vis[i][j])return false;
return true;
}
void bfsBees()
{
queue<pair<int,int> > q;
for(int i=0; i<hiveinitial.size(); i++)
{
int x=hiveinitial[i].first;
int y=hiveinitial[i].second;
q.push({x,y});
visbees[x][y]=true;
timebees[x][y]=0;
}
while(!q.empty())
{
pair<int,int> curr=q.front();
q.pop();
int x=curr.first;
int y=curr.second;
for(int k=0; k<4; k++)
{
int newi=x+di[k];
int newj=y+dj[k];
if(validBees(newi,newj))
{
q.push({newi,newj});
timebees[newi][newj]=timebees[x][y]+1;
visbees[newi][newj]=true;
}
}
}
return;
}
bool bearreached(int mechodis, int beedis)
{
return mechodis/s<beedis;
}
bool canReach(int secs)
{
queue<pair<int,int> > q;
q.push({mi,mj});
if(timebees[mi][mj]<=secs)
{
return false;
}
memset(vis,false,sizeof(vis));
vis[mi][mj]=true;
queue<int> steps;
steps.push(0);
while(!q.empty())
{
pair<int,int> curr=q.front();
int x=curr.first;
int y=curr.second;
int currstep=steps.front();
steps.pop();
q.pop();
for(int k=0; k<4; k++)
{
int newi=x+di[k];
int newj=y+dj[k];
if(mat[newi][newj]=='D')return true;
if(validBear(newi,newj))
{
if(bearreached(currstep+1,timebees[newi][newj]-secs))
{
q.push({newi,newj});
steps.push(currstep+1);
vis[newi][newj]=true;
}
}
}
}
for(int k=0; k<4; k++)
{
int newi=homei+di[k];
int newj=homej+dj[k];
if(validBear(newi,newj)&& vis[newi][newj])return true;
}
return false;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>s;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
cin>>mat[i][j];
if(mat[i][j]=='H')
{
hiveinitial.push_back({i,j});
}
if(mat[i][j]=='M')
{
mi=i;
mj=j;
}
if(mat[i][j]=='D')
{
homei=i;
homej=j;
}
}
}
bfsBees();
int b=0;
int e=n*n;
while(b<=e)
{
int mid=(b+e)/2;
if(canReach(mid))
{
b=mid+1;
}
else
{
e=mid-1;
}
}
cout<<b-1<<endl;
return 0;
}
Compilation message
mecho.cpp: In function 'void bfsBees()':
mecho.cpp:30:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(int i=0; i<hiveinitial.size(); i++)
| ~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
71 ms |
5044 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4700 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
1 ms |
4700 KB |
Output is correct |
20 |
Correct |
1 ms |
4700 KB |
Output is correct |
21 |
Correct |
1 ms |
4700 KB |
Output is correct |
22 |
Correct |
1 ms |
4700 KB |
Output is correct |
23 |
Correct |
1 ms |
4700 KB |
Output is correct |
24 |
Correct |
1 ms |
4700 KB |
Output is correct |
25 |
Correct |
1 ms |
4700 KB |
Output is correct |
26 |
Correct |
1 ms |
4700 KB |
Output is correct |
27 |
Correct |
1 ms |
4700 KB |
Output is correct |
28 |
Correct |
1 ms |
4700 KB |
Output is correct |
29 |
Correct |
1 ms |
4700 KB |
Output is correct |
30 |
Correct |
1 ms |
4700 KB |
Output is correct |
31 |
Correct |
1 ms |
4700 KB |
Output is correct |
32 |
Correct |
1 ms |
4700 KB |
Output is correct |
33 |
Correct |
3 ms |
4700 KB |
Output is correct |
34 |
Correct |
3 ms |
4700 KB |
Output is correct |
35 |
Correct |
18 ms |
4700 KB |
Output is correct |
36 |
Correct |
3 ms |
4696 KB |
Output is correct |
37 |
Correct |
4 ms |
4572 KB |
Output is correct |
38 |
Correct |
24 ms |
4748 KB |
Output is correct |
39 |
Correct |
4 ms |
4700 KB |
Output is correct |
40 |
Correct |
4 ms |
4700 KB |
Output is correct |
41 |
Correct |
30 ms |
4572 KB |
Output is correct |
42 |
Correct |
5 ms |
4696 KB |
Output is correct |
43 |
Correct |
6 ms |
4700 KB |
Output is correct |
44 |
Correct |
43 ms |
4700 KB |
Output is correct |
45 |
Correct |
6 ms |
4700 KB |
Output is correct |
46 |
Correct |
8 ms |
4588 KB |
Output is correct |
47 |
Correct |
45 ms |
4584 KB |
Output is correct |
48 |
Correct |
7 ms |
4696 KB |
Output is correct |
49 |
Correct |
7 ms |
4700 KB |
Output is correct |
50 |
Correct |
52 ms |
4700 KB |
Output is correct |
51 |
Correct |
7 ms |
4700 KB |
Output is correct |
52 |
Correct |
8 ms |
4700 KB |
Output is correct |
53 |
Correct |
64 ms |
4836 KB |
Output is correct |
54 |
Correct |
8 ms |
4700 KB |
Output is correct |
55 |
Correct |
9 ms |
4700 KB |
Output is correct |
56 |
Correct |
77 ms |
4860 KB |
Output is correct |
57 |
Correct |
10 ms |
4700 KB |
Output is correct |
58 |
Correct |
10 ms |
4700 KB |
Output is correct |
59 |
Correct |
88 ms |
4900 KB |
Output is correct |
60 |
Correct |
11 ms |
4696 KB |
Output is correct |
61 |
Correct |
12 ms |
4700 KB |
Output is correct |
62 |
Correct |
103 ms |
4936 KB |
Output is correct |
63 |
Correct |
103 ms |
4956 KB |
Output is correct |
64 |
Correct |
159 ms |
4956 KB |
Output is correct |
65 |
Correct |
141 ms |
4708 KB |
Output is correct |
66 |
Correct |
95 ms |
4956 KB |
Output is correct |
67 |
Correct |
96 ms |
4952 KB |
Output is correct |
68 |
Correct |
36 ms |
4944 KB |
Output is correct |
69 |
Correct |
36 ms |
4956 KB |
Output is correct |
70 |
Correct |
26 ms |
4984 KB |
Output is correct |
71 |
Correct |
31 ms |
4952 KB |
Output is correct |
72 |
Correct |
22 ms |
4804 KB |
Output is correct |
73 |
Correct |
25 ms |
5092 KB |
Output is correct |
74 |
Correct |
46 ms |
5212 KB |
Output is correct |
75 |
Correct |
59 ms |
5200 KB |
Output is correct |
76 |
Correct |
47 ms |
5204 KB |
Output is correct |
77 |
Correct |
49 ms |
5236 KB |
Output is correct |
78 |
Correct |
66 ms |
5208 KB |
Output is correct |
79 |
Correct |
38 ms |
5208 KB |
Output is correct |
80 |
Correct |
51 ms |
5108 KB |
Output is correct |
81 |
Correct |
57 ms |
5208 KB |
Output is correct |
82 |
Correct |
48 ms |
5208 KB |
Output is correct |
83 |
Correct |
64 ms |
5164 KB |
Output is correct |
84 |
Correct |
56 ms |
4952 KB |
Output is correct |
85 |
Correct |
68 ms |
4956 KB |
Output is correct |
86 |
Correct |
61 ms |
4956 KB |
Output is correct |
87 |
Correct |
61 ms |
4952 KB |
Output is correct |
88 |
Correct |
66 ms |
4956 KB |
Output is correct |
89 |
Correct |
67 ms |
4956 KB |
Output is correct |
90 |
Correct |
68 ms |
4952 KB |
Output is correct |
91 |
Correct |
84 ms |
5336 KB |
Output is correct |
92 |
Correct |
73 ms |
5084 KB |
Output is correct |