Problem p02579 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define lint long long
#define st first
#define nd second
#define INF 0x3f3f3f3f
#define N 103
using namespace std;

int mar[N][N];
char grid[N][N];

int id = 1;

int n, m;
void go(int i, int j, int x) {
  if (i < 0 || i >= n)
    return;
  if (j < 0 || j >= m)
    return;
  if (grid[i][j] == '#')
    return;
  if (mar[i][j] != 0)
    return;
  mar[i][j] = x;
  go(i + 1, j, x);
  go(i - 1, j, x);
  go(i, j + 1, x);
  go(i, j - 1, x);
}

int bi, bj;
int ei, ej;

int dist[N][N];
bool vis[N][N];
typedef pair<int, int> pii;
typedef pair<int, pii> piii;

const int inf = 5000000;

bool valid(int i, int j) {
  if (i < 0 || i >= n)
    return 0;
  if (j < 0 || j >= m)
    return 0;
  if (grid[i][j] == '#')
    return 0;
  if (vis[i][j])
    return 0;
  return 1;
}

int dij() {
  if (!valid(bi, bj))
    return inf;
  dist[bi][bj] = 0;
  priority_queue<piii, vector<piii>, greater<piii>> q;
  q.push({0, {bi, bj}});
  while (!q.empty()) {
    int x, y;
    tie(x, y) = q.top().nd;
    int d = q.top().st;
    vis[x][y] = 1;
    if (tie(x, y) == tie(ei, ej))
      return dist[ei][ej];
    q.pop();

    for (int i = x - 2; i <= x + 2; i++) {
      for (int j = y - 2; j <= y + 2; j++) {
        if (!valid(i, j))
          continue;
        int d2 = d + (mar[x][y] != mar[i][j]);
        if (d2 < dist[i][j]) {
          dist[i][j] = d2;
          q.push({d2, {i, j}});
        }
      }
    }
  }

  return dist[ei][ej];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  cin >> n >> m;
  cin >> bi >> bj >> ei >> ej;
  bi--;
  bj--;
  ei--;
  ej--;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < m; j++)
      grid[i][j] = s[j];
  }
  int t = 1;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      dist[i][j] = inf;
      if (grid[i][j] == '.' && mar[i][j] == 0) {
        go(i, j, t);
        t++;
      }
    }
  auto aux = dij();

  if (aux == inf)
    cout << "-1\n";
  else
    cout << aux << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define lint long long
#define st first
#define nd second
#define INF 0x3f3f3f3f
#define N 1003
using namespace std;

int mar[N][N];
char grid[N][N];

int id = 1;

int n, m;
void go(int i, int j, int x) {
  if (i < 0 || i >= n)
    return;
  if (j < 0 || j >= m)
    return;
  if (grid[i][j] == '#')
    return;
  if (mar[i][j] != 0)
    return;
  mar[i][j] = x;
  go(i + 1, j, x);
  go(i - 1, j, x);
  go(i, j + 1, x);
  go(i, j - 1, x);
}

int bi, bj;
int ei, ej;

int dist[N][N];
bool vis[N][N];
typedef pair<int, int> pii;
typedef pair<int, pii> piii;

const int inf = 5000000;

bool valid(int i, int j) {
  if (i < 0 || i >= n)
    return 0;
  if (j < 0 || j >= m)
    return 0;
  if (grid[i][j] == '#')
    return 0;
  if (vis[i][j])
    return 0;
  return 1;
}

int dij() {
  if (!valid(bi, bj))
    return inf;
  dist[bi][bj] = 0;
  priority_queue<piii, vector<piii>, greater<piii>> q;
  q.push({0, {bi, bj}});
  while (!q.empty()) {
    int x, y;
    tie(x, y) = q.top().nd;
    int d = q.top().st;
    vis[x][y] = 1;
    if (tie(x, y) == tie(ei, ej))
      return dist[ei][ej];
    q.pop();

    for (int i = x - 2; i <= x + 2; i++) {
      for (int j = y - 2; j <= y + 2; j++) {
        if (!valid(i, j))
          continue;
        int d2 = d + (mar[x][y] != mar[i][j]);
        if (d2 < dist[i][j]) {
          dist[i][j] = d2;
          q.push({d2, {i, j}});
        }
      }
    }
  }

  return dist[ei][ej];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  cin >> n >> m;
  cin >> bi >> bj >> ei >> ej;
  bi--;
  bj--;
  ei--;
  ej--;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < m; j++)
      grid[i][j] = s[j];
  }
  int t = 1;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      dist[i][j] = inf;
      if (grid[i][j] == '.' && mar[i][j] == 0) {
        go(i, j, t);
        t++;
      }
    }
  auto aux = dij();

  if (aux == inf)
    cout << "-1\n";
  else
    cout << aux << "\n";
  return 0;
}