#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i = (a) ; i<(b) ; i++)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for(int i = (b)-1 ; i>=(a) ; i--)
#define R0F(i,a) ROF(i,0,a)
#define REP(a) F0R(_,a)
#define each(e,a) for(auto &e : (a))
#define sz(v) (int)(v).size()
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define rsz resize
#define pb push_back
#define f first
#define s second
#define pf push_front
#define ft front()
#define bk back()
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define tcT template<class T
#define tcTU tcT, class U
#define nl "\n"
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vb = vector<bool>;
using pi = pair<int,int>;
using vpi = vector<pi>;
using str = string;
using vs = vector<str>;
using db = long double;
using pl = pair<ll,ll>;
using pd = pair<db,db>;
tcT> using V = vector<T>;
tcT> using pqg = priority_queue<T,vector<T>,greater<T>>;
tcTU> using PR = pair<T,U>;
const int MOD = 1e9+7;
const ll INF = 1e18;
const int dx[] = {-1,0,1,0}, dy[] = {0,-1,0,1};
constexpr int bits(int x) { return x == 0 ? 0 : 31 - __builtin_clz(x); }
constexpr bool onbit(int msk, int i) { return msk>>i&1; }
constexpr ll p2(int x) { return 1LL<<x; }
constexpr int pct(int x) { return __builtin_popcount(x); }
constexpr int lg2(int x) { return x == 0 ? 0 : 31 - __builtin_clz(x); }; //int only
tcT> bool ckmin(T&a, const T&b) { return b < a ? a = b,1 : 0; }
tcT> bool ckmax(T&a, const T&b) { return b > a ? a = b,1 : 0; }
ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); }
ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); }
tcTU> T fstTrue(T lo, T hi, U f) {
hi++; assert(lo <= hi);
while (lo < hi) {
T mid = lo+(hi-lo)/2;
f(mid) ? hi = mid : lo = mid+1;
}
return lo;
}
tcTU> T lstTrue(T lo, T hi, U f) {
lo--; assert(lo <= hi);
while (lo < hi) {
T mid = lo+(hi-lo+1)/2;
f(mid) ? lo = mid : hi = mid-1;
}
return lo;
}
tcT> void remDup(V<T> &v) {
sort(all(v)); v.erase(unique(all(v)), end(v)); }
void DBG() { cerr << "]\n"; }
template<class H, class... T> void DBG(H h, T... t) {
cerr << h; if(sizeof...(t)) cerr << ", ";
DBG(t...);
}
#ifdef LOCAL
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif // LOCAL
void setPrec() { cout << fixed << setprecision(16); }
void setIO(string NAME = "") {
cin.tie(0)->sync_with_stdio(0);
setPrec();
if(sz(NAME)) {
freopen((NAME + ".inp").c_str(),"r",stdin);
freopen((NAME + ".out").c_str(),"w",stdout);
}
}
const int MX = 805;
int N, S, si, sj, ei, ej;
str g[MX];
int d[MX][MX], d1[MX][MX];
bool isInside(int x, int y) {
return min(x,y)>=0 && max(x,y)<N;
}
void bfs(int dis[MX][MX], int st) {
queue<pi> q;
F0R(i,N) F0R(j,N) if(!dis[i][j]) q.push({i,j});
while(!q.empty()) {
int x = q.ft.f, y = q.ft.s; q.pop();
if(st>=0 && dis[x][y]/S + st >= d1[x][y]) continue;
F0R(i,4) {
int nx = x + dx[i], ny = y + dy[i];
if(isInside(nx, ny) && (g[nx][ny]!='T') && (st>=0 || g[nx][ny] != 'D') && dis[nx][ny]>=MOD) {
q.push({nx, ny});
dis[nx][ny] = dis[x][y] + 1;
}
}
}
}
void solve() {
cin>>N>>S;
F0R(i,N) cin>>g[i];
memset(d1, 0x3f, sizeof d1);
F0R(i,N) F0R(j,N) {
if(g[i][j] == 'M') {
si = i, sj = j;
}
else if(g[i][j] == 'H') {
d1[i][j] = 0;
}
else if(g[i][j] == 'D') {
ei = i, ej = j;
}
}
bfs(d1, -1);
int lo = 0, hi = N*N+1;
while(lo<hi) {
int mid = lo + (hi-lo+1)/2;
memset(d, 0x3f, sizeof d);
d[si][sj] = 0;
bfs(d, mid);
if(d[ei][ej] < MOD) lo = mid;
else hi = mid-1;
}
cout << lo << nl;
}
int main() {
setIO();
int t=1;
//cin>>t;
while(t-->0) {
solve();
}
return 0;
}
Compilation message
mecho.cpp: In function 'void setIO(std::string)':
mecho.cpp:93:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | freopen((NAME + ".inp").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:94:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
94 | freopen((NAME + ".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5324 KB |
Output is correct |
2 |
Correct |
4 ms |
5324 KB |
Output is correct |
3 |
Correct |
3 ms |
5324 KB |
Output is correct |
4 |
Correct |
3 ms |
5324 KB |
Output is correct |
5 |
Correct |
4 ms |
5340 KB |
Output is correct |
6 |
Correct |
3 ms |
5328 KB |
Output is correct |
7 |
Correct |
124 ms |
6708 KB |
Output is correct |
8 |
Incorrect |
3 ms |
5324 KB |
Output isn't correct |
9 |
Correct |
5 ms |
5324 KB |
Output is correct |
10 |
Correct |
3 ms |
5324 KB |
Output is correct |
11 |
Correct |
3 ms |
5332 KB |
Output is correct |
12 |
Correct |
3 ms |
5336 KB |
Output is correct |
13 |
Correct |
3 ms |
5332 KB |
Output is correct |
14 |
Incorrect |
4 ms |
5332 KB |
Output isn't correct |
15 |
Correct |
3 ms |
5332 KB |
Output is correct |
16 |
Correct |
3 ms |
5324 KB |
Output is correct |
17 |
Correct |
3 ms |
5324 KB |
Output is correct |
18 |
Correct |
3 ms |
5324 KB |
Output is correct |
19 |
Correct |
3 ms |
5324 KB |
Output is correct |
20 |
Correct |
3 ms |
5324 KB |
Output is correct |
21 |
Correct |
3 ms |
5324 KB |
Output is correct |
22 |
Correct |
3 ms |
5324 KB |
Output is correct |
23 |
Correct |
3 ms |
5324 KB |
Output is correct |
24 |
Correct |
4 ms |
5452 KB |
Output is correct |
25 |
Correct |
3 ms |
5332 KB |
Output is correct |
26 |
Correct |
3 ms |
5324 KB |
Output is correct |
27 |
Correct |
3 ms |
5324 KB |
Output is correct |
28 |
Correct |
3 ms |
5324 KB |
Output is correct |
29 |
Correct |
5 ms |
5324 KB |
Output is correct |
30 |
Correct |
4 ms |
5324 KB |
Output is correct |
31 |
Correct |
4 ms |
5324 KB |
Output is correct |
32 |
Correct |
5 ms |
5428 KB |
Output is correct |
33 |
Correct |
7 ms |
5580 KB |
Output is correct |
34 |
Correct |
7 ms |
5580 KB |
Output is correct |
35 |
Correct |
21 ms |
5584 KB |
Output is correct |
36 |
Correct |
8 ms |
5656 KB |
Output is correct |
37 |
Correct |
8 ms |
5596 KB |
Output is correct |
38 |
Correct |
26 ms |
5708 KB |
Output is correct |
39 |
Correct |
9 ms |
5764 KB |
Output is correct |
40 |
Correct |
10 ms |
5724 KB |
Output is correct |
41 |
Correct |
48 ms |
5808 KB |
Output is correct |
42 |
Correct |
15 ms |
5856 KB |
Output is correct |
43 |
Correct |
12 ms |
5924 KB |
Output is correct |
44 |
Correct |
44 ms |
5900 KB |
Output is correct |
45 |
Correct |
12 ms |
5964 KB |
Output is correct |
46 |
Correct |
13 ms |
5984 KB |
Output is correct |
47 |
Correct |
57 ms |
5936 KB |
Output is correct |
48 |
Correct |
13 ms |
6056 KB |
Output is correct |
49 |
Correct |
15 ms |
6104 KB |
Output is correct |
50 |
Correct |
69 ms |
6044 KB |
Output is correct |
51 |
Correct |
15 ms |
6244 KB |
Output is correct |
52 |
Correct |
26 ms |
6272 KB |
Output is correct |
53 |
Correct |
74 ms |
6220 KB |
Output is correct |
54 |
Correct |
17 ms |
6340 KB |
Output is correct |
55 |
Correct |
18 ms |
6348 KB |
Output is correct |
56 |
Correct |
81 ms |
6364 KB |
Output is correct |
57 |
Correct |
21 ms |
6468 KB |
Output is correct |
58 |
Correct |
22 ms |
6500 KB |
Output is correct |
59 |
Correct |
107 ms |
6576 KB |
Output is correct |
60 |
Correct |
21 ms |
6596 KB |
Output is correct |
61 |
Correct |
22 ms |
6604 KB |
Output is correct |
62 |
Correct |
170 ms |
6828 KB |
Output is correct |
63 |
Correct |
90 ms |
6612 KB |
Output is correct |
64 |
Correct |
176 ms |
6688 KB |
Output is correct |
65 |
Correct |
144 ms |
6600 KB |
Output is correct |
66 |
Correct |
120 ms |
6688 KB |
Output is correct |
67 |
Incorrect |
106 ms |
6700 KB |
Output isn't correct |
68 |
Correct |
49 ms |
6624 KB |
Output is correct |
69 |
Correct |
44 ms |
6620 KB |
Output is correct |
70 |
Correct |
46 ms |
6620 KB |
Output is correct |
71 |
Correct |
48 ms |
6628 KB |
Output is correct |
72 |
Correct |
29 ms |
6596 KB |
Output is correct |
73 |
Correct |
32 ms |
6972 KB |
Output is correct |
74 |
Correct |
50 ms |
7024 KB |
Output is correct |
75 |
Correct |
81 ms |
6876 KB |
Output is correct |
76 |
Correct |
61 ms |
6852 KB |
Output is correct |
77 |
Correct |
65 ms |
6884 KB |
Output is correct |
78 |
Incorrect |
65 ms |
6940 KB |
Output isn't correct |
79 |
Correct |
55 ms |
6940 KB |
Output is correct |
80 |
Correct |
51 ms |
6864 KB |
Output is correct |
81 |
Correct |
65 ms |
6976 KB |
Output is correct |
82 |
Correct |
58 ms |
6944 KB |
Output is correct |
83 |
Correct |
69 ms |
6852 KB |
Output is correct |
84 |
Correct |
72 ms |
6904 KB |
Output is correct |
85 |
Correct |
95 ms |
6908 KB |
Output is correct |
86 |
Correct |
94 ms |
6852 KB |
Output is correct |
87 |
Correct |
68 ms |
6812 KB |
Output is correct |
88 |
Correct |
73 ms |
6852 KB |
Output is correct |
89 |
Correct |
93 ms |
6760 KB |
Output is correct |
90 |
Correct |
111 ms |
6856 KB |
Output is correct |
91 |
Correct |
70 ms |
6880 KB |
Output is correct |
92 |
Correct |
74 ms |
6848 KB |
Output is correct |