// Source: https://usaco.guide/general/io
#include <bits/stdc++.h>
using namespace std;
int N,S;
string M[1001];
struct node {
int hd;
int md;
int hop;
};
bool v[1001][1001];
node all[1001][1001];
pair<int,int> m;
bool work(int e) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
all[i][j].md = -1;
}
}
queue<pair<int,int> > q;
q.push(m);
all[m.first][m.second].md = e;
all[m.first][m.second].hop = 0;
if (all[m.first][m.second].hd != -1 && all[m.first][m.second].hd <= e) return false;
while (!q.empty()) {
auto c = q.front();
//cout << "handling " << c.first << "/" << c.second << "/" << all[c.first][c.second].md << endl;
int cost = all[c.first][c.second].md+1;
q.pop();
queue<tuple<int,int,int> > tq;
tq.push(make_tuple(c.first,c.second,0));
while (!tq.empty()) {
auto tc = tq.front();
//cout << " tq " << get<0>(tc) << "/" << get<1>(tc) << "/" << get<2>(tc) << endl;
tq.pop();
int hop = get<2>(tc)+1;
if (hop > S) continue;
for (auto i : {-1,1}) {
for (int j = 0; j <= 1; j++) {
int ti = j ? get<0>(tc) : (get<0>(tc)+i);
int tj = j ? (get<1>(tc)+i) : (get<1>(tc));
if (ti < 0 || ti >= N || tj < 0 || tj >= N) continue;
//cout << " " << ti << "/" << tj << " " << endl;
if (M[ti][tj] == 'T') continue;
if (all[ti][tj].md >= 0
&& (all[ti][tj].md < cost || (all[ti][tj].md == cost && all[ti][tj].hop <= hop))) continue;
if (all[ti][tj].hd >= 0 && all[ti][tj].hd < cost) continue;
if (M[ti][tj] == 'D') return true;
if (hop == S && all[ti][tj].hd >= 0 && all[ti][tj].hd == cost) continue;
//cout << " " << ti << "/" << tj << "/" << hop << " passed" << endl;
all[ti][tj].md = cost;
all[ti][tj].hop = hop;
if (all[ti][tj].hd > cost) {
q.push(make_pair(ti,tj));
//cout << " add q " << ti << "/" << tj << "/" << cost << endl;
}
if (hop == S) continue;
tq.push(make_tuple(ti,tj,hop));
}
}
}
/*for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << all[i][j].md << "/" << all[i][j].hop << " ";
}
cout << endl;
}*/
}
return false;
}
int solve() {
int lo = 0, hi = 2*N;
// if none of the values in the range work, return lo - 1
lo--;
while (lo < hi) {
// find the middle of the current range (rounding up)
int mid = lo + (hi - lo + 1) / 2;
//cout << "mid: " << mid << ", " << lo << "/" << hi;
if (work(mid)) {
//cout << " worked" << endl;
// if mid works, then all numbers smaller than mid also work
lo = mid;
} else {
//cout << " not work" << endl;
// if mid does not work, greater values would not work either
hi = mid - 1;
}
}
return lo;
}
int main() {
cin >> N >> S;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
all[i][j].hd = -1;
}
}
queue<pair<int,int> > q;
for (int i = 0; i < N; i++) {
cin >> M[i];
for (int j = 0; j < N; j++) {
if (M[i][j] == 'H') {
all[i][j].hd = 0;
q.push(make_pair(i,j));
} else if (M[i][j] == 'M') {
m = make_pair(i,j);
}
}
}
while (!q.empty()) {
auto c = q.front();
int cost = all[c.first][c.second].hd+1;
q.pop();
for (auto i : {-1,1}) {
for (int j = 0; j <= 1; j++) {
int ti = j ? c.first : c.first+i;
int tj = j ? c.second+i : c.second;
if (ti < 0 || ti >= N || tj < 0 || tj >= N) continue;
if (M[ti][tj]=='T' || (all[ti][tj].hd!=-1 && all[ti][tj].hd<=cost)) continue;
all[ti][tj].hd = cost;
q.push(make_pair(ti,tj));
//cout << "add " << ti << "/" << tj << endl;
}
}
}
/*for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << all[i][j].hd << " ";
}
cout << endl;
}*/
int r = solve();
cout << r << endl;
//cout << work(2) << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
7 |
Execution timed out |
1063 ms |
13944 KB |
Time limit exceeded |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Incorrect |
1 ms |
2384 KB |
Output isn't correct |
13 |
Correct |
1 ms |
2552 KB |
Output is correct |
14 |
Correct |
2 ms |
2384 KB |
Output is correct |
15 |
Incorrect |
1 ms |
2384 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
2384 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
2384 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
2384 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
2384 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
2552 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
2384 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
2384 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
2384 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
2384 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
2384 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
2384 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
28 |
Incorrect |
2 ms |
2384 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
2384 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
2384 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
2384 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
2384 KB |
Output isn't correct |
33 |
Incorrect |
6 ms |
4944 KB |
Output isn't correct |
34 |
Incorrect |
6 ms |
5112 KB |
Output isn't correct |
35 |
Correct |
430 ms |
13452 KB |
Output is correct |
36 |
Incorrect |
7 ms |
6736 KB |
Output isn't correct |
37 |
Incorrect |
7 ms |
6888 KB |
Output isn't correct |
38 |
Correct |
601 ms |
19944 KB |
Output is correct |
39 |
Incorrect |
8 ms |
6992 KB |
Output isn't correct |
40 |
Incorrect |
8 ms |
7196 KB |
Output isn't correct |
41 |
Correct |
951 ms |
25616 KB |
Output is correct |
42 |
Incorrect |
9 ms |
7248 KB |
Output isn't correct |
43 |
Incorrect |
9 ms |
7404 KB |
Output isn't correct |
44 |
Execution timed out |
1064 ms |
32676 KB |
Time limit exceeded |
45 |
Incorrect |
14 ms |
9296 KB |
Output isn't correct |
46 |
Incorrect |
12 ms |
9552 KB |
Output isn't correct |
47 |
Execution timed out |
1061 ms |
43372 KB |
Time limit exceeded |
48 |
Incorrect |
12 ms |
9552 KB |
Output isn't correct |
49 |
Incorrect |
12 ms |
9704 KB |
Output isn't correct |
50 |
Execution timed out |
1050 ms |
53592 KB |
Time limit exceeded |
51 |
Incorrect |
14 ms |
9808 KB |
Output isn't correct |
52 |
Incorrect |
13 ms |
9464 KB |
Output isn't correct |
53 |
Execution timed out |
1079 ms |
65536 KB |
Time limit exceeded |
54 |
Incorrect |
16 ms |
9808 KB |
Output isn't correct |
55 |
Incorrect |
16 ms |
9808 KB |
Output isn't correct |
56 |
Runtime error |
681 ms |
65536 KB |
Execution killed with signal 9 |
57 |
Incorrect |
25 ms |
11600 KB |
Output isn't correct |
58 |
Incorrect |
19 ms |
12112 KB |
Output isn't correct |
59 |
Runtime error |
761 ms |
65536 KB |
Execution killed with signal 9 |
60 |
Incorrect |
22 ms |
12112 KB |
Output isn't correct |
61 |
Incorrect |
21 ms |
12280 KB |
Output isn't correct |
62 |
Runtime error |
231 ms |
65536 KB |
Execution killed with signal 9 |
63 |
Correct |
172 ms |
11600 KB |
Output is correct |
64 |
Correct |
261 ms |
13128 KB |
Output is correct |
65 |
Correct |
272 ms |
12112 KB |
Output is correct |
66 |
Correct |
204 ms |
11664 KB |
Output is correct |
67 |
Correct |
218 ms |
12288 KB |
Output is correct |
68 |
Correct |
69 ms |
12116 KB |
Output is correct |
69 |
Correct |
57 ms |
12360 KB |
Output is correct |
70 |
Correct |
65 ms |
12112 KB |
Output is correct |
71 |
Correct |
51 ms |
12112 KB |
Output is correct |
72 |
Correct |
36 ms |
11704 KB |
Output is correct |
73 |
Correct |
126 ms |
12368 KB |
Output is correct |
74 |
Correct |
469 ms |
24692 KB |
Output is correct |
75 |
Correct |
322 ms |
12368 KB |
Output is correct |
76 |
Correct |
225 ms |
12368 KB |
Output is correct |
77 |
Correct |
521 ms |
12368 KB |
Output is correct |
78 |
Correct |
442 ms |
12368 KB |
Output is correct |
79 |
Correct |
622 ms |
46648 KB |
Output is correct |
80 |
Correct |
301 ms |
12368 KB |
Output is correct |
81 |
Correct |
339 ms |
12368 KB |
Output is correct |
82 |
Correct |
587 ms |
12712 KB |
Output is correct |
83 |
Correct |
530 ms |
12668 KB |
Output is correct |
84 |
Correct |
468 ms |
29424 KB |
Output is correct |
85 |
Correct |
916 ms |
12812 KB |
Output is correct |
86 |
Correct |
693 ms |
12368 KB |
Output is correct |
87 |
Execution timed out |
1059 ms |
13744 KB |
Time limit exceeded |
88 |
Correct |
972 ms |
12708 KB |
Output is correct |
89 |
Runtime error |
235 ms |
65536 KB |
Execution killed with signal 9 |
90 |
Execution timed out |
1068 ms |
14068 KB |
Time limit exceeded |
91 |
Execution timed out |
1051 ms |
18628 KB |
Time limit exceeded |
92 |
Execution timed out |
1035 ms |
13172 KB |
Time limit exceeded |