#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int MX = 3000006;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
struct vortex{
int id;
int d[MX];
vortex(): id(0) {}
void push_back(int x){
id++;
d[id] = x;
}
void pop_back(){
if(id) id--;
}
void clear(){
id = 0;
}
int gx(int i){
return !i || i > id? -1 : d[i];
}
};
int n, m, k;
int Sx, Sy, Ex, Ey;
vector<vector<int>> ttr;
vector<vector<int>> ttb;
vector<vector<int>> d;
string a[MX];
queue<pii> Q[2];
void nhap(){
cin >> n >> m >> k;
cin >> Sx >> Sy >> Ex >> Ey;
ttr.assign(n + 5, vector<int>(m + 5, 0));
ttb.assign(m + 5, vector<int>(n + 5, 0));
for(int i=1; i<=n; i++){
cin >> a[i];
a[i] = ' ' + a[i];
for(int j=1; j<=m; j++){
ttr[i][j] = j + 1;
ttb[j][i] = i + 1;
}
}
}
void solve(){
d.assign(n + 5, vector<int>(m + 5, 1e9));
d[Sx][Sy] = 0;
Q[0].push({Sx, Sy});
for(int f=0; f<n*m; f++){
int c = f&1;
vector<pii> vt;
while(Q[c].size()){
int x, y;
tie(x, y) = Q[c].front();
vt.push_back({x, y});
Q[c].pop();
for(int i=0; i<4; i++){
int u = x + dx[i];
int v = y + dy[i];
if(u < 1 || u > n || v < 1 || v > m || a[u][v] == '#' || d[u][v] <= d[x][y]) continue;
d[u][v] = d[x][y];
Q[c].push({u, v});
}
}
for(pii td: vt){
int x, y;
tie(x, y) = td;
vector<int> vert;
for(int i=max(1, x-k); i<=min(n, x+k);){
vert.push_back(i);
vector<int> hori;
for(int j=max(1, y-k); j<=min(m, y+k);){
if((i == x - k || i == x + k) && (j == y - k || j == y + k)){
j = ttr[i][j];
continue;
}
hori.push_back(j);
if(d[i][j] > d[x][y] + 1){
d[i][j] = d[x][y] + 1;
Q[c^1].push({i, j});
}
j = ttr[i][j];
}
for(int j: hori) ttr[i][j] = ttr[i][hori.back()];
i = ttb[y][i];
}
for(int i: vert) ttb[y][i] = ttb[y][vert.back()];
}
}
cout << d[Ex][Ey];
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
nhap();
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
94544 KB |
Output is correct |
2 |
Correct |
45 ms |
94296 KB |
Output is correct |
3 |
Correct |
40 ms |
94168 KB |
Output is correct |
4 |
Correct |
40 ms |
94288 KB |
Output is correct |
5 |
Correct |
42 ms |
94296 KB |
Output is correct |
6 |
Correct |
57 ms |
94180 KB |
Output is correct |
7 |
Correct |
43 ms |
94392 KB |
Output is correct |
8 |
Correct |
41 ms |
94300 KB |
Output is correct |
9 |
Correct |
44 ms |
94288 KB |
Output is correct |
10 |
Correct |
40 ms |
94300 KB |
Output is correct |
11 |
Correct |
42 ms |
94288 KB |
Output is correct |
12 |
Correct |
43 ms |
94300 KB |
Output is correct |
13 |
Correct |
44 ms |
94392 KB |
Output is correct |
14 |
Incorrect |
40 ms |
94296 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
94288 KB |
Output is correct |
2 |
Correct |
49 ms |
94288 KB |
Output is correct |
3 |
Correct |
41 ms |
94300 KB |
Output is correct |
4 |
Correct |
43 ms |
94292 KB |
Output is correct |
5 |
Correct |
42 ms |
94288 KB |
Output is correct |
6 |
Correct |
49 ms |
94368 KB |
Output is correct |
7 |
Correct |
43 ms |
94288 KB |
Output is correct |
8 |
Correct |
43 ms |
94276 KB |
Output is correct |
9 |
Correct |
42 ms |
94292 KB |
Output is correct |
10 |
Correct |
40 ms |
94288 KB |
Output is correct |
11 |
Correct |
43 ms |
94220 KB |
Output is correct |
12 |
Correct |
41 ms |
94288 KB |
Output is correct |
13 |
Correct |
38 ms |
94288 KB |
Output is correct |
14 |
Correct |
42 ms |
94240 KB |
Output is correct |
15 |
Correct |
47 ms |
94288 KB |
Output is correct |
16 |
Correct |
42 ms |
94328 KB |
Output is correct |
17 |
Correct |
42 ms |
94288 KB |
Output is correct |
18 |
Correct |
43 ms |
94300 KB |
Output is correct |
19 |
Correct |
40 ms |
94296 KB |
Output is correct |
20 |
Correct |
42 ms |
94288 KB |
Output is correct |
21 |
Correct |
42 ms |
94148 KB |
Output is correct |
22 |
Correct |
43 ms |
94292 KB |
Output is correct |
23 |
Correct |
39 ms |
94340 KB |
Output is correct |
24 |
Correct |
45 ms |
94292 KB |
Output is correct |
25 |
Correct |
39 ms |
94288 KB |
Output is correct |
26 |
Correct |
41 ms |
94292 KB |
Output is correct |
27 |
Incorrect |
52 ms |
94224 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
94288 KB |
Output is correct |
2 |
Correct |
41 ms |
94288 KB |
Output is correct |
3 |
Correct |
42 ms |
94300 KB |
Output is correct |
4 |
Correct |
43 ms |
94300 KB |
Output is correct |
5 |
Correct |
39 ms |
94288 KB |
Output is correct |
6 |
Correct |
43 ms |
94288 KB |
Output is correct |
7 |
Correct |
45 ms |
94288 KB |
Output is correct |
8 |
Correct |
43 ms |
94292 KB |
Output is correct |
9 |
Correct |
49 ms |
94292 KB |
Output is correct |
10 |
Correct |
41 ms |
94292 KB |
Output is correct |
11 |
Correct |
46 ms |
94288 KB |
Output is correct |
12 |
Correct |
43 ms |
94300 KB |
Output is correct |
13 |
Correct |
43 ms |
94292 KB |
Output is correct |
14 |
Correct |
46 ms |
94196 KB |
Output is correct |
15 |
Correct |
41 ms |
94372 KB |
Output is correct |
16 |
Correct |
40 ms |
94376 KB |
Output is correct |
17 |
Correct |
41 ms |
94292 KB |
Output is correct |
18 |
Correct |
43 ms |
94300 KB |
Output is correct |
19 |
Correct |
47 ms |
94364 KB |
Output is correct |
20 |
Correct |
41 ms |
94160 KB |
Output is correct |
21 |
Correct |
42 ms |
94300 KB |
Output is correct |
22 |
Correct |
43 ms |
94292 KB |
Output is correct |
23 |
Correct |
48 ms |
94288 KB |
Output is correct |
24 |
Correct |
38 ms |
94300 KB |
Output is correct |
25 |
Correct |
46 ms |
94764 KB |
Output is correct |
26 |
Correct |
58 ms |
95704 KB |
Output is correct |
27 |
Correct |
53 ms |
95712 KB |
Output is correct |
28 |
Correct |
49 ms |
95808 KB |
Output is correct |
29 |
Correct |
51 ms |
96212 KB |
Output is correct |
30 |
Correct |
51 ms |
96216 KB |
Output is correct |
31 |
Correct |
51 ms |
95324 KB |
Output is correct |
32 |
Correct |
67 ms |
95648 KB |
Output is correct |
33 |
Correct |
55 ms |
95312 KB |
Output is correct |
34 |
Correct |
62 ms |
97224 KB |
Output is correct |
35 |
Correct |
63 ms |
98504 KB |
Output is correct |
36 |
Correct |
58 ms |
98508 KB |
Output is correct |
37 |
Correct |
64 ms |
100300 KB |
Output is correct |
38 |
Correct |
62 ms |
100020 KB |
Output is correct |
39 |
Correct |
147 ms |
104648 KB |
Output is correct |
40 |
Correct |
241 ms |
122944 KB |
Output is correct |
41 |
Correct |
301 ms |
131596 KB |
Output is correct |
42 |
Correct |
212 ms |
131776 KB |
Output is correct |
43 |
Correct |
219 ms |
145040 KB |
Output is correct |
44 |
Correct |
207 ms |
145100 KB |
Output is correct |
45 |
Correct |
308 ms |
134408 KB |
Output is correct |
46 |
Correct |
302 ms |
133728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
94288 KB |
Output is correct |
2 |
Correct |
49 ms |
94288 KB |
Output is correct |
3 |
Correct |
41 ms |
94300 KB |
Output is correct |
4 |
Correct |
43 ms |
94292 KB |
Output is correct |
5 |
Correct |
42 ms |
94288 KB |
Output is correct |
6 |
Correct |
49 ms |
94368 KB |
Output is correct |
7 |
Correct |
43 ms |
94288 KB |
Output is correct |
8 |
Correct |
43 ms |
94276 KB |
Output is correct |
9 |
Correct |
42 ms |
94292 KB |
Output is correct |
10 |
Correct |
40 ms |
94288 KB |
Output is correct |
11 |
Correct |
43 ms |
94220 KB |
Output is correct |
12 |
Correct |
41 ms |
94288 KB |
Output is correct |
13 |
Correct |
38 ms |
94288 KB |
Output is correct |
14 |
Correct |
42 ms |
94240 KB |
Output is correct |
15 |
Correct |
47 ms |
94288 KB |
Output is correct |
16 |
Correct |
42 ms |
94328 KB |
Output is correct |
17 |
Correct |
42 ms |
94288 KB |
Output is correct |
18 |
Correct |
43 ms |
94300 KB |
Output is correct |
19 |
Correct |
40 ms |
94296 KB |
Output is correct |
20 |
Correct |
42 ms |
94288 KB |
Output is correct |
21 |
Correct |
42 ms |
94148 KB |
Output is correct |
22 |
Correct |
43 ms |
94292 KB |
Output is correct |
23 |
Correct |
39 ms |
94340 KB |
Output is correct |
24 |
Correct |
45 ms |
94292 KB |
Output is correct |
25 |
Correct |
39 ms |
94288 KB |
Output is correct |
26 |
Correct |
41 ms |
94292 KB |
Output is correct |
27 |
Incorrect |
52 ms |
94224 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
94288 KB |
Output is correct |
2 |
Correct |
49 ms |
94288 KB |
Output is correct |
3 |
Correct |
41 ms |
94300 KB |
Output is correct |
4 |
Correct |
43 ms |
94292 KB |
Output is correct |
5 |
Correct |
42 ms |
94288 KB |
Output is correct |
6 |
Correct |
49 ms |
94368 KB |
Output is correct |
7 |
Correct |
43 ms |
94288 KB |
Output is correct |
8 |
Correct |
43 ms |
94276 KB |
Output is correct |
9 |
Correct |
42 ms |
94292 KB |
Output is correct |
10 |
Correct |
40 ms |
94288 KB |
Output is correct |
11 |
Correct |
43 ms |
94220 KB |
Output is correct |
12 |
Correct |
41 ms |
94288 KB |
Output is correct |
13 |
Correct |
38 ms |
94288 KB |
Output is correct |
14 |
Correct |
42 ms |
94240 KB |
Output is correct |
15 |
Correct |
47 ms |
94288 KB |
Output is correct |
16 |
Correct |
42 ms |
94328 KB |
Output is correct |
17 |
Correct |
42 ms |
94288 KB |
Output is correct |
18 |
Correct |
43 ms |
94300 KB |
Output is correct |
19 |
Correct |
40 ms |
94296 KB |
Output is correct |
20 |
Correct |
42 ms |
94288 KB |
Output is correct |
21 |
Correct |
42 ms |
94148 KB |
Output is correct |
22 |
Correct |
43 ms |
94292 KB |
Output is correct |
23 |
Correct |
39 ms |
94340 KB |
Output is correct |
24 |
Correct |
45 ms |
94292 KB |
Output is correct |
25 |
Correct |
39 ms |
94288 KB |
Output is correct |
26 |
Correct |
41 ms |
94292 KB |
Output is correct |
27 |
Incorrect |
52 ms |
94224 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
94544 KB |
Output is correct |
2 |
Correct |
45 ms |
94296 KB |
Output is correct |
3 |
Correct |
40 ms |
94168 KB |
Output is correct |
4 |
Correct |
40 ms |
94288 KB |
Output is correct |
5 |
Correct |
42 ms |
94296 KB |
Output is correct |
6 |
Correct |
57 ms |
94180 KB |
Output is correct |
7 |
Correct |
43 ms |
94392 KB |
Output is correct |
8 |
Correct |
41 ms |
94300 KB |
Output is correct |
9 |
Correct |
44 ms |
94288 KB |
Output is correct |
10 |
Correct |
40 ms |
94300 KB |
Output is correct |
11 |
Correct |
42 ms |
94288 KB |
Output is correct |
12 |
Correct |
43 ms |
94300 KB |
Output is correct |
13 |
Correct |
44 ms |
94392 KB |
Output is correct |
14 |
Incorrect |
40 ms |
94296 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
94544 KB |
Output is correct |
2 |
Correct |
45 ms |
94296 KB |
Output is correct |
3 |
Correct |
40 ms |
94168 KB |
Output is correct |
4 |
Correct |
40 ms |
94288 KB |
Output is correct |
5 |
Correct |
42 ms |
94296 KB |
Output is correct |
6 |
Correct |
57 ms |
94180 KB |
Output is correct |
7 |
Correct |
43 ms |
94392 KB |
Output is correct |
8 |
Correct |
41 ms |
94300 KB |
Output is correct |
9 |
Correct |
44 ms |
94288 KB |
Output is correct |
10 |
Correct |
40 ms |
94300 KB |
Output is correct |
11 |
Correct |
42 ms |
94288 KB |
Output is correct |
12 |
Correct |
43 ms |
94300 KB |
Output is correct |
13 |
Correct |
44 ms |
94392 KB |
Output is correct |
14 |
Incorrect |
40 ms |
94296 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
94544 KB |
Output is correct |
2 |
Correct |
45 ms |
94296 KB |
Output is correct |
3 |
Correct |
40 ms |
94168 KB |
Output is correct |
4 |
Correct |
40 ms |
94288 KB |
Output is correct |
5 |
Correct |
42 ms |
94296 KB |
Output is correct |
6 |
Correct |
57 ms |
94180 KB |
Output is correct |
7 |
Correct |
43 ms |
94392 KB |
Output is correct |
8 |
Correct |
41 ms |
94300 KB |
Output is correct |
9 |
Correct |
44 ms |
94288 KB |
Output is correct |
10 |
Correct |
40 ms |
94300 KB |
Output is correct |
11 |
Correct |
42 ms |
94288 KB |
Output is correct |
12 |
Correct |
43 ms |
94300 KB |
Output is correct |
13 |
Correct |
44 ms |
94392 KB |
Output is correct |
14 |
Incorrect |
40 ms |
94296 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |