#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define all(a) (a).begin(), (a).end()
using pii = pair<int, int>;
const int inf = 1e7;
int r, c, n;
int sr, sc, gr, gc;
vector<string> A;
int main() {
cin >> r >> c >> n;
if(c > r) {
swap(r, c);
cin >> sc >> sr >> gc >> gr;
A = vector<string>(r, string(c, '#'));
for(int i = 0; i < c; i++) {
string s; cin >> s;
for(int j = 0; j < r; j++) {
A[j][i] = s[j];
}
}
}
else {
cin >> sr >> sc >> gr >> gc;
A = vector<string>(r);
for(int i = 0; i < c; i++) {
cin >> A[i];
}
}
sr--, sc--, gr--, gc--;
// cerr << "\n";
// for(int i = 0; i < r; i++) {
// cerr << A[i] << "\n";
// }
// c < 2500
vector<vector<int>> dist(r, vector<int>(c, inf));
vector<vector<bool>> visited(r, vector<bool>(c));
vector<set<int>> checked(c);
deque<pii> deq;
vector<pii> dir{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
set<pii> available;
for(int i = 0; i < r; i++) {
for(int j = 0; j < r; j++) {
available.insert({i, j});
}
}
auto transition = [&](int row, int s, int e, int d) {
//cerr << "transit: " << row << " " << s << " " << e << " " << d << "\n";
// for(int i = s; i <= e; i++) {
// if(dist[row][i] > d+1) {
// dist[row][i] = d+1;
// deq.push_back({row, i});
// }
// }
// for(int i = e; i >= s; i--) {
// if(dist[row][i] > d+1) {
// dist[row][i] = d+1;
// deq.push_back({row, i});
// }
// }
auto it = available.lower_bound({row, s});
vector<pii> del;
while(it != available.end() && it->fi == row && it->se <= e) {
if(dist[row][it->se] > d+1) {
dist[row][it->se] = d+1;
deq.push_back({row, it->se});
del.pb(*it);
}
it = next(it);
}
for(auto p : del) {
available.erase(p);
}
};
dist[sr][sc] = 0;
deq.push_back({sr, sc});
while(!deq.empty()) {
int i = deq.front().fi, j = deq.front().se;
deq.pop_front();
if(visited[i][j]) continue;
visited[i][j] = true;
for(auto d : dir) {
int ii = i+d.fi, jj = j+d.se;
if(ii < 0 || ii >= r || jj < 0 || jj >= c) continue;
if(A[ii][jj] == '.' && dist[ii][jj] > dist[i][j]) {
dist[ii][jj] = dist[i][j];
deq.push_front({ii, jj});
}
}
int li = max(0, i-(n-1)), ri = min(r-1, i+(n-1));
if(!checked[j].empty()) {
auto it = checked[j].lower_bound(i);
if(it == checked[j].end()) {
it = prev(it);
li = max(li, (*it)+n);
}
else {
ri = min(ri, (*it)-n);
if(it != checked[j].begin()) {
it = prev(it);
li = max(li, (*it)+n);
}
}
}
checked[j].insert(i);
if(li <= ri) {
for(int row = li; row <= ri; row++) {
transition(row, max(0, j-n), min(c-1, j+n), dist[i][j]);
}
}
if(i-n >= 0) {
transition(i-n, max(0, j-n+1), min(c-1, j+n-1), dist[i][j]);
}
if(i+n < r) {
transition(i+n, max(0, j-n+1), min(c-1, j+n-1), dist[i][j]);
}
}
cout << dist[gr][gc] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
168 ms |
47456 KB |
Output is correct |
8 |
Correct |
171 ms |
47288 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
296 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
13 |
Correct |
3 ms |
1336 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
50 ms |
3572 KB |
Output is correct |
20 |
Execution timed out |
2094 ms |
472488 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
2 ms |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
173 ms |
47396 KB |
Output is correct |
16 |
Correct |
175 ms |
47380 KB |
Output is correct |
17 |
Correct |
38 ms |
12108 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
3 ms |
1236 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
3 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
300 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
300 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
174 ms |
47388 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
552 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
3 ms |
1364 KB |
Output is correct |
21 |
Correct |
1 ms |
428 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
296 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
83 ms |
17612 KB |
Output is correct |
26 |
Correct |
304 ms |
67856 KB |
Output is correct |
27 |
Correct |
36 ms |
6232 KB |
Output is correct |
28 |
Correct |
21 ms |
3916 KB |
Output is correct |
29 |
Correct |
21 ms |
3520 KB |
Output is correct |
30 |
Correct |
20 ms |
3544 KB |
Output is correct |
31 |
Correct |
212 ms |
44656 KB |
Output is correct |
32 |
Correct |
47 ms |
4868 KB |
Output is correct |
33 |
Correct |
45 ms |
4556 KB |
Output is correct |
34 |
Correct |
129 ms |
17796 KB |
Output is correct |
35 |
Correct |
106 ms |
15324 KB |
Output is correct |
36 |
Correct |
54 ms |
9420 KB |
Output is correct |
37 |
Correct |
55 ms |
8296 KB |
Output is correct |
38 |
Correct |
52 ms |
8272 KB |
Output is correct |
39 |
Correct |
351 ms |
27696 KB |
Output is correct |
40 |
Execution timed out |
2100 ms |
447256 KB |
Time limit exceeded |
41 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
2 ms |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
173 ms |
47396 KB |
Output is correct |
16 |
Correct |
175 ms |
47380 KB |
Output is correct |
17 |
Correct |
38 ms |
12108 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
3 ms |
1236 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
3 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
300 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
43 ms |
3660 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1020 ms |
235040 KB |
Output is correct |
36 |
Correct |
84 ms |
17636 KB |
Output is correct |
37 |
Execution timed out |
2111 ms |
445804 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
2 ms |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
173 ms |
47396 KB |
Output is correct |
16 |
Correct |
175 ms |
47380 KB |
Output is correct |
17 |
Correct |
38 ms |
12108 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
3 ms |
1236 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
3 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
300 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
43 ms |
3660 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1020 ms |
235040 KB |
Output is correct |
36 |
Correct |
84 ms |
17636 KB |
Output is correct |
37 |
Execution timed out |
2111 ms |
445804 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
168 ms |
47456 KB |
Output is correct |
8 |
Correct |
171 ms |
47288 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
296 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
13 |
Correct |
3 ms |
1336 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
50 ms |
3572 KB |
Output is correct |
20 |
Execution timed out |
2094 ms |
472488 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
168 ms |
47456 KB |
Output is correct |
8 |
Correct |
171 ms |
47288 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
296 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
13 |
Correct |
3 ms |
1336 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
50 ms |
3572 KB |
Output is correct |
20 |
Execution timed out |
2094 ms |
472488 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
168 ms |
47456 KB |
Output is correct |
8 |
Correct |
171 ms |
47288 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
296 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
13 |
Correct |
3 ms |
1336 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
50 ms |
3572 KB |
Output is correct |
20 |
Execution timed out |
2094 ms |
472488 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |