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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int minDist(const vector<string> &plane, char wall, int y1, int x1, int y2,
            int x2) {
  int dy[] = {0, 0, -1, 1};
  int dx[] = {-1, 1, 0, 0};

  int h = plane.size();
  int w = plane[0].size();

  queue<pair<int, int>> q;
  vector<vector<bool>> check(h, vector<bool>(w, false));
  q.push(make_pair(y1, x1));
  check[y1][x1] = true;
  int ret = 0;
  while (!q.empty()) {
    queue<pair<int, int>> q1;
    while (!q.empty()) {
      int y = q.front().first;
      int x = q.front().second;
      q.pop();
      if (y == y2 && x == x2)
        return ret;
      for (int i = 0; i < 4; ++i) {
        int y0 = y + dy[i];
        int x0 = x + dx[i];
        if (0 <= y0 && y0 < h && 0 <= x0 && x0 < w && plane[y0][x0] != wall &&
            !check[y0][x0]) {
          q1.push(make_pair(y0, x0));
          check[y0][x0] = true;
        }
      }
    }
    ++ret;
    q = q1;
  }
  return -1;
}

int main() {
  int w, h, n;
  cin >> w >> h >> n;

  vector<string> s(2 * h + 1, string(2 * w + 1, '#'));
  for (int i = 1; i < 2 * h; ++i) {
    for (int j = 1; j < 2 * w; ++j) {
      if (i % 2 == 1 || j % 2 == 1)
        s[i][j] = ' ';
    }
  }

  for (int i = 0; i < n; ++i) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;

    if (x1 == x2) {
      if (y1 > y2)
        swap(y1, y2);
      for (int i = y1; i < y2; ++i)
        s[2 * i + 1][2 * x1] = '#';
    } else {
      if (x1 > x2)
        swap(x1, x2);
      for (int i = x1; i < x2; ++i)
        s[2 * y1][2 * i + 1] = '#';
    }
  }

  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;

  int ret = 0;
  for (int i = 1; i < 2 * h; ++i) {
    for (int j = 1; j < 2 * w; ++j) {
      if ((i + j) % 2 == 0 || s[i][j] == '#')
        continue;
      s[i][j] = '#';
      ret = max(
          ret,
          minDist(s, '#', 2 * sy + 1, 2 * sx + 1, 2 * gy + 1, 2 * gx + 1) / 2);
      s[i][j] = ' ';
    }
  }

  if (ret != 0)
    ret -= minDist(s, '#', 2 * sy + 1, 2 * sx + 1, 2 * gy + 1, 2 * gx + 1) / 2;
  cout << ret << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int minDist(const vector<string> &plane, char wall, int y1, int x1, int y2,
            int x2) {
  int dy[] = {0, 0, -1, 1};
  int dx[] = {-1, 1, 0, 0};

  int h = plane.size();
  int w = plane[0].size();

  queue<pair<int, int>> q;
  vector<vector<bool>> check(h, vector<bool>(w, false));
  q.push(make_pair(y1, x1));
  check[y1][x1] = true;
  int ret = 0;
  while (!q.empty()) {
    queue<pair<int, int>> q1;
    while (!q.empty()) {
      int y = q.front().first;
      int x = q.front().second;
      q.pop();
      if (y == y2 && x == x2)
        return ret;
      for (int i = 0; i < 4; ++i) {
        int y0 = y + dy[i];
        int x0 = x + dx[i];
        if (0 <= y0 && y0 < h && 0 <= x0 && x0 < w && plane[y0][x0] != wall &&
            !check[y0][x0]) {
          q1.push(make_pair(y0, x0));
          check[y0][x0] = true;
        }
      }
    }
    ++ret;
    q = q1;
  }
  return -1;
}

int main() {
  int w, h, n;
  cin >> w >> h >> n;

  vector<string> s(2 * h - 1, string(2 * w - 1, ' '));
  for (int i = 0; i < h - 1; ++i) {
    for (int j = 0; j < w - 1; ++j) {
      s[2 * i + 1][2 * j + 1] = '#';
    }
  }

  for (int i = 0; i < n; ++i) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    if (x1 > x2)
      swap(x1, x2);
    if (y1 > y2)
      swap(y1, y2);

    if (x1 == x2) {
      for (int i = y1; i < y2; ++i)
        s[2 * i][2 * x1 - 1] = '#';
    } else {
      for (int i = x1; i < x2; ++i)
        s[2 * y1 - 1][2 * i] = '#';
    }
  }

  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;

  int ret = 0;
  for (int i = 0; i < 2 * h - 1; ++i) {
    for (int j = 0; j < 2 * w - 1; ++j) {
      if ((i + j) % 2 == 0 || s[i][j] == '#')
        continue;
      s[i][j] = '#';
      ret = max(ret, minDist(s, '#', 2 * sy, 2 * sx, 2 * gy, 2 * gx) / 2);
      s[i][j] = ' ';
    }
  }

  ret -= minDist(s, '#', 2 * sy, 2 * sx, 2 * gy, 2 * gx) / 2;
  cout << ret << endl;

  return 0;
}