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

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
const int inf = 1e9;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = 1; i <= n; i++)
typedef pair<int, int> P;

queue<P> q1, q2;
char str[1004][1004];
int a[1004][1004], b[1004][1004], h, w;
int dy[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dx[] = {-1, 0, 1, -1, 1, -1, 0, 1};
bool ok(int y, int x) {
  if (y < 0 || h + 1 < y)
    return 1;
  if (x < 0 || w + 1 < x)
    return 1;
  return 0;
}

void calc(int y, int x) {
  if (b[y][x] || !a[y][x] || ok(y, x))
    return;
  if (a[y][x] != inf) {
    a[y][x]--;
    if (!a[y][x])
      q1.push(P(y, x));
    return;
  }
  b[y][x] = 1;
  for (int i = 0; i < 8; i++) {
    calc(y + dy[i], x + dx[i]);
  }
}

int solve(int k) {
  if (q1.empty())
    return 0;
  while (!q1.empty()) {
    P p = q1.front();
    q1.pop();
    b[p.fi][p.se] = 1;
    for (int i = 0; i < 8; i++) {
      int yy = p.fi + dy[i], xx = p.se + dx[i];
      if (!b[yy][xx] && a[yy][xx] != inf && a[yy][xx] != 0) {
        a[yy][xx]--;
        if (a[yy][xx] == 0)
          q2.push(P(yy, xx));
      }
    }
  }
  swap(q1, q2);
  return k + 1;
}

int main() {
  fill((int *)a, (int *)(a + 1004), inf);
  scanf("%d%d", &h, &w);
  rep(i, h) scanf("%s", str[i]);
  rep(i, h) rep(j, w) {
    if (str[i][j] != '.')
      a[i + 1][j + 1] = str[i][j] - '0';
  }

  int res = 0, ans = 1;
  rrep(i, h) rrep(j, w) if (a[i][j] == inf) calc(i, j);
  while (1) {
    res = solve(res);
    if (res == 0)
      break;
    ans = res;
  }
  printf("%d\\n", ans);

  /*for(int i = 1;i <= h;i++){
          for(int j = 1;j <= w;j++)printf("%d ", a[i][j]);
          printf("\\n");
  }*/
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
const int inf = 1e9;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = 1; i <= n; i++)
typedef pair<int, int> P;

queue<P> q1, q2;
char str[1004][1004];
int a[1004][1004], b[1004][1004], h, w;
int dy[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dx[] = {-1, 0, 1, -1, 1, -1, 0, 1};
bool ok(int y, int x) {
  if (y < 0 || h + 1 < y)
    return 1;
  if (x < 0 || w + 1 < x)
    return 1;
  return 0;
}

void calc(int y, int x) {
  if (b[y][x] || !a[y][x] || ok(y, x))
    return;
  if (a[y][x] != inf) {
    a[y][x]--;
    if (!a[y][x])
      q1.push(P(y, x));
    return;
  }
  b[y][x] = 1;
  for (int i = 0; i < 8; i++) {
    calc(y + dy[i], x + dx[i]);
  }
}

int solve(int k) {
  if (q1.empty())
    return k;
  while (!q1.empty()) {
    P p = q1.front();
    q1.pop();
    b[p.fi][p.se] = 1;
    for (int i = 0; i < 8; i++) {
      int yy = p.fi + dy[i], xx = p.se + dx[i];
      if (!b[yy][xx] && a[yy][xx] != inf && a[yy][xx] != 0) {
        a[yy][xx]--;
        if (a[yy][xx] == 0)
          q2.push(P(yy, xx));
      }
    }
  }
  swap(q1, q2);
  k = solve(k + 1);
  return k;
}

int main() {
  fill((int *)a, (int *)(a + 1004), inf);
  scanf("%d%d", &h, &w);
  rep(i, h) scanf("%s", str[i]);
  rep(i, h) rep(j, w) {
    if (str[i][j] != '.')
      a[i + 1][j + 1] = str[i][j] - '0';
  }

  rrep(i, h) rrep(j, w) if (a[i][j] == inf) calc(i, j);
  printf("%d\\n", solve(0));

  /*for(int i = 1;i <= h;i++){
          for(int j = 1;j <= w;j++)printf("%d ", a[i][j]);
          printf("\\n");
  }*/
  return 0;
}