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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef int INT;
typedef vector<INT> VI;
typedef vector<VI> VVI;
typedef pair<INT, INT> II;
typedef vector<II> VII;

template <class T> ostream &operator<<(ostream &os, vector<T> v) {
  for (typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i) {
    os << *it_i << ", ";
  }
  return os;
}

const int SIZE = 11;
const int MSIZE = 101;

int w, h;
int s;
int X[SIZE], Y[SIZE];
int t;
int PT[SIZE], QT[SIZE];
int T[MSIZE][MSIZE];
int Tcopy[MSIZE][MSIZE];

const int DSIZE = 6;
const int dr[2][DSIZE] = {{1, 1, 0, 0, -1, -1}, {1, 1, 0, 0, -1, -1}};
const int dc[2][DSIZE] = {{-1, 0, -1, 1, -1, 0}, {0, 1, -1, 1, 0, 1}};

void init() {
  for (int i = 0; i < MSIZE; ++i) {
    for (int j = 0; j < MSIZE; ++j) {
      T[i][j] = INT_MAX;
    }
  }
}

void save() {
  for (int i = 0; i < MSIZE; ++i) {
    for (int j = 0; j < MSIZE; ++j) {
      Tcopy[i][j] = T[i][j];
    }
  }
}

void load() {
  for (int i = 0; i < MSIZE; ++i) {
    for (int j = 0; j < MSIZE; ++j) {
      T[i][j] = Tcopy[i][j];
    }
  }
}

typedef II NODE;
typedef queue<NODE> QUEUE;

bool isValidPos(int r, int c) { return r >= 0 && r < h && c >= 0 && c < w; }

int spread(int sr, int sc) {
  QUEUE Q;
  Q.push(II(sr, sc));
  T[sr][sc] = 0;

  int res = 1;
  while (!Q.empty()) {
    NODE node = Q.front();
    Q.pop();

    int r = node.first;
    int c = node.second;
    int d = r % 2;

    for (int i = 0; i < DSIZE; ++i) {
      int nr = r + dr[d][i];
      int nc = c + dc[d][i];
      if (!isValidPos(nr, nc))
        continue;
      if (T[r][c] + 1 >= T[nr][nc])
        continue;
      T[nr][nc] = T[r][c] + 1;
      res++;
      NODE next(nr, nc);
      Q.push(next);
    }
  }
  return res;
}

void solve() {
  init();
  for (int i = 0; i < s; ++i) {
    spread(Y[i], X[i]);
  }
  save();
  int res = 0;
  for (int i = 0; i < t; ++i) {
    load();
    res = max(res, spread(QT[i], PT[i]));
  }
  cout << res << endl;
  return;
}

void input() {
  cin >> s;
  for (int i = 0; i < s; ++i) {
    cin >> X[i] >> Y[i];
    X[i] -= 1;
    Y[i] -= 1;
  }
  cin >> t;
  for (int i = 0; i < t; ++i) {
    cin >> PT[i] >> QT[i];
    PT[i] -= 1;
    QT[i] -= 1;
  }
}

int main() {
  while (cin >> w >> h) {
    if (w == 0 && h == 0)
      break;
    input();
    solve();
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;

typedef pair<int, int> PII;
typedef PII Node;
typedef queue<Node> Queue;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef set<PII> SPII;

int w, h;
const int inf = 1 << 24;
const int odx[6] = {0, 1, 1, 1, 0, -1};
const int ody[6] = {-1, -1, 0, 1, 1, 0};
const int edx[6] = {-1, 0, 1, 0, -1, -1};
const int edy[6] = {-1, -1, 0, 1, 1, 0};

int bfs(VVI &M, int X, int Y) {
  SPII V;
  V.clear();
  Queue Q;
  Node start(X, Y);
  Q.push(start);
  M[Y][X] = 0;
  while (!Q.empty()) {
    Node node = Q.front();
    Q.pop();

    int x = node.first;
    int y = node.second;
    if (V.count(PII(x, y)) == 0) {
      V.insert(PII(x, y));
    }

    for (int i = 0; i < 6; i++) {
      int nx = x + (y % 2 == 0 ? edx[i] : odx[i]);
      int ny = y + (y % 2 == 0 ? edy[i] : ody[i]);
      if (nx < 0 || nx >= w || ny < 0 || ny >= h)
        continue;
      if (M[y][x] + 1 >= M[ny][nx])
        continue;
      M[ny][nx] = M[y][x] + 1;
      Node nnode(nx, ny);
      Q.push(nnode);
    }
  }
  return V.size();
}

int main() {
  while (cin >> w >> h) {
    VVI M(h + 1, VI(w + 1, inf));

    int s;
    cin >> s;
    for (int i = 0; i < s; i++) {
      int x, y;
      cin >> x >> y;
      x--;
      y--;
      bfs(M, x, y);
    }

    int answer = 0;
    VVI backup = M;
    int t;
    cin >> t;
    for (int i = 0; i < t; i++) {
      M = backup;
      int x, y;
      cin >> x >> y;
      x--;
      y--;
      answer = max(answer, bfs(M, x, y));
    }

    cout << answer << endl;
  }
  return 0;
}