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

#include <bits/stdc++.h>
using namespace std;
using p = pair<int, int>;
const int INF = 10000000;

vector<vector<char>> maze;
vector<vector<int>> cost;
queue<p> que_a, que_b;
int h, w, ch, cw, dh, dw;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

void bfs_a() {
  while (que_a.size() > 0) {
    p r = que_a.front();
    que_b.push(r);
    que_a.pop();
    for (int i = 0; i < 4; i++) {
      int nx = r.first + dx[i], ny = r.second + dy[i];
      if (0 <= nx && nx < h && 0 <= ny && ny < w && maze[nx][ny] != '#' &&
          cost[nx][ny] == INF) {
        cost[nx][ny] = cost[r.first][r.second];
        que_a.push(p(nx, ny));
      }
    }
  }
}

void warp_b() {
  while (que_b.size() > 0) {
    p r = que_b.front();
    que_b.pop();
    for (int x = -2; x <= 2; x++) {
      for (int y = -2; y <= 2; y++) {
        int nx = r.first + x, ny = r.second + y;
        if (0 <= nx && nx < h && 0 <= ny && ny < w && maze[nx][ny] != '#' &&
            cost[nx][ny] == INF) {
          cost[nx][ny] = cost[r.first][r.second] + 1;
          que_a.push(p(nx, ny));
        }
      }
    }
  }
}

int main() {
  cin >> h >> w >> ch >> cw >> dh >> dw;
  vector<char> tmp(w);
  vector<int> tmp2(w, INF);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> tmp[j];
    }
    maze.push_back(tmp);
    cost.push_back(tmp2);
  }
  que_a.push(p(ch - 1, cw - 1));
  cost[ch - 1][cw - 1] = 0;

  while (que_a.size() > 0) {
    bfs_a();
    warp_b();
  }

  if (cost[dh - 1][dw - 1] == INF)
    puts("-1");
  else
    cout << cost[dh - 1][dw - 1] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using p = pair<int, int>;
const int INF = 1000000;

void bfs_a();
void warp_a();

vector<vector<char>> maze;
vector<vector<int>> cost;
queue<p> que_a, que_b;
int h, w, ch, cw, dh, dw;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0 - 1};

void bfs_a() {
  while (que_a.size() > 0) {
    p r = que_a.front();
    que_a.pop();
    for (int i = 0; i < 4; i++) {
      int nx = r.first + dx[i], ny = r.second + dy[i];
      if (0 <= nx && nx < h && 0 <= ny && ny < w && maze[nx][ny] != '#' &&
          cost[nx][ny] == INF) {
        cost[nx][ny] = cost[r.first][r.second];
        que_a.push(p(nx, ny));
        que_b.push(p(nx, ny));
      }
    }
  }
}

void warp_b() {
  while (que_b.size() > 0) {
    p r = que_b.front();
    que_b.pop();
    for (int x = -2; x <= 2; x++) {
      for (int y = -2; y <= 2; y++) {
        int nx = r.first + x, ny = r.second + y;
        if (0 <= nx && nx < h && 0 <= ny && ny < w && maze[nx][ny] != '#' &&
            cost[nx][ny] == INF) {
          cost[nx][ny] = cost[r.first][r.second] + 1;
          que_a.push(p(nx, ny));
        }
      }
    }
  }
}

int main() {
  cin >> h >> w >> ch >> cw >> dh >> dw;
  vector<char> tmp(w);
  vector<int> tmp2(w, INF);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> tmp[j];
    }
    maze.push_back(tmp);
    cost.push_back(tmp2);
  }
  que_a.push(p(ch - 1, cw - 1));
  que_b.push(p(ch - 1, cw - 1));
  cost[ch - 1][cw - 1] = 0;

  while (que_a.size() > 0) {
    bfs_a();
    warp_b();
  }
  if (cost[dh - 1][dw - 1] == INF)
    puts("-1");
  else
    cout << cost[dh - 1][dw - 1] << endl;
  return 0;
}