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

#include <bits/stdc++.h>
using namespace std;

#define mx 1005
#define ll long long
#define inf 10000000

void IO() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
}

int ar[mx];
char ch[mx][mx], ch1[mx][mx];
int dis[mx][mx];
int m, n, k, ii;

int xx[] = {1, -1, 0, 0};
int yy[] = {0, 0, 1, -1};
int xxx[] = {1, -1, 2,  -2, 0,  0,  0, 0, 1,  2,  -1, -2,
             1, 2,  -1, -2, -1, -2, 1, 2, -1, -2, 1,  2};
int yyy[] = {0,  0,  0, 0, 1,  -1, 2, -2, 1, 2, -1, -2,
             -1, -2, 1, 2, -2, -1, 2, 1,  2, 1, -2, -1};

bool chk(int x, int y) {
  if (x > 0 && x <= n && y > 0 && y <= m && ch[x][y] == '.')
    return true;
  return false;
}

void bfs(int x, int y) {

  dis[x][y] = 0;
  priority_queue<pair<int, pair<int, int>>> q;
  q.push({0, {x, y}});
  while (!q.empty()) {
    //  k++;
    int val = q.top().first;
    val *= -1;
    x = q.top().second.first;
    y = q.top().second.second;
    q.pop();
    if (dis[x][y] < val)
      continue;
    for (int i = 0; i < 4; i++) {
      int temx = x + xx[i];
      int temy = y + yy[i];
      if (chk(temx, temy) == false)
        continue;
      if (dis[temx][temy] > dis[x][y]) {
        dis[temx][temy] = dis[x][y];
        q.push({-dis[x][y], {temx, temy}});
      }
    }
    for (int i = 0; i < 24; i++) {
      int temx = x + xxx[i];
      int temy = y + yyy[i];
      if (chk(temx, temy) == false)
        continue;
      if (dis[temx][temy] > dis[x][y] + 1) {
        dis[temx][temy] = dis[x][y] + 1;
        q.push({-dis[x][y], {temx, temy}});
      }
    }
  }
}

void solve() {
  scanf("%d%d", &n, &m);
  int x, y, st, en;
  scanf("%d%d%d%d", &x, &y, &st, &en);
  for (int i = 0; i < n; i++)
    scanf("%s", ch1[i]);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      ch[i][j] = ch1[i - 1][j - 1];
      dis[i][j] = inf;
      // cout<<ch[i][j];
    }
    // cout<<endl;
  }

  if (ch[x][y] == '.')
    bfs(x, y);
  if (dis[st][en] == inf)
    printf("-1\\n");
  else
    printf("%d\\n", dis[st][en]);
  // for(int i=0;i<24;i++)printf("%d %d\\n",xxx[i],yyy[i]);
  // cout<<k<<endl;
}

int main() {
  IO();
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define mx 1005
#define ll long long
#define inf 10000000

void IO() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
}

int ar[mx];
char ch[mx][mx], ch1[mx][mx];
int dis[mx][mx];
int m, n, k, ii;

int xx[] = {1, -1, 0, 0};
int yy[] = {0, 0, 1, -1};
int xxx[] = {1, -1, 2,  -2, 0,  0,  0, 0, 1,  2,  -1, -2,
             1, 2,  -1, -2, -1, -2, 1, 2, -1, -2, 1,  2};
int yyy[] = {0,  0,  0, 0, 1,  -1, 2, -2, 1, 2, -1, -2,
             -1, -2, 1, 2, -2, -1, 2, 1,  2, 1, -2, -1};

bool chk(int x, int y) {
  if (x > 0 && x <= n && y > 0 && y <= m && ch[x][y] == '.')
    return true;
  return false;
}

void bfs(int x, int y) {

  dis[x][y] = 0;
  priority_queue<pair<int, pair<int, int>>> q;
  q.push({0, {x, y}});
  while (!q.empty()) {
    int val = q.top().first;
    val *= -1;
    x = q.top().second.first;
    y = q.top().second.second;
    q.pop();
    if (dis[x][y] < val)
      continue;
    for (int i = 0; i < 4; i++) {
      int temx = x + xx[i];
      int temy = y + yy[i];
      if (chk(temx, temy) == false)
        continue;
      if (dis[temx][temy] > dis[x][y]) {
        dis[temx][temy] = dis[x][y];
        q.push({-dis[x][y], {temx, temy}});
      }
    }
    for (int i = 0; i < 25; i++) {
      int temx = x + xxx[i];
      int temy = y + yyy[i];
      if (chk(temx, temy) == false)
        continue;
      if (dis[temx][temy] > dis[x][y] + 1) {
        dis[temx][temy] = dis[x][y] + 1;
        q.push({-dis[x][y], {temx, temy}});
      }
    }
  }
}

void solve() {
  scanf("%d%d", &n, &m);
  int x, y, st, en;
  scanf("%d%d%d%d", &x, &y, &st, &en);
  for (int i = 0; i < n; i++)
    scanf("%s", ch1[i]);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      ch[i][j] = ch1[i - 1][j - 1];
      dis[i][j] = inf;
      // cout<<ch[i][j];
    }
    // cout<<endl;
  }

  if (ch[x][y] == '.')
    bfs(x, y);
  if (dis[st][en] == inf)
    printf("-1\\n");
  else
    printf("%d\\n", dis[st][en]);
}

int main() {
  IO();
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}