Problem p02644 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
using piii = pair<int, pair<int, int>>;
const int N = 1000005;
char g[N];
// char g[N][N];

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

bool check(vector<vector<int>> &g, int y, int x, int qy, int qx) {
  if (y == qy) {
    if (x < qx)
      swap(x, qx);
    for (int i = qx; i <= x; ++i)
      if (g[y][i] == '@')
        return false;
  } else {
    if (y < qy)
      swap(y, qy);
    for (int i = qy; i <= y; ++i)
      if (g[i][x] == '@')
        return false;
  }
}

int h, w, k;
int xa, ya, xb, yb;
// xa, ya
int dijkstra(vector<vector<char>> &g, vector<vector<int>> &st,
             pair<int, int> s) {
  vector<vector<int>> dist(h, vector<int>(w, 0x3f3f3f3f));
  priority_queue<piii, vector<piii>, greater<piii>> heap;
  heap.push({0, s});
  // cout << h << "---" << w << endl;
  // cout << s.second << "---" << s.first << endl;
  dist[s.first][s.second] = 0;

  while (heap.size()) {
    auto t = heap.top();
    heap.pop();

    int dis = t.first, x = t.second.first, y = t.second.second;
    if (x == xb && y == yb)
      return dis;
    if (st[x][y])
      continue;
    st[x][y] = true;

    for (int i = 0; i < 4; ++i) {
      for (int j = 1; j <= k; ++j) {
        int qy = y + dy[i] * j;
        int qx = x + dx[i] * j;
        if (qy < 0 || qy >= w || qx < 0 || qx >= h || g[qx][qy] == '@')
          break;
        // 中间也需要检测一下
        // if (!check(g, y, x, qy, qx)) continue;
        if (dist[qx][qy] > dist[x][y] + 1) {
          // printf("(%d, %d)\n", qy, qx);
          dist[qx][qy] = dis + 1;
          heap.push({dist[qx][qy], {qx, qy}});
        } else if (dist[qx][qy] <= dist[x][y])
          break;
      }
    }
  }
  if (dist[xb][yb] == 0x3f3f3f3f)
    return -1;
  return dist[xb][yb];
}

int main() {
  freopen("in.txt", "r", stdin);
  scanf("%d%d%d", &h, &w, &k);

  scanf("%d%d%d%d", &xa, &ya, &xb, &yb);

  --xa, --ya;
  --xb, --yb;

  vector<vector<char>> maze(h, vector<char>(w, 0));
  vector<vector<int>> st(h, vector<int>(w, 0));

  for (int i = 0; i < h; ++i) {
    scanf("%s", g);
    for (int j = 0; j < w; ++j)
      maze[i][j] = g[j];
  }

  // check(maze, 0, 3, 1, 3);

  int res = dijkstra(maze, st, {xa, ya});

  printf("%d\n", res);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
using piii = pair<int, pair<int, int>>;
const int N = 1000005;
char g[N];
// char g[N][N];

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

bool check(vector<vector<int>> &g, int y, int x, int qy, int qx) {
  if (y == qy) {
    if (x < qx)
      swap(x, qx);
    for (int i = qx; i <= x; ++i)
      if (g[y][i] == '@')
        return false;
  } else {
    if (y < qy)
      swap(y, qy);
    for (int i = qy; i <= y; ++i)
      if (g[i][x] == '@')
        return false;
  }
}

int h, w, k;
int xa, ya, xb, yb;
// xa, ya
int dijkstra(vector<vector<char>> &g, vector<vector<int>> &st,
             pair<int, int> s) {
  vector<vector<int>> dist(h, vector<int>(w, 0x3f3f3f3f));
  priority_queue<piii, vector<piii>, greater<piii>> heap;
  heap.push({0, s});
  // cout << h << "---" << w << endl;
  // cout << s.second << "---" << s.first << endl;
  dist[s.first][s.second] = 0;

  while (heap.size()) {
    auto t = heap.top();
    heap.pop();

    int dis = t.first, x = t.second.first, y = t.second.second;
    if (x == xb && y == yb)
      return dis;
    if (st[x][y])
      continue;
    st[x][y] = true;

    for (int i = 0; i < 4; ++i) {
      for (int j = 1; j <= k; ++j) {
        int qy = y + dy[i] * j;
        int qx = x + dx[i] * j;
        if (qy < 0 || qy >= w || qx < 0 || qx >= h || g[qx][qy] == '@')
          break;
        // 中间也需要检测一下
        // if (!check(g, y, x, qy, qx)) continue;
        if (dist[qx][qy] > dist[x][y] + 1) {
          // printf("(%d, %d)\n", qy, qx);
          dist[qx][qy] = dis + 1;
          heap.push({dist[qx][qy], {qx, qy}});
        } else if (dist[qx][qy] <= dist[x][y])
          break;
      }
    }
  }
  if (dist[xb][yb] == 0x3f3f3f3f)
    return -1;
  return dist[xb][yb];
}

int main() {
  // freopen("in.txt", "r", stdin);
  scanf("%d%d%d", &h, &w, &k);

  scanf("%d%d%d%d", &xa, &ya, &xb, &yb);

  --xa, --ya;
  --xb, --yb;

  vector<vector<char>> maze(h, vector<char>(w, 0));
  vector<vector<int>> st(h, vector<int>(w, 0));

  for (int i = 0; i < h; ++i) {
    scanf("%s", g);
    for (int j = 0; j < w; ++j)
      maze[i][j] = g[j];
  }

  // check(maze, 0, 3, 1, 3);

  int res = dijkstra(maze, st, {xa, ya});

  printf("%d\n", res);

  return 0;
}