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

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

#define mp(x, y) make_pair(x, y)

const int maxn = 5e5 + 10;

int nex1[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}},
    nex2[20][2] = {{1, 1},   {1, -1}, {-1, 1}, {-1, -1}, {-2, -2},
                   {-2, -1}, {-2, 0}, {-2, 1}, {-2, 2},  {-1, 2},
                   {-1, -2}, {0, 2},  {0, -2}, {1, 2},   {1, -2},
                   {2, 2},   {2, 1},  {2, 0},  {2, -1},  {2, -2}};
int d[1005][1005];
char Map[10005][1005];
int n, m;
int check(int x, int y) {
  if (x <= 0 || x > n || y <= 0 || y > m || Map[x][y] == '#')
    return 0;
  return 1;
}
void bfs(int a, int b) {
  d[a][b] = 0;
  deque<pair<int, int>> que;
  que.push_back(mp(a, b));
  while (!que.empty()) {
    int x, y;
    x = que.front().first, y = que.front().second;
    que.pop_front();
    for (int i = 0; i < 4; i++) {
      int _x = x + nex1[i][0], _y = y + nex1[i][1];
      if (check(_x, _y) && d[_x][_y] > d[x][y]) {
        que.push_front(mp(_x, _y));
        d[_x][_y] = d[x][y];
      }
    }
    for (int i = 0; i < 20; i++) {
      int _x = x + nex2[i][0], _y = y + nex2[i][1];
      if (check(_x, _y) && d[_x][_y] > d[x][y] + 1) {
        d[_x][_y] = d[x][y] + 1;
        que.push_back(mp(_x, _y));
      }
    }
  }
}
void init() {
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= m; j++)
      d[i][j] = 1e9;
}
void solve() {
  cin >> n >> m;
  int x, y, tox, toy;
  cin >> x >> y >> tox >> toy;
  init();
  getchar();
  for (int i = 1; i <= n; i++)
    scanf("%s", Map[i] + 1);
  bfs(x, y);
  if (d[tox][toy] == 1e9)
    cout << -1 << endl;
  else
    cout << d[tox][toy] << endl;
}
signed main() {
  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
char ch[2006][2006];
int s[2006][2006];
int x, y, x1, y1;
int h, w;
int falg = 0;
void find1(int n, int m) {
  ch[n][m] = '1';
  if (n - 1 >= 0 && ch[n - 1][m] == '.')
    find1(n - 1, m);
  if (n + 1 < h && ch[n + 1][m] == '.')
    find1(n + 1, m);
  if (m - 1 >= 0 && ch[n][m - 1] == '.')
    find1(n, m - 1);
  if (m + 1 < w && ch[n][m + 1] == '.')
    find1(n, m + 1);
}
int find2(int n, int m) {
  if (ch[n][m] != '1')
    return 0;
  s[n][m] = 1;
  if (n - 2 >= 0 && m - 2 >= 0)
    if (ch[n - 2][m - 2] == '.') {
      ch[n - 2][m - 2] = '1';
      falg = 1;
    }
  if (n - 2 >= 0 && m - 1 >= 0)
    if (ch[n - 2][m - 1] == '.') {
      ch[n - 2][m - 1] = '1';
      falg = 1;
    }
  if (n - 2 >= 0)
    if (ch[n - 2][m] == '.') {
      ch[n - 2][m] = '1';
      falg = 1;
    }
  if (n - 2 >= 0 && m + 1 < w)
    if (ch[n - 2][m + 1] == '.') {
      ch[n - 2][m + 1] = '1';
      falg = 1;
    }
  if (n - 2 >= 0 && m + 2 < w)
    if (ch[n - 2][m + 2] == '.') {
      ch[n - 2][m + 2] = '1';
      falg = 1;
    }

  if (n - 1 >= 0 && m - 2 >= 0)
    if (ch[n - 1][m - 2] == '.') {
      ch[n - 1][m - 2] = '1';
      falg = 1;
    }
  if (n - 1 >= 0 && m - 1 >= 0)
    if (ch[n - 1][m - 1] == '.') {
      ch[n - 1][m - 1] = '1';
      falg = 1;
    }
  if (n - 1 >= 0)
    if (ch[n - 1][m] == '.') {
      ch[n - 1][m] = '1';
      falg = 1;
    }
  if (n - 1 >= 0 && m + 1 < w)
    if (ch[n - 1][m + 1] == '.') {
      ch[n - 1][m + 1] = '1';
      falg = 1;
    }
  if (n - 1 >= 0 && m + 2 < w)
    if (ch[n - 1][m + 2] == '.') {
      ch[n - 1][m + 2] = '1';
      falg = 1;
    }

  if (m - 2 >= 0)
    if (ch[n][m - 2] == '.') {
      ch[n][m - 2] = '1';
      falg = 1;
    }
  if (m - 1 >= 0)
    if (ch[n][m - 1] == '.') {
      ch[n][m - 1] = '1';
      falg = 1;
    }
  if (m + 1 < w)
    if (ch[n][m + 1] == '.') {
      ch[n][m + 1] = '1';
      falg = 1;
    }
  if (m + 2 < w)
    if (ch[n][m + 2] == '.') {
      ch[n][m + 2] = '1';
      falg = 1;
    }

  if (n + 1 < h && m - 2 >= 0)
    if (ch[n + 1][m - 2] == '.') {
      ch[n + 1][m - 2] = '1';
      falg = 1;
    }
  if (n + 1 < h && m - 1 >= 0)
    if (ch[n + 1][m - 1] == '.') {
      ch[n + 1][m - 1] = '1';
      falg = 1;
    }
  if (n + 1 < h)
    if (ch[n + 1][m] == '.') {
      ch[n + 1][m] = '1';
      falg = 1;
    }
  if (n + 1 < 0 && m + 1 < w)
    if (ch[n + 1][m + 1] == '.') {
      ch[n + 1][m + 1] = '1';
      falg = 1;
    }
  if (n + 1 < h && m + 2 < w)
    if (ch[n + 1][m + 2] == '.') {
      ch[n + 1][m + 2] = '1';
      falg = 1;
    }

  if (n + 2 < h && m - 2 >= 0)
    if (ch[n + 2][m - 2] == '.') {
      ch[n + 2][m - 2] = '1';
      falg = 1;
    }
  if (n + 2 < h && m - 1 >= 0)
    if (ch[n + 2][m - 1] == '.') {
      ch[n + 2][m - 1] = '1';
      falg = 1;
    }
  if (n + 2 < h)
    if (ch[n + 2][m] == '.') {
      ch[n + 2][m] = '1';
      falg = 1;
    }
  if (n + 2 < 0 && m + 1 < w)
    if (ch[n + 2][m + 1] == '.') {
      ch[n + 2][m + 1] = '1';
      falg = 1;
    }
  if (n + 2 < h && m + 2 < w)
    if (ch[n + 2][m + 2] == '.') {
      ch[n + 2][m + 2] = '1';
      falg = 1;
    }
}
int main() {
  //    int h,w;
  scanf("%d%d", &h, &w);
  int i;
  scanf("%d%d%d%d", &x, &y, &x1, &y1);
  for (i = 0; i < h; i++) {
    scanf("%s", ch[i]);
  }
  find1(x - 1, y - 1);
  int sum = 0;
  while (1) {
    falg = 1;
    if (ch[x1 - 1][y1 - 1] == '1')
      break;
    int j;
    falg = 0;
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        if (ch[i][j] == '1' && s[i][j] == 0)
          find2(i, j);
      }
    }
    if (falg == 0) {
      printf("-1\\n");
      break;
    }
    sum++;
  }
  if (falg == 1)
    printf("%d\\n", sum);
  return 0;
}