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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef pair<int, int> P;
typedef pair<int, P> PP;

int mx[2][6] = {{-1, 0, 1, 0, -1, -1}, {0, 1, 1, 1, 0, -1}};
int my[2][6] = {{-1, -1, 0, 1, 1, 0}, {-1, -1, 0, 1, 1, 0}};

int W, H, S, T;

void update(int sy, int sx, vector<vector<int>> &v) {
  queue<PP> open;
  open.push(PP(0, P(sy, sx)));
  v[sy][sx] = 0;
  while (!open.empty()) {
    PP pp = open.front();
    open.pop();
    int y = pp.second.first, x = pp.second.second, n = pp.first,
        t = (y % 2 == 0);
    REP(i, 6) {
      int ny = y + my[t][i], nx = x + mx[t][i];
      if (ny <= 0 || nx <= 0 || ny > H || nx > W || v[ny][nx] <= n + 1)
        continue;
      v[ny][nx] = n + 1;
      open.push(PP(n + 1, P(ny, nx)));
    }
  }
}

int countC(int sy, int sx, vector<vector<int>> &v) {
  queue<PP> open;
  open.push(PP(0, P(sy, sx)));
  vector<vector<bool>> visited(H + 1, vector<bool>(W + 1, 0));
  visited[sy][sx] = 1;
  int res = 0;
  while (!open.empty()) {
    PP pp = open.front();
    open.pop();
    int y = pp.second.first, x = pp.second.second, n = pp.first,
        t = (y % 2 == 0);
    if (v[y][x] > n)
      ++res;
    REP(i, 6) {
      int ny = y + my[t][i], nx = x + mx[t][i];
      if (ny <= 0 || nx <= 0 || ny > H || nx > W || v[ny][nx] <= n + 1 ||
          visited[ny][nx])
        continue;
      visited[ny][nx] = 1;
      open.push(PP(n + 1, P(ny, nx)));
    }
  }
  return res;
}

int main() {
  while (cin >> W >> H && W && H) {
    vector<vector<int>> v(H + 1, vector<int>(W + 1, 1e9));
    cin >> S;
    REP(s, S) {
      int x, y;
      cin >> x >> y;
      update(y, x, v);
    }
    cin >> T;
    int ans = 0;
    REP(t, T) {
      int x, y;
      cin >> x >> y;
      ans = max(ans, countC(y, x, v));
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef pair<int, int> P;
typedef pair<int, P> PP;

int mx[2][6] = {{-1, 0, 1, 0, -1, -1}, {0, 1, 1, 1, 0, -1}};
int my[2][6] = {{-1, -1, 0, 1, 1, 0}, {-1, -1, 0, 1, 1, 0}};

int W, H, S, T;

void update(int sy, int sx, vector<vector<int>> &v) {
  queue<PP> open;
  open.push(PP(0, P(sy, sx)));
  v[sy][sx] = 0;
  while (!open.empty()) {
    PP pp = open.front();
    open.pop();
    int y = pp.second.first, x = pp.second.second, n = pp.first,
        t = (y % 2 == 0);
    REP(i, 6) {
      int ny = y + my[t][i], nx = x + mx[t][i];
      if (ny <= 0 || nx <= 0 || ny > H || nx > W || v[ny][nx] <= n + 1)
        continue;
      v[ny][nx] = n + 1;
      open.push(PP(n + 1, P(ny, nx)));
    }
  }
}

int countc(int sy, int sx, vector<vector<int>> &v) {
  queue<PP> open;
  open.push(PP(0, P(sy, sx)));
  vector<vector<bool>> visited(H + 1, vector<bool>(W + 1, 0));
  visited[sy][sx] = 1;
  int res = 0;
  while (!open.empty()) {
    PP pp = open.front();
    open.pop();
    int y = pp.second.first, x = pp.second.second, n = pp.first,
        t = (y % 2 == 0);
    if (v[y][x] > n)
      ++res;
    REP(i, 6) {
      int ny = y + my[t][i], nx = x + mx[t][i];
      if (ny <= 0 || nx <= 0 || ny > H || nx > W || v[ny][nx] <= n + 1 ||
          visited[ny][nx])
        continue;
      visited[y][x] = 1;
      open.push(PP(n + 1, P(ny, nx)));
    }
  }
  return res;
}

int main() {
  while (cin >> W >> H && W && H) {
    vector<vector<int>> v(H + 1, vector<int>(W + 1, 1e9));
    cin >> S;
    REP(s, S) {
      int x, y;
      cin >> x >> y;
      update(y, x, v);
    }
    cin >> T;
    int ans = 0;
    REP(t, T) {
      int x, y;
      cin >> x >> y;
      ans = max(ans, countc(y, x, v));
    }
    cout << ans << endl;
  }
  return 0;
}