This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using pint = array <int, 2>;
const int INF = 1e9;
vector <vector <int>> ans, G;
vector <vector <pint>> Gelm;
int r, c, n, gno = 0, dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
vector <int> Gvst;
vector <string> A;
deque <pint> dq;
pint s, g;
struct segtree { // +1
int mxX = 1, mxY = 1;
vector <vector <pint>> node;
void init(){
while (mxX < r) mxX *= 2;
while (mxY < c) mxY *= 2;
node.resize(2 * mxX + 1, vector <pint> (2 * mxY + 1, (pint) {INF, INF}));
}
void update(int x, int y, pint val){
x++, y++;
x += mxX - 1, y += mxY - 1;
node[x][y] = val;
for (int cy = y / 2; cy > 0; cy /= 2){
node[x][cy] = min(node[x][2 * cy], node[x][2 * cy + 1]);
}
for (int cx = x / 2; cx > 0; cx /= 2){
for (int cy = y; cy > 0; cy /= 2){
node[cx][cy] = min(node[cx * 2][cy], node[cx * 2 + 1][cy]);
}
}
}
pint query(int L1, int R1, int L2, int R2){
L1++, R1++, L2++, R2++;
pint ret = {INF, INF};
for (L1 += mxX - 1, R1 += mxX - 1; L1 <= R1; L1 /= 2, R1 /= 2){
if (L1 % 2 == 1){
for (int cL2 = L2 + mxY - 1, cR2 = R2 + mxY - 1; cL2 <= cR2; cL2 /= 2, cR2 /= 2){
if (cL2 % 2 == 1) ret = min(ret, node[L1][cL2++]);
if (cR2 % 2 == 0) ret = min(ret, node[L1][cR2--]);
}
L1++;
}
if (R1 % 2 == 0){
for (int cL2 = L2 + mxY - 1, cR2 = R2 + mxY - 1; cL2 <= cR2; cL2 /= 2, cR2 /= 2){
if (cL2 % 2 == 1) ret = min(ret, node[R1][cL2++]);
if (cR2 % 2 == 0) ret = min(ret, node[R1][cR2--]);
}
R1--;
}
}
return ret;
}
} tree;
void val_up(int L1, int R1, int L2, int R2, int val){
L1 = max(L1, 0), R1 = min(R1, r - 1), L2 = max(L2, 0), R2 = min(R2, c - 1);
while (1){
pint ret = tree.query(L1, R1, L2, R2);
if (ret == (pint) {INF, INF}) break;
ans[ret[0]][ret[1]] = val;
tree.update(ret[0], ret[1], (pint) {INF, INF});
dq.push_back(ret);
}
}
int main()
{
scanf("%d %d %d", &r, &c, &n);
A.resize(r);
ans.resize(r, vector <int> (c, -1));
G.resize(r, vector <int> (c, -1));
scanf("%d %d %d %d", &s[0], &s[1], &g[0], &g[1]);
s[0]--, s[1]--, g[0]--, g[1]--;
for (int i = 0; i < r; i++) cin >> A[i];
for (int i = 0; i < r; i++){
for (int j = 0; j < c; j++){
if (G[i][j] != -1) continue;
if (A[i][j] == '#') continue;
Gelm.push_back(vector <pint> {});
queue <pint> q;
G[i][j] = gno;
Gelm[gno].push_back({i, j});
q.push({i, j});
while (!q.empty()){
pint now = q.front(); q.pop();
for (int k : {0, 1, 2, 3}){
int nx = now[0] + dx[k], ny = now[1] + dy[k];
if (nx < 0 || ny < 0 || nx >= r || ny >= c) continue;
if (G[nx][ny] != -1) continue;
if (A[nx][ny] == '#') continue;
G[nx][ny] = gno;
Gelm[gno].push_back({nx, ny});
q.push({nx, ny});
}
}
gno++;
}
}
Gvst.resize(gno, 0);
tree.init();
for (int i = 0; i < r; i++){
for (int j = 0; j < c; j++){
if (A[i][j] != '#') continue;
tree.update(i, j, {i, j});
}
}
ans[s[0]][s[1]] = 0;
dq.push_front(s);
while (!dq.empty()){
pint now = dq.front(); dq.pop_front();
for (int k : {0, 1, 2, 3}){
int nx = now[0] + dx[k], ny = now[1] + dy[k];
if (nx < 0 || ny < 0 || nx >= r || ny >= c) continue;
if (A[nx][ny] == '#' || Gvst[G[nx][ny]] == 1) continue;
for (pint j : Gelm[G[nx][ny]]){
if (ans[j[0]][j[1]] == -1){
ans[j[0]][j[1]] = ans[now[0]][now[1]];
dq.push_front(j);
}
}
Gvst[G[nx][ny]] = 1;
}
val_up(now[0] - n, now[0] + n, now[1] - n + 1, now[1] + n - 1, ans[now[0]][now[1]] + 1);
val_up(now[0] - n + 1, now[0] + n - 1, now[1] + n, now[1] + n, ans[now[0]][now[1]] + 1);
val_up(now[0] - n + 1, now[0] + n - 1, now[1] - n, now[1] - n, ans[now[0]][now[1]] + 1);
}
printf("%d\n", ans[g[0]][g[1]]);
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:70:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | scanf("%d %d %d", &r, &c, &n);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:74:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
74 | scanf("%d %d %d %d", &s[0], &s[1], &g[0], &g[1]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |