Language: C++

#include<bits/stdc++.h>
using namespace std;
int const nax = 1e6 + 10;
char s[nax];
pair <int, int> q[nax];

int main () {
    vector <vector <int>> a;
    vector <vector <int>> dist;
    vector <vector <int>> vis;
    int n, m, k;
    scanf("%d %d %d", &n, &m, &k);
    a.resize(n);

    pair <int, int> st, ed;

    scanf("%d %d", &st.first, &st.second);
    scanf("%d %d", &ed.first, &ed.second);
    -- st.first, -- st.second;
    -- ed.first, -- ed.second;

    for (int i = 0 ; i < n ; ++ i) {
        scanf("%s", s);
        for (int j = 0 ; j < m ; ++ j) {
            a[i].push_back(s[j] == '@' ? 0 : 1);
        }
    }



    vector <vector <int> > up;
    vector <vector <int> > down;
    vector <vector <int> > left;
    vector <vector <int> > right;

    auto resize_vec = [&](vector <vector <int>> &f)->void  {
        f.resize(n);
        for (int i = 0 ; i < n ; ++ i) {
            f[i].resize(m, -1);
        }
    };

    resize_vec(up);
    resize_vec(down);
    resize_vec(left);
    resize_vec(right);
    resize_vec(dist);

    vis.resize(n);
    for (int i = 0 ; i < n ; ++ i) {
        vis[i].resize(m, false);
    }

    for (int i = 0 ; i < n ; ++ i) {
        for (int j = 0 ; j < m ; ++ j) {
            if (!a[i][j]) continue;
            if (i > 0 && a[i - 1][j]) {
                up[i][j] = i - 1;
            }

            if (i + 1 < n && a[i + 1][j]) {
                down[i][j] = i + 1;
            }

            if (j > 0 && a[i][j - 1])
                left[i][j] = j - 1;

            if (j + 1 < m && a[i][j + 1])
                right[i][j] = j + 1;
        }
    }

    auto get_neightbor = [&] (pair <int, int> p, int dir) {
        if (dir == 0) return make_pair(up[p.first][p.second], p.second);
        if (dir == 1) return make_pair(down[p.first][p.second], p.second);
        if (dir == 2) return make_pair(p.first, left[p.first][p.second]);
        return make_pair(p.first, right[p.first][p.second]);
    };



    const int inf = 1e9;
    for (int i = 0 ; i < n; ++ i)
    for (int j = 0 ; j < m; ++ j) {
        dist[i][j] = inf;
    }

    dist[st.first][st.second] = 0;

    auto sf = get_neightbor(st, 3);
   // printf("%d %d\n", sf.first, sf.second);
   // return 0;


    int topQ = 0;

    if (a[i][j]) {
    q[topQ ++] = st;
    {
            int i = st.first;
            int j = st.second;
            if (up[i][j] != -1) {
                down[up[i][j]][j] = down[i][j];
            }
            if (down[i][j] != n && down[i][j] != -1) {
                up[down[i][j]][j] = up[i][j];
            }
            if (left[i][j] != -1 ) {
                right[i][left[i][j]] = right[i][j];
            }
            if (right[i][j] != m && right[i][j] != -1) {
                left[i][right[i][j]] = left[i][j];
            }

    }
    }
    vis[st.first][st.second] = true;

    for (int g = 0 ; g < topQ ; ++ g) {

        int x = q[g].first;
        int y = q[g].second;
        int cur_dist = dist[x][y];

        if (make_pair(x, y) == ed) {
            return !printf("%d\n", dist[x][y]);
        }

        vector <pair <int, int> > to_push;

        for (int dir = 0 ; dir < 4 ; ++ dir) {
            int cur_x = x, cur_y = y;
            while (true) {
                auto u = get_neightbor(make_pair(cur_x, cur_y), dir);
                if (u.first == -1 || u.second == -1) break;

                if (abs(u.first - x) + abs(u.second - y) > k) break;
                if (!vis[u.first][u.second])
                    to_push.emplace_back(u.first, u.second);

                cur_x = u.first;
                cur_y = u.second;
            }
        }



        for (auto& p : to_push) {
            int i = p.first;
            int j = p.second;

            dist[i][j] = min(dist[i][j], cur_dist + 1);
            if (up[i][j] != -1) {
                down[up[i][j]][j] = down[i][j];
            }
            if (down[i][j] != n && down[i][j] != -1) {
                up[down[i][j]][j] = up[i][j];
            }
            if (left[i][j] != -1 ) {
                right[i][left[i][j]] = right[i][j];
            }
            if (right[i][j] != m && right[i][j] != -1) {
                left[i][right[i][j]] = left[i][j];
            }
        }

        for (auto &p : to_push) {
            if (!vis[p.first][p.second])
                q[topQ ++] = p;
            vis[p.first][p.second] = true;
        }
    }

    if (!vis[ed.first][ed.second]) {
        return !printf("-1");
    }

    printf("%d\n", dist[ed.first][ed.second]);
}


Compiler output:
a.cc: In function 'int main()':
a.cc:97:11: error: 'i' was not declared in this scope
   97 |     if (a[i][j]) {
      |           ^
a.cc:97:14: error: 'j' was not declared in this scope
   97 |     if (a[i][j]) {
      |              ^