/* * * * * * * * * *\
* Author: Haidara *
* LANG: C++17 *
* PROB: *
\* * * * * * * * * */
#include<bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define int long long
#define rep(i,x,n) for(int i=x;i<(n);i++)
#define FOR(i,n) rep(i,0,n)
#define per(i,x,n) for(int i=x;i>(n);i--)
#define ROF(i,x) for(int i=x;i>=0;i--)
#define v(i) vector< i >
#define p(i,j) pair< i , j >
#define pii pair<int,int>
#define m(i,j) map< i , j >
#define um(i,j) unordered_map< i , j >
#define pq(i) priority_queue< i >
#define ff first
#define all(x) x.begin(),x.end()
#define ss second
#define pp push_back
using namespace std;
void SIO(string name="")
{
if(name!="")
{
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
}
const int inf=1LL<<62LL;
const int mod=1e9+7;
const int maxn=808;
char a[maxn][maxn];
int n,jump;
pii M,D;
queue<pii>q;
int dist[maxn][maxn],dx[] {-1,1,0,0},dy[] {0,0,1,-1};
bool valid(int x,int y)
{
return x>=0&&y>=0&&x<n&&y<n&&a[x][y]!='T';
}
void solve()
{
while(q.size())
{
pii f=q.front();
q.pop();
FOR(i,4)
{
int x=f.ff+dx[i],y=f.ss+dy[i];
if(valid(x,y)&&dist[f.ff][f.ss]+jump<dist[x][y])
dist[x][y]=dist[f.ff][f.ss]+jump,q.push({x,y});
}
}
}
bool check(int st)
{
queue<p(pii,int)>qq;
qq.push({M,st*jump});
bool vis[maxn][maxn];
FOR(i,maxn)
FOR(j,maxn)
vis[i][j]=0;
bool ret=0;
vis[M.ff][M.ss]=1;
while(qq.size())
{
pii f=qq.front().ff;
int curr=qq.front().ss;
qq.pop();
if(f==D)
ret=1;
FOR(i,4)
{
int x=f.ff+dx[i],y=f.ss+dy[i];
if(valid(x,y))
{
if(dist[x][y]>curr+1&&!vis[x][y])
qq.push({{x,y},curr+1}),vis[x][y]=1;
}
}
}
return ret;
}
signed main()
{
SIO("");
cin>>n>>jump;
FOR(i,n)
FOR(j,n)
{
dist[i][j]=inf;
cin>>a[i][j];
if(a[i][j]=='M')
M= {i,j};
else if(a[i][j]=='D')
D= {i,j};
else if(a[i][j]=='H')
q.push({i,j}),dist[i][j]=0;
}
solve();
int l=1,r=maxn*maxn*2,ans=-1;
while(l<=r)
{
int mid=l+(r-l)/2;
if(check(mid))
ans=max(ans,mid),l=mid+1;
else
r=mid-1;
}
cout<<ans;
}
Compilation message
mecho.cpp: In function 'void SIO(std::string)':
mecho.cpp:28:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | freopen((name+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:29:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | freopen((name+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
844 KB |
Output is correct |
2 |
Incorrect |
1 ms |
844 KB |
Output isn't correct |
3 |
Correct |
1 ms |
844 KB |
Output is correct |
4 |
Incorrect |
2 ms |
844 KB |
Output isn't correct |
5 |
Incorrect |
1 ms |
844 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
972 KB |
Output isn't correct |
7 |
Incorrect |
148 ms |
6956 KB |
Output isn't correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
972 KB |
Output is correct |
11 |
Correct |
1 ms |
972 KB |
Output is correct |
12 |
Incorrect |
2 ms |
1228 KB |
Output isn't correct |
13 |
Incorrect |
2 ms |
1100 KB |
Output isn't correct |
14 |
Correct |
2 ms |
1228 KB |
Output is correct |
15 |
Correct |
2 ms |
972 KB |
Output is correct |
16 |
Correct |
2 ms |
972 KB |
Output is correct |
17 |
Correct |
1 ms |
972 KB |
Output is correct |
18 |
Correct |
2 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
1100 KB |
Output is correct |
20 |
Correct |
2 ms |
1100 KB |
Output is correct |
21 |
Correct |
2 ms |
1100 KB |
Output is correct |
22 |
Correct |
1 ms |
1100 KB |
Output is correct |
23 |
Correct |
2 ms |
1100 KB |
Output is correct |
24 |
Correct |
2 ms |
1100 KB |
Output is correct |
25 |
Correct |
2 ms |
1228 KB |
Output is correct |
26 |
Correct |
2 ms |
1252 KB |
Output is correct |
27 |
Correct |
2 ms |
1228 KB |
Output is correct |
28 |
Correct |
2 ms |
1228 KB |
Output is correct |
29 |
Correct |
2 ms |
1228 KB |
Output is correct |
30 |
Correct |
2 ms |
1228 KB |
Output is correct |
31 |
Correct |
2 ms |
1228 KB |
Output is correct |
32 |
Correct |
2 ms |
1228 KB |
Output is correct |
33 |
Correct |
12 ms |
3424 KB |
Output is correct |
34 |
Correct |
15 ms |
3404 KB |
Output is correct |
35 |
Correct |
37 ms |
3420 KB |
Output is correct |
36 |
Correct |
17 ms |
3692 KB |
Output is correct |
37 |
Correct |
16 ms |
3788 KB |
Output is correct |
38 |
Correct |
50 ms |
3788 KB |
Output is correct |
39 |
Correct |
18 ms |
4044 KB |
Output is correct |
40 |
Correct |
19 ms |
4044 KB |
Output is correct |
41 |
Correct |
59 ms |
4148 KB |
Output is correct |
42 |
Correct |
24 ms |
4464 KB |
Output is correct |
43 |
Correct |
22 ms |
4400 KB |
Output is correct |
44 |
Correct |
91 ms |
4420 KB |
Output is correct |
45 |
Correct |
27 ms |
4804 KB |
Output is correct |
46 |
Correct |
35 ms |
4828 KB |
Output is correct |
47 |
Correct |
93 ms |
4856 KB |
Output is correct |
48 |
Correct |
30 ms |
5068 KB |
Output is correct |
49 |
Correct |
30 ms |
5112 KB |
Output is correct |
50 |
Correct |
107 ms |
5184 KB |
Output is correct |
51 |
Correct |
37 ms |
5504 KB |
Output is correct |
52 |
Correct |
37 ms |
5548 KB |
Output is correct |
53 |
Correct |
141 ms |
5556 KB |
Output is correct |
54 |
Correct |
46 ms |
5816 KB |
Output is correct |
55 |
Correct |
43 ms |
5904 KB |
Output is correct |
56 |
Correct |
174 ms |
6040 KB |
Output is correct |
57 |
Correct |
47 ms |
6252 KB |
Output is correct |
58 |
Correct |
50 ms |
6252 KB |
Output is correct |
59 |
Correct |
188 ms |
6268 KB |
Output is correct |
60 |
Correct |
55 ms |
6556 KB |
Output is correct |
61 |
Correct |
58 ms |
6596 KB |
Output is correct |
62 |
Correct |
212 ms |
6628 KB |
Output is correct |
63 |
Incorrect |
143 ms |
6532 KB |
Output isn't correct |
64 |
Correct |
231 ms |
6600 KB |
Output is correct |
65 |
Correct |
215 ms |
6616 KB |
Output is correct |
66 |
Correct |
169 ms |
6680 KB |
Output is correct |
67 |
Correct |
173 ms |
6724 KB |
Output is correct |
68 |
Incorrect |
88 ms |
6580 KB |
Output isn't correct |
69 |
Correct |
82 ms |
6568 KB |
Output is correct |
70 |
Correct |
83 ms |
6616 KB |
Output is correct |
71 |
Correct |
95 ms |
6596 KB |
Output is correct |
72 |
Incorrect |
68 ms |
6696 KB |
Output isn't correct |
73 |
Incorrect |
76 ms |
7144 KB |
Output isn't correct |
74 |
Correct |
103 ms |
7108 KB |
Output is correct |
75 |
Correct |
138 ms |
7144 KB |
Output is correct |
76 |
Correct |
117 ms |
7236 KB |
Output is correct |
77 |
Correct |
119 ms |
7124 KB |
Output is correct |
78 |
Correct |
135 ms |
7100 KB |
Output is correct |
79 |
Correct |
93 ms |
7028 KB |
Output is correct |
80 |
Correct |
109 ms |
7228 KB |
Output is correct |
81 |
Correct |
121 ms |
7092 KB |
Output is correct |
82 |
Correct |
119 ms |
7080 KB |
Output is correct |
83 |
Incorrect |
146 ms |
6980 KB |
Output isn't correct |
84 |
Correct |
139 ms |
7032 KB |
Output is correct |
85 |
Correct |
125 ms |
7032 KB |
Output is correct |
86 |
Correct |
142 ms |
6944 KB |
Output is correct |
87 |
Correct |
145 ms |
7028 KB |
Output is correct |
88 |
Incorrect |
145 ms |
6852 KB |
Output isn't correct |
89 |
Correct |
160 ms |
6940 KB |
Output is correct |
90 |
Correct |
163 ms |
6884 KB |
Output is correct |
91 |
Correct |
148 ms |
6932 KB |
Output is correct |
92 |
Correct |
177 ms |
6932 KB |
Output is correct |