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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int mo[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int vis[1005][1005] = {0}, vis2[1005][1005] = {0}, c[1005][1005] = {0};
char s[1005][1005];
struct node {
  int x, y;
};
int n, m, s1, e1, s2, e2;
int bfs() {
  queue<node> q1, q2;
  node a, b;
  a.x = s1, a.y = e1;
  vis[a.x][a.y] = 1;
  q1.push(a);
  while (!q1.empty() || !q2.empty()) {
    // cout<<123<<endl;
    while (!q1.empty()) {
      a = q1.front();
      // cout<<a.x<<a.y<<endl;
      q1.pop();
      if (a.x == s2 && a.y == e2) {
        return c[a.x][a.y];
      }
      for (int i = 0; i < 4; i++) {
        b.x = a.x + mo[i][0], b.y = a.y + mo[i][1];
        int flag = 0;
        if (!vis[b.x][b.y] && b.x >= 0 && b.x < n && b.y >= 0 && b.y < m &&
            s[b.x][b.y] != '#') {
          // cout<<b.x<<b.y<<endl;
          flag = 1;
          vis[b.x][b.y] = 1;
          c[b.x][b.y] = c[a.x][a.y];
          q1.push(b);
        }
        if (!flag)
          q2.push(a);
      }
    }
    while (!q2.empty()) {
      a = q2.front();
      q2.pop();
      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          if (abs(i) + abs(j) >= 2) {
            b.x = a.x + i, b.y = a.y + j;
            if (!vis[b.x][b.y] && b.x >= 0 && b.x < n && b.y >= 0 && b.y < m &&
                s[b.x][b.y] != '#') {
              vis[b.x][b.y] = 1;
              c[b.x][b.y] = c[a.x][a.y] + 1;
              // cout<<b.x<<b.y<<endl;
              q1.push(b);
            }
          }
        }
      }
    }
  }
  return -1;
}
int main() {
  cin >> n >> m >> s1 >> e1 >> s2 >> e2;
  s1--, s2--, e1--, e2--;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> s[i][j];
    }
  }
  cout << bfs();
}
/*
4 4
1 1
4 4
....
..#.
.#..
....
*/


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int mo[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int vis[1005][1005], c[1005][1005] = {0};
char s[1005][1005];
int n, m, s1, e1, s2, e2, ans = 1e6 + 5;
int bfs() {
  int q[100000][2];
  int head = 0, tail = 1;
  q[head][0] = s1, q[head][1] = e1;
  vis[s1][e1] = 0;
  while (head < tail) {
    int x1 = q[head][0], y1 = q[head][1];
    // cout<<x1<<y1<<endl;
    if (x1 == s2 && y1 == e2) {
      return c[x1][y1];
    }
    int flag = 0;
    for (int i = 0; i < 4; i++) {
      int x2 = x1 + mo[i][0], y2 = y1 + mo[i][1];
      if (!vis[x2][y2] && x2 >= 0 && x2 < n && y2 >= 0 && y2 < m &&
          s[x2][y2] != '#') {
        flag = 1;
        // cout<<'a'<<x2<<y2<<endl;
        vis[x2][y2] = 1;
        c[x2][y2] = c[x1][y1];
        q[tail][0] = x2, q[tail][1] = y2, tail++;
      }
    }
    if (!flag) {
      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          if (abs(i) + abs(j) >= 2) {
            int x2 = x1 + i, y2 = y1 + j;
            if (!vis[x2][y2] && x2 >= 0 && x2 < n && y2 >= 0 && y2 < m &&
                s[x2][y2] != '#') {
              // cout<<'b'<<x2<<y2<<endl;
              vis[x2][y2] = 1;
              c[x2][y2] = c[x1][y1] + 1;
              q[tail][0] = x2, q[tail][1] = y2, tail++;
            }
          }
        }
      }
    }
    head++;
  }
  return -1;
}
int main() {
  cin >> n >> m >> s1 >> e1 >> s2 >> e2;
  s1--, s2--, e1--, e2--;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> s[i][j];
    }
  }
  cout << bfs();
}