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

/*
14:03 - 14:24
14:24 - 14:46 debug
*/

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <set>
#include <vector>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define IINF (INT_MAX)
#define MAX 500

using namespace std;

struct Data {
  int x, y, cost;
  Data(int x = IINF, int y = IINF, int cost = IINF) : x(x), y(y), cost(cost) {}
};

int H, W, N, ix, iy, ox, oy;
int dx[] = {0, 2, 0, -2};
int dy[] = {2, 0, -2, 0};
bool G[MAX][MAX];
bool used[MAX][MAX];

void print() {
  rep(i, 2 * H + 1) {
    rep(j, 2 * W + 1) { cout << G[i][j]; }
    cout << endl;
  }
  cout << endl;
}

inline bool isValid(int nx, int ny) {
  return (0 <= nx && nx <= 2 * W && 0 <= ny && ny <= 2 * H);
}

int getValue() {
  if (ix == ox && iy == oy)
    return 0;
  rep(i, 2 * H + 1) rep(j, 2 * W + 1) used[i][j] = false;
  deque<Data> deq;
  deq.push_back(Data(ix, iy, 0));
  used[iy][ix] = true;
  while (!deq.empty()) {
    Data data = deq.front();
    deq.pop_front();
    int x = data.x, y = data.y;
    rep(i, 4) {
      int nx = x + dx[i], ny = y + dy[i];
      int mx = (x + nx) / 2, my = (y + ny) / 2;
      if (!isValid(nx, ny))
        continue;
      if (G[my][mx])
        continue;
      if (used[ny][nx])
        continue;
      used[ny][nx] = true;
      if (nx == ox && ny == oy)
        return data.cost + 1;
      deq.push_back(Data(nx, ny, data.cost + 1));
    }
  }
  return IINF;
}

int main() {

  cin >> W >> H >> N;
  rep(i, 2 * H + 1) G[i][0] = G[i][2 * W] = true;
  rep(i, 2 * W + 1) G[0][i] = G[2 * H][i] = true;
  rep(i, N) {
    int sx, sy, dx, dy;
    cin >> sx >> sy >> dx >> dy;
    sx <<= 1, sy <<= 1, dx <<= 1, dy <<= 1;
    REP(y, min(sy, dy), max(sy, dy) + 1)
    REP(x, min(sx, dx), max(sx, dx) + 1) G[y][x] = true;
  }

  cin >> ix >> iy >> ox >> oy;
  ix <<= 1, iy <<= 1, ox <<= 1, oy <<= 1;
  ix++, iy++, ox++, oy++;

  int tmp = getValue();
  int ans = 0;
  rep(y, 2 * H + 1) rep(x, 2 * W + 1) {
    if (y == 0 || x == 0 || y == 2 * H || x == 2 * W)
      continue;
    if (!(y & 1) && !(x & 1))
      continue;
    if ((y & 1) && (x & 1))
      continue;
    if (G[y][x])
      continue;
    G[y][x] = true;
    int cost = getValue();
    if (cost != IINF)
      ans = max(ans, cost - tmp);
    G[y][x] = false;
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

/*
14:03 - 14:24
14:24 - 14:46 debug
*/

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <set>
#include <vector>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define IINF (INT_MAX)
#define MAX 500

using namespace std;

struct Data {
  int x, y, cost;
  Data(int x = IINF, int y = IINF, int cost = IINF) : x(x), y(y), cost(cost) {}
};

int H, W, N, ix, iy, ox, oy;
int dx[] = {0, 2, 0, -2};
int dy[] = {2, 0, -2, 0};
bool G[MAX][MAX];
bool used[MAX][MAX];

void print() {
  rep(i, 2 * H + 1) {
    rep(j, 2 * W + 1) { cout << G[i][j]; }
    cout << endl;
  }
  cout << endl;
}

inline bool isValid(int nx, int ny) {
  return (0 <= nx && nx <= 2 * W && 0 <= ny && ny <= 2 * H);
}

int getValue() {
  if (ix == ox && iy == oy)
    return 0;
  rep(i, 2 * H + 1) rep(j, 2 * W + 1) used[i][j] = false;
  deque<Data> deq;
  deq.push_back(Data(ix, iy, 0));
  used[iy][ix] = true;
  while (true) {
    Data data = deq.front();
    deq.pop_front();
    int x = data.x, y = data.y;
    rep(i, 4) {
      int nx = x + dx[i], ny = y + dy[i];
      int mx = (x + nx) / 2, my = (y + ny) / 2;
      if (!isValid(nx, ny))
        continue;
      if (G[my][mx])
        continue;
      if (used[ny][nx])
        continue;
      used[ny][nx] = true;
      if (nx == ox && ny == oy)
        return data.cost + 1;
      deq.push_back(Data(nx, ny, data.cost + 1));
    }
  }
  return IINF;
}

int main() {

  cin >> W >> H >> N;
  rep(i, 2 * H + 1) G[i][0] = G[i][2 * W] = true;
  rep(i, 2 * W + 1) G[0][i] = G[2 * H][i] = true;
  rep(i, N) {
    int sx, sy, dx, dy;
    cin >> sx >> sy >> dx >> dy;
    sx <<= 1, sy <<= 1, dx <<= 1, dy <<= 1;
    REP(y, min(sy, dy), max(sy, dy) + 1)
    REP(x, min(sx, dx), max(sx, dx) + 1) G[y][x] = true;
  }

  cin >> ix >> iy >> ox >> oy;
  ix <<= 1, iy <<= 1, ox <<= 1, oy <<= 1;
  ix++, iy++, ox++, oy++;

  int tmp = getValue();
  int ans = 0;
  rep(y, 2 * H + 1) rep(x, 2 * W + 1) {
    if (y == 0 || x == 0 || y == 2 * H || x == 2 * W)
      continue;
    if (!(y & 1) && !(x & 1))
      continue;
    if ((y & 1) && (x & 1))
      continue;
    if (G[y][x])
      continue;
    G[y][x] = true;
    ans = max(ans, getValue() - tmp);
    G[y][x] = false;
  }

  cout << ans << endl;

  return 0;
}