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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<P, int> Pi;
int n, m, cnt;
int mincost[110][110];
bool visited[110][110];
int dx[2][6] = {{-1, +0, 1, 0, -1, -1}, {+0, +1, 1, 1, 0, -1}};
int dy[2][6] = {{-1, -1, 0, 1, +1, +0}, {-1, -1, 0, 1, 1, +0}};

void dfs(int x, int y) {

  deque<Pi> deq;
  deq.push_back(Pi(P(x, y), 0));
  mincost[y][x] = 0;
  visited[y][x] = true;
  cnt = 1;
  while (!deq.empty()) {
    Pi pi = deq.front();
    deq.pop_front();

    for (int i = 0; i < 6; i++) {
      int nx = pi.first.first + dx[pi.first.second % 2][i];
      int ny = pi.first.second + dy[pi.first.second % 2][i];

      if (!(0 <= nx && nx < m && 0 <= ny && ny < n))
        continue;

      if (mincost[ny][nx] > pi.second + 1) {
        if (!visited[ny][nx])
          cnt++;
        visited[ny][nx] = true;
        mincost[ny][nx] = pi.second + 1;
        deq.push_back(Pi(P(nx, ny), pi.second + 1));
      }
    }
  }
}

int main() {
  while (cin >> m >> n, m) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        mincost[i][j] = (1 << 29);

    int s, t, mex = 1;
    scanf("%d", &s);
    for (int i = 0; i < s; i++) {
      int x, y;
      scanf("%d %d", &x, &y);
      x--, y--;
      mincost[y][x] = 0;
      dfs(x, y);
    }

    scanf("%d", &t);
    for (int i = 0; i < t; i++) {
      int pre_mincost[n][m];
      for (int j = 0; j < n; j++)
        for (int k = 0; k < m; k++)
          pre_mincost[j][k] = mincost[j][k], visited[j][k] = false;
      int p, q;
      scanf("%d %d", &p, &q);
      p--, q--;
      dfs(p, q);

      mex = max(mex, cnt);
      for (int j = 0; j < n; j++)
        for (int k = 0; k < m; k++)
          mincost[j][k] = pre_mincost[j][k];
    }
    cout << mex << endl;
  }
  return 0;
}

Error: 0
6 0
6
1 1
6 1
3 2
3 5
1 6
5 6
2
1 3
5 3
6 6
6
3 2
3 5
6 1
1 1
1 6
5 6
2
2 3
5 3
0 

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<P, int> Pi;
int n, m, cnt;
int mincost[110][110];
bool visited[110][110];
int dx[2][6] = {{-1, +0, 1, 0, -1, -1}, {+0, +1, 1, 1, 0, -1}};
int dy[2][6] = {{-1, -1, 0, 1, +1, +0}, {-1, -1, 0, 1, 1, +0}};

void dfs(int x, int y) {

  deque<Pi> deq;
  deq.push_back(Pi(P(x, y), 0));
  mincost[y][x] = 0;
  visited[y][x] = true;
  cnt = 1;
  while (!deq.empty()) {
    Pi pi = deq.front();
    deq.pop_front();

    for (int i = 0; i < 6; i++) {
      int nx = pi.first.first + dx[pi.first.second % 2][i];
      int ny = pi.first.second + dy[pi.first.second % 2][i];

      if (!(0 <= nx && nx < m && 0 <= ny && ny < n))
        continue;

      if (mincost[ny][nx] > pi.second + 1) {
        if (!visited[ny][nx])
          cnt++;
        visited[ny][nx] = true;
        mincost[ny][nx] = pi.second + 1;
        deq.push_back(Pi(P(nx, ny), pi.second + 1));
      }
    }
  }
}

int main() {
  while (true) {
    cin >> n;
    cerr << n << " ";
    if (!n)
      break;
    cerr << m << endl;
    cin >> m;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        mincost[i][j] = (1 << 29);

    int s, t, mex = 1;
    scanf("%d", &s);
    cerr << s << endl;
    for (int i = 0; i < s; i++) {
      int x, y;
      scanf("%d %d", &x, &y);
      cerr << x << " " << y << endl;
      x--, y--;
      mincost[y][x] = 0;
      dfs(x, y);
    }

    scanf("%d", &t);
    cerr << t << endl;
    for (int i = 0; i < t; i++) {
      int pre_mincost[n][m];
      for (int j = 0; j < n; j++)
        for (int k = 0; k < m; k++)
          pre_mincost[j][k] = mincost[j][k], visited[j][k] = false;
      int p, q;
      scanf("%d %d", &p, &q);
      cerr << p << " " << q << endl;
      p--, q--;
      dfs(p, q);

      mex = max(mex, cnt);
      for (int j = 0; j < n; j++)
        for (int k = 0; k < m; k++)
          mincost[j][k] = pre_mincost[j][k];
    }
    cout << mex << endl;
  }
  return 0;
}