Problem p02579 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int n, m;

bool safe(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }

int main() {
  // freopen("input.txt", "r", stdin);

  vector<string> grid;
  int sx, sy;
  int tx, ty;
  const int dx[4] = {0, 0, -1, 1};
  const int dy[4] = {-1, 1, 0, 0};
  const int mx[20] = {-2, -1, 0,  1, 2, -2, -1, 1, 2, -2,
                      2,  -2, -1, 1, 2, -2, -1, 0, 1, 2};
  const int my[20] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
                      0,  1,  1,  1,  1,  2,  2,  2,  2,  2};

  scanf("%d %d %d %d %d %d", &n, &m, &sx, &sy, &tx, &ty);
  sx--;
  sy--;
  tx--;
  ty--;

  for (int i = 0; i < n; i++) {
    char input[1001];

    scanf("%s", input);
    string str = input;

    grid.push_back(str);
  }

  priority_queue<pair<int, pair<int, int>>> pq;

  int sol[1001][1001];

  memset(sol, -1, sizeof(sol));

  sol[sx][sy] = 0;

  pq.push({0, {sx, sy}});

  while (!pq.empty()) {
    const int val = -pq.top().first;
    const int x = pq.top().second.first;
    const int y = pq.top().second.second;
    pq.pop();

    if ((x == tx && y == ty) || val > sol[x][y]) {
      continue;
    }

    for (int i = 0; i < 4; i++) {
      const int nx = x + dx[i];
      const int ny = y + dy[i];

      if (safe(nx, ny) && grid[nx][ny] == '.' &&
          (sol[nx][ny] == -1 || val < sol[nx][ny])) {
        sol[nx][ny] = val;

        pq.push({-val, {nx, ny}});
      }
    }

    for (int i = 0; i < 20; i++) {
      const int nx = x + mx[i];
      const int ny = y + my[i];

      if (safe(nx, ny) && grid[nx][ny] == '.' &&
          (sol[nx][ny] == -1 || val + 1 < sol[nx][ny])) {
        sol[nx][ny] = val + 1;

        pq.push({-(val + 1), {nx, ny}});
      }
    }
  }

  printf("%d\\n", sol[tx][ty]);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int n, m;

bool safe(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }

int main() {
  // freopen("input.txt", "r", stdin);

  vector<string> grid;
  int sx, sy;
  int tx, ty;
  const int dx[4] = {0, 0, -1, 1};
  const int dy[4] = {-1, 1, 0, 0};
  const int mx[20] = {-2, -1, 0,  1, 2, -2, -1, 1, 2, -2,
                      2,  -2, -1, 1, 2, -2, -1, 0, 1, 2};
  const int my[20] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
                      0,  1,  1,  1,  1,  2,  2,  2,  2,  2};

  scanf("%d %d %d %d %d %d", &n, &m, &sx, &sy, &tx, &ty);
  sx--;
  sy--;
  tx--;
  ty--;

  for (int i = 0; i < n; i++) {
    char input[1001];

    scanf("%s", input);
    string str = input;

    grid.push_back(str);
  }

  priority_queue<pair<int, pair<int, int>>> pq;

  int sol[1001][1001];

  memset(sol, -1, sizeof(sol));

  sol[sx][sy] = 0;

  pq.push({0, {sx, sy}});

  while (!pq.empty()) {
    const int val = -pq.top().first;
    const int x = pq.top().second.first;
    const int y = pq.top().second.second;
    pq.pop();

    if ((x == tx && y == ty) || val > sol[x][y]) {
      continue;
    }

    for (int i = 0; i < 4; i++) {
      const int nx = x + dx[i];
      const int ny = y + dy[i];

      if (safe(nx, ny) && grid[nx][ny] == '.' && sol[nx][ny] == -1 ||
          val < sol[nx][ny]) {
        sol[nx][ny] = val;

        pq.push({-val, {nx, ny}});
      }
    }

    for (int i = 0; i < 20; i++) {
      const int nx = x + mx[i];
      const int ny = y + my[i];

      if (safe(nx, ny) && grid[nx][ny] == '.' && sol[nx][ny] == -1 ||
          val + 1 < sol[nx][ny]) {
        sol[nx][ny] = val + 1;

        pq.push({-(val + 1), {nx, ny}});
      }
    }
  }

  printf("%d\\n", sol[tx][ty]);

  return 0;
}