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

#include <bits/stdc++.h>
using namespace std;
#define intt long long int
#define intu unsigned long long int
#define vi vector<int>
#define ii pair<int, int>
#define pb push_back
#define ff first
#define ss second
#define fast_io                                                                \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(NULL);                                                               \\
  std::cout.tie(NULL);
#define PI 3.14159265358979323846
#define all(a) a.begin(), a.end()
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define bloop(i, a, b) for (int i = a; i >= b; i--)
#define tc(t)                                                                  \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
int mod = 1000000007;
intt gcd(intt a, intt b) { return b ? gcd(b, a % b) : a; }
intt lcm(intt a, intt b) { return a * b / gcd(a, b); }
intt bpow(intt a, intt b) {
  intt res = 1;
  while (b > 0) {
    if (b & 1)
      res = ((res) * (a)) % mod;
    a = ((a) * (a)) % mod;
    b >>= 1;
  }
  return res;
}
intt fact(intt n) {
  if ((n == 0) || (n == 1))
    return 1;
  else
    return n * fact(n - 1);
}
void go() {
#ifndef ONLINE_JUDGE
  freopen("inn.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
}
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

void dfs(vector<string> grid, vector<vector<int>> &dist, int sx, int sy, int ex,
         int ey, int n, int m) {
  deque<ii> dq;
  dq.push_front({sx, sy});
  dist[sx][sy] = 0;
  while (!dq.empty()) {
    int x = dq.front().ff;
    int y = dq.front().ss;
    dq.pop_front();
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx >= 0 && ny >= 0 && nx < n && ny < m && grid[nx][ny] == '.') {
        if (dist[nx][ny] > dist[x][y]) {
          dist[nx][ny] = dist[x][y];
          dq.push_front({nx, ny});
        }
      }
    }
    for (int i = x - 2; i <= x + 2; i++) {
      for (int j = y - 2; j <= y + 2; j++) {
        if (i >= 0 && j >= 0 && i < n && j < m && grid[i][j] == '.') {
          if (dist[i][j] > dist[x][y] + 1) {
            dist[i][j] = dist[x][y] + 1;
            dq.push_back({i, j});
          } else
            continue;
        }
      }
    }
  }
}

int main() {

  go();
  fast_io int n, m;
  cin >> n >> m;
  vector<string> graph(n);
  vector<vector<int>> dist(n, vector<int>(m, 1e7));
  int sx, sy, ex, ey;
  cin >> sx >> sy >> ex >> ey;
  sx--;
  sy--;
  ex--;
  ey--;
  for0(i, n) { cin >> graph[i]; }

  dfs(graph, dist, sx, sy, ex, ey, n, m);
  if (dist[ex][ey] >= 1e7)
    cout << "-1"
         << "\\n";
  else
    cout << dist[ex][ey] << "\\n";
}

/*
3
17
4
*/


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define intt long long int
#define intu unsigned long long int
#define vi vector<int>
#define ii pair<int, int>
#define pb push_back
#define ff first
#define ss second
#define fast_io                                                                \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(NULL);                                                               \\
  std::cout.tie(NULL);
#define PI 3.14159265358979323846
#define all(a) a.begin(), a.end()
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define bloop(i, a, b) for (int i = a; i >= b; i--)
#define tc(t)                                                                  \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
int mod = 1000000007;
intt gcd(intt a, intt b) { return b ? gcd(b, a % b) : a; }
intt lcm(intt a, intt b) { return a * b / gcd(a, b); }
intt bpow(intt a, intt b) {
  intt res = 1;
  while (b > 0) {
    if (b & 1)
      res = ((res) * (a)) % mod;
    a = ((a) * (a)) % mod;
    b >>= 1;
  }
  return res;
}
intt fact(intt n) {
  if ((n == 0) || (n == 1))
    return 1;
  else
    return n * fact(n - 1);
}
void go() {
#ifndef ONLINE_JUDGE
  freopen("ino.txt", "r", stdin);
  freopen("onc.txt", "w", stdout);
#endif
}
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
bool f = 0;

void dfs(vector<vector<char>> graph, vector<vector<bool>> &vis, int sx, int sy,
         int ex, int ey, int temp, int &move, int n, int m) {

  vis[sx][sy] = 1;

  int diffx = abs(ex - sx);
  int diffy = abs(ey - sy);

  if (diffx <= 2 && diffy <= 2) {
    if (move == 0)
      move = temp;
    else
      move = min(move, temp);
    f = 1;
    return;
  }

  for0(i, 4) {
    int nx = sx + dx[i];
    int ny = sy + dy[i];
    if (nx >= 1 && ny >= 1 && nx <= n && ny <= m && !vis[nx][ny] &&
        graph[nx][ny] == '.') {
      vis[nx][ny] = 1;
      dfs(graph, vis, nx, ny, ex, ey, temp, move, n, m);
    }
  }
  for (int i = sx - 2; i <= sx + 2; i++) {
    for (int j = sy - 2; j <= sy + 2; j++) {
      if (i >= 1 && j >= 1 && i <= n && j <= m && !vis[i][j] &&
          graph[i][j] == '.') {
        vis[i][j] = 1;
        temp++;
        dfs(graph, vis, i, j, ex, ey, temp, move, n, m);
        temp--;
      }
    }
  }
}

int main() {

  // go();
  fast_io int n, m;
  cin >> n >> m;
  vector<vector<char>> graph(n + 1, vector<char>(m + 1));
  vector<vector<bool>> vis(n + 1, vector<bool>(m + 1, false));
  int sx, sy, ex, ey;
  cin >> sx >> sy >> ex >> ey;
  for1(i, n) {
    for1(j, n) { cin >> graph[i][j]; }
  }
  int move = 0;
  dfs(graph, vis, sx, sy, ex, ey, 0, move, n, m);
  if (f)
    cout << move << "\\n";
  else
    cout << "-1"
         << "\\n";
}

/*
3
17
4
*/