Problem p00535 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define mp make_pair
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> piii; // cnt, coodinate
string grid[1111];
int neiber[1111][1111];
int dx[] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[] = {1, 1, 0, -1, -1, -1, 0, 1};
int w, h;
bool renge(int x, int y) { return (0 <= x && x < w && 0 <= y && y < h); }
bool del(int x, int y) {
  if (!renge(x, y) || grid[y][x] == '.')
    return false;
  int cnt = 0;
  for (int i = 0; i < 8; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (!renge(nx, ny))
      continue;
    if (grid[ny][nx] == '.')
      cnt++;
  }
  char tmp = grid[y][x];
  neiber[y][x] = cnt;
  return cnt >= atoi(&tmp);
}

int main(int argc, char *argv[]) {
  queue<piii> que;
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    cin >> grid[i];
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (del(j, i)) {
        que.push(mp(1, mp(j, i)));
      }
    }
  }
  int ans = 0;
  while (!que.empty()) {
    int cnt = que.front().first;
    int x = que.front().second.first;
    int y = que.front().second.second;
    que.pop();
    if (grid[y][x] == '.')
      continue;
    grid[y][x] = '.';
    ans = cnt;
    for (int i = 0; i < 8; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (!renge(nx, ny))
        continue;
      char tmp = grid[ny][nx];
      neiber[ny][nx]++;
      if (neiber[ny][nx] >= atoi(&tmp)) {
        que.push(mp(cnt + 1, mp(nx, ny)));
      }
    }
  }
  std::cout << ans << std::endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define mp make_pair
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> piii; // cnt, coodinate
string grid[1111];
int neiber[1111][1111];
int dx[] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[] = {1, 1, 0, -1, -1, -1, 0, 1};
int w, h;
bool renge(int x, int y) {
  if (0 <= x && x < w && 0 <= y && y < h)
    return true;
  return false;
}
bool del(int x, int y) {
  int cnt = 0;
  if (renge(x, y) == false || grid[y][x] == '.')
    return false;
  for (int i = 0; i < 8; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (renge(nx, ny) == false)
      continue;
    if (grid[ny][nx] == '.')
      cnt++;
  }
  char tmp = grid[y][x];
  neiber[y][x] = cnt;
  if (cnt >= atoi(&tmp))
    return true;
  else
    return false;
}

int main(int argc, char *argv[]) {
  queue<piii> que;
  queue<pii> p;
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    cin >> grid[i];
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (del(j, i)) {
        p.push(mp(j, i));
        que.push(mp(1, mp(j, i)));
      }
    }
  }
  int ans = 0;
  while (!que.empty()) {
    int cnt = que.front().first;
    if (ans != cnt) {
      while (!p.empty()) {
        int x = p.front().first;
        int y = p.front().second;
        p.pop();
        if (grid[y][x] == '.')
          continue;
        grid[y][x] = '.';
        for (int i = 0; i < 8; i++) {
          int nx = x + dx[i], ny = y + dy[i];
          if (!renge(nx, ny))
            continue;
          neiber[ny][nx]++;
        }
      }
    }
    int x = que.front().second.first;
    int y = que.front().second.second;
    que.pop();
    ans = cnt;
    for (int i = 0; i < 8; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (renge(nx, ny) == false || grid[ny][nx] == '.' || grid[ny][nx] == '#')
        continue;
      char tmp = grid[ny][nx];
      if (neiber[ny][nx] >= atoi(&tmp)) {
        grid[y][x] = '#';
        p.push(mp(nx, ny));
        que.push(mp(cnt + 1, mp(nx, ny)));
      }
    }
  }
  std::cout << ans << std::endl;
  return 0;
}