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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> Pi;
typedef pair<int, Pi> Pii;
#define fr first
#define sc second

int m, n, s;
int min_cost[100][100];
bool used[100][100];
int dy[] = {-1, 0, 1, -1, 0, 1};
int dx[][6] = {{-1, -1, -1, 0, 1, 0}, {0, -1, 0, 1, 1, 1}};

bool isover(int y, int x) { return y < 0 || y >= n || x < 0 || x >= m; }

void bfs(int y, int x) {
  queue<Pii> que;
  que.push(Pii(0, Pi(y, x)));
  min_cost[y][x] = 0;
  while (!que.empty()) {
    Pii p = que.front();
    que.pop();
    for (int i = 0; i < 6; i++) {
      int ny = p.sc.fr + dy[i], nx = p.sc.sc + dx[p.sc.fr % 2][i];
      if (isover(ny, nx) || min_cost[ny][nx] <= p.fr + 1)
        continue;
      que.push(Pii(p.fr + 1, Pi(ny, nx)));
      min_cost[ny][nx] = p.fr + 1;
    }
  }
}

int prebfs(int y, int x) {
  if (!min_cost[y][x])
    return 0;

  int ret = 1;
  fill_n(*used, 10000, false);
  queue<Pii> que;
  que.push(Pii(0, Pi(y, x)));
  used[y][x] = true;

  while (!que.empty()) {
    Pii p = que.front();
    que.pop();
    for (int i = 0; i < 6; i++) {
      int ny = p.sc.fr + dy[i], nx = p.sc.sc + dx[p.sc.fr % 2][i];
      if (isover(ny, nx) || used[ny][nx] || min_cost[ny][nx] <= p.fr + 1)
        continue;
      que.push(Pii(p.fr + 1, Pi(ny, nx)));
      used[ny][nx] = true;
      ret++;
    }
  }
  return ret;
}

int main() {
  while (cin >> m, m) {
    fill_n(*min_cost, 10000, 1 << 30);

    cin >> n >> s;

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

    int t, ret = 0;
    cin >> t;
    while (t--) {
      int p, q;
      cin >> p >> q;
      ret = max(ret, prebfs(q - 1, p - 1));
    }
    cout << ret << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> Pi;
typedef pair<int, Pi> Pii;
#define fr first
#define sc second

int m, n, s;
int min_cost[100][100];
bool used[100][100];
int dy[] = {-1, -1, 0, 1, 1, 0};
int dx[][6] = {{-1, 0, 1, 0, -1, -1}, {0, 1, 1, 1, 0, -1}};

bool isover(int y, int x) { return y < 0 || y >= m || x < 0 || x >= n; }

void bfs(int y, int x) {
  queue<Pii> que;
  que.push(Pii(0, Pi(y, x)));
  min_cost[y][x] = 0;
  while (!que.empty()) {
    Pii p = que.front();
    que.pop();
    for (int i = 0; i < 6; i++) {
      int ny = p.sc.fr + dy[i], nx = p.sc.sc + dx[p.sc.fr % 2][i];
      if (isover(ny, nx) || min_cost[ny][nx] < p.fr + 1)
        continue;
      que.push(Pii(p.fr + 1, Pi(ny, nx)));
      min_cost[ny][nx] = p.fr + 1;
    }
  }
}

int prebfs(int y, int x) {
  if (!min_cost[y][x])
    return 0;

  int ret = 1;
  fill_n(*used, 10000, false);
  queue<Pii> que;
  que.push(Pii(0, Pi(y, x)));
  used[y][x] = true;

  while (!que.empty()) {
    Pii p = que.front();
    que.pop();
    for (int i = 0; i < 6; i++) {
      int ny = p.sc.fr + dy[i], nx = p.sc.sc + dx[p.sc.fr % 2][i];
      if (isover(ny, nx) || used[ny][nx] || min_cost[ny][nx] <= p.fr + 1)
        continue;
      que.push(Pii(p.fr + 1, Pi(ny, nx)));
      used[ny][nx] = true;
      ret++;
    }
  }
  return ret;
}

int main() {
  while (cin >> m, m) {
    queue<Pii> que;
    fill_n(*min_cost, 10000, 1 << 30);

    cin >> n >> s;

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

    int t, ret = 0;
    cin >> t;
    while (t--) {
      int p, q;
      cin >> p >> q;
      ret = max(ret, prebfs(q - 1, p - 1));
    }
    cout << ret << endl;
  }
}