Problem p02579 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

#define input freopen("input.txt", "r", stdin)
#define output freopen("output.txt", "w", stdout)
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pi(a) printf("%d", a)
#define spc printf(" ")
#define bl printf("\n")
#define here(a) printf("#### %d ####", a)
#define pii pair<int, int>
#define fr(i, n) for (int i = 0; i < n; i++)
#define case(num) printf("Case %d:", num)
#define inf 1000000050
#define MAX 1010

char grid[MAX + 10][MAX + 10];
int visited[MAX + 10][MAX + 10];
int mx[4] = {-1, 0, 1, 0};
int my[4] = {0, +1, 0, -1};

vector<pii> vec[100000];
int h, w, x, y, dx, dy, p = 0, flag = 0;

void bfs(int x, int y, int pos) {
  visited[x][y] = 1;
  queue<pii> q;
  q.push(pii(x, y));
  while (!q.empty()) {
    x = q.front().first;
    y = q.front().second;
    q.pop();

    for (int i = 0; i < 4; i++) {
      int u = x + mx[i];
      int v = y + my[i];

      if (u >= 0 && u < h && v >= 0 && v < w) {
        if (visited[u][v] == 0 && grid[u][v] == '.') {
          visited[u][v] = 1;
          q.push(pii(u, v));
        } else if (grid[u][v] == '#' && visited[x][y] <= 1) {
          vec[pos].push_back(pii(x, y));
          visited[x][y] = 2;
        }
      }
    }
  }
}

void printVisited() {
  //    for(int i=0;i<h;i++){
  //        for(int j=0;j<w;j++) cout<<visited[i][j]<<" ";
  //        cout<<endl;
  //    }
  //    cout<<endl;
}

int main() {

  // input;
  // output;

  cin >> h >> w;
  cin >> x >> y;
  cin >> dx >> dy;
  x--;
  y--;
  dx--;
  dy--;
  for (int i = 0; i < h; i++)
    cin >> grid[i];

  bfs(x, y, p);

  printVisited();

  if (visited[dx][dy] >= 1) {
    cout << 0 << endl;
    return 0;
  } else {
    while (!vec[p].empty()) {
      for (int k = 0; k < vec[p].size(); k++) {
        x = vec[p][k].first;
        y = vec[p][k].second;
        // p++;
        // cout<<x<<" "<<y<<endl;
        for (int i = -2; i <= 2; i++) {
          for (int j = -2; j <= 2; j++) {
            int u = x + i;
            int v = y + j;
            // if(x==2 && y==2)
            if (u < 0 || u > h || v < 0 || v > w)
              continue;
            if (visited[u][v] == 0 && grid[u][v] == '.') {
              // cout<<u<<" -> "<<v<<endl;
              bfs(u, v, p + 1);
              if (visited[dx][dy] >= 1) {
                flag = 1;
                break;
              }
            }
          }
          printVisited();
          if (flag == 1)
            break;
        }
        if (flag == 1)
          break;
      }

      if (flag == 1)
        break;
      p++;
    }
  }

  if (visited[dx][dy] >= 1) {
    cout << p + 1 << endl;
  } else
    cout << -1 << endl;

  return 0;
}
/**
 */


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

#define input freopen("input.txt", "r", stdin)
#define output freopen("output.txt", "w", stdout)
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pi(a) printf("%d", a)
#define spc printf(" ")
#define bl printf("\n")
#define here(a) printf("#### %d ####", a)
#define pii pair<int, int>
#define fr(i, n) for (int i = 0; i < n; i++)
#define case(num) printf("Case %d:", num)
#define inf 1000000050
#define MAX 1010

char grid[MAX + 10][MAX + 10];
int visited[MAX + 10][MAX + 10];
int mx[4] = {-1, 0, 1, 0};
int my[4] = {0, +1, 0, -1};

vector<pii> vec[10000000];
int h, w, x, y, dx, dy, p = 0, flag = 0;

void bfs(int x, int y, int pos) {
  visited[x][y] = 1;
  queue<pii> q;
  q.push(pii(x, y));
  while (!q.empty()) {
    x = q.front().first;
    y = q.front().second;
    q.pop();

    for (int i = 0; i < 4; i++) {
      int u = x + mx[i];
      int v = y + my[i];

      if (u >= 0 && u < h && v >= 0 && v < w) {
        if (visited[u][v] == 0 && grid[u][v] == '.') {
          visited[u][v] = 1;
          q.push(pii(u, v));
        } else if (grid[u][v] == '#' && visited[x][y] <= 1) {
          vec[pos].push_back(pii(x, y));
          visited[x][y] = 2;
        }
      }
    }
  }
}

void printVisited() {
  //    for(int i=0;i<h;i++){
  //        for(int j=0;j<w;j++) cout<<visited[i][j]<<" ";
  //        cout<<endl;
  //    }
  //    cout<<endl;
}

int main() {

  // input;
  // output;

  cin >> h >> w;
  cin >> x >> y;
  cin >> dx >> dy;
  x--;
  y--;
  dx--;
  dy--;
  for (int i = 0; i < h; i++)
    cin >> grid[i];

  bfs(x, y, p);

  printVisited();

  if (visited[dx][dy] >= 1) {
    cout << 0 << endl;
    return 0;
  } else {
    while (!vec[p].empty()) {
      for (int k = 0; k < vec[p].size(); k++) {
        x = vec[p][k].first;
        y = vec[p][k].second;
        // p++;
        // cout<<x<<" "<<y<<endl;
        for (int i = -2; i <= 2; i++) {
          for (int j = -2; j <= 2; j++) {
            int u = x + i;
            int v = y + j;
            // if(x==2 && y==2)
            if (u < 0 || u > h || v < 0 || v > w)
              continue;
            if (visited[u][v] == 0 && grid[u][v] == '.') {
              // cout<<u<<" -> "<<v<<endl;
              bfs(u, v, p + 1);
              if (visited[dx][dy] >= 1) {
                flag = 1;
                break;
              }
            }
          }
          printVisited();
          if (flag == 1)
            break;
        }
        if (flag == 1)
          break;
      }

      if (flag == 1)
        break;
      p++;
    }
  }

  if (visited[dx][dy] >= 1) {
    cout << p + 1 << endl;
  } else
    cout << -1 << endl;

  return 0;
}
/**
 */