alexjercan/bugnet huggingface dataset:
Problem p00535 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;

int h, w;
int sa[1005][1005];
int d[9] = {-1, 0, 1};
struct P {
  int x, y, h;
  P(int a, int b, int c) { x = a, y = b, h = c; }
};
int main() {
  cin >> h >> w;
  queue<P> q;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char c;
      cin >> c;
      if (c == '.') {
        sa[i][j] = 0;
        q.push(P(j, i, 0));
      } else
        sa[i][j] = c - '0';
    }
  }
  int ans = 0;
  while (q.size()) {
    P t = q.front();
    int y = t.y, x = t.x, ph = t.h;
    ans = ph;
    q.pop();
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++) {
        int nx = x + d[i];
        int ny = y + d[j];
        if (nx >= 0 && nx < w && ny >= 0 && ny < h && --sa[ny][nx] == 0)
          q.push(P(nx, ny, ph + 1));
      }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;
typedef unsigned long long ll;

int h, w;
int sa[1005][1005];

int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char c;
      cin >> c;
      if (c == '.')
        sa[i][j] = -1;
      else
        sa[i][j] = c - '0';
    }
  }
  int ans = 0;
  while (1) {
    int ch = false;
    queue<pair<int, int>> q;
    for (int i = 1; i < h - 1; i++) {
      for (int j = 1; j < w - 1; j++) {
        if (sa[i][j] == -1)
          continue;
        int c = 0;
        if (sa[i - 1][j - 1] == -1)
          c++;
        if (sa[i - 1][j] == -1)
          c++;
        if (sa[i - 1][j + 1] == -1)
          c++;
        if (sa[i][j - 1] == -1)
          c++;
        if (sa[i][j] == -1)
          c++;
        if (sa[i][j + 1] == -1)
          c++;
        if (sa[i + 1][j - 1] == -1)
          c++;
        if (sa[i + 1][j] == -1)
          c++;
        if (sa[i + 1][j + 1] == -1)
          c++;
        if (sa[i][j] <= c) {
          q.push(make_pair(i, j));
          ch = true;
        }
      }
    }
    while (!q.empty()) {
      pair<int, int> p = q.front();
      q.pop();
      sa[p.first][p.second] = -1;
    }
    if (!ch)
      break;
    ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p00535 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;

int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};

int main() {
  int h, w, cnt[1000][1000] = {};
  string field[1000];
  cin >> h >> w;
  for (int i = 0; i < h; i++)
    cin >> field[i];
  set<P> next;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (field[i][j] == '.')
        continue;
      next.insert(P(i, j));
      for (int k = 0; k < 8; k++) {
        int nx = i + dx[k], ny = j + dy[k];
        if (nx >= 0 && nx < h && ny >= 0 && ny < w && field[nx][ny] == '.')
          cnt[i][j]++;
      }
    }
  }
  for (int res = 0;; res++) {
    bool chg = false;
    vector<P> chgs;
    for (auto it = next.begin(); it != next.end(); it++) {
      P p = *it;
      int i = p.first, j = p.second;
      if (field[i][j] != '.' && cnt[i][j] >= field[i][j] - '0') {
        field[i][j] = '.';
        chg = true;
        for (int k = 0; k < 8; k++) {
          int nx = i + dx[k], ny = j + dy[k];
          if (nx >= 0 && nx < h && ny >= 0 && ny < w && field[nx][ny] != '.') {
            chgs.push_back(P(nx, ny));
          }
        }
      }
    }
    next.clear();
    for (int i = 0; i < chgs.size(); i++) {
      int x = chgs[i].first, y = chgs[i].second;
      cnt[x][y]++;
      next.insert(P(x, y));
    }
    if (!chg) {
      cout << res << endl;
      break;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;

int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};

int main() {
  int h, w, cnt[1000][1000] = {};
  string field[1000];
  cin >> h >> w;
  for (int i = 0; i < h; i++)
    cin >> field[i];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (field[i][j] == '.')
        continue;
      for (int k = 0; k < 8; k++) {
        int nx = i + dx[k], ny = j + dy[k];
        if (nx >= 0 && nx < h && ny >= 0 && ny < w && field[nx][ny] == '.')
          cnt[i][j]++;
      }
    }
  }
  for (int res = 0;; res++) {
    bool chg = false;
    vector<P> chgs;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (field[i][j] != '.' && cnt[i][j] >= field[i][j] - '0') {
          field[i][j] = '.';
          chg = true;
          for (int k = 0; k < 8; k++) {
            int nx = i + dx[k], ny = j + dy[k];
            if (nx >= 0 && nx < h && ny >= 0 && ny < w &&
                field[nx][ny] != '.') {
              chgs.push_back(P(nx, ny));
            }
          }
        }
      }
    }
    for (int i = 0; i < chgs.size(); i++) {
      int x = chgs[i].first, y = chgs[i].second;
      cnt[x][y]++;
    }
    if (!chg) {
      cout << res << endl;
      break;
    }
  }
  return 0;
}
Problem p00535 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <queue>
#include <set>

#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

typedef pair<int, int> P;

int n, m;
char c[1000][1001];

int main() {
  scanf("%d%d", &n, &m);
  rep(i, n) { scanf("%s", c[i]); }

  queue<P> q;
  rep(i, n) {
    rep(j, m) {
      if (c[i][j] != '.') {
        q.push(P(i, j));
      }
    }
  }

  int a = 0;
  while (!q.empty()) {
    /*
    rep(i, n){
            puts(c[i]);
    }
    printf("%d\\n", q.size());
    */

    set<P> s;
    set<P> t;
    int y = q.size();
    rep(k, y) {
      P v = q.front();
      q.pop();
      if (c[v.first][v.second] == '.') {
        continue;
      }
      int z = 0;

      for (int i = -1; i <= 1; ++i) {
        for (int j = -1; j <= 1; ++j) {
          int x = v.first + i;
          int y = v.second + j;
          if ((i != 0 || j != 0) && 0 <= x && x < n && 0 <= y && y < m) {
            if (c[x][y] == '.') {
              ++z;
            }
          }
        }
      }
      if (z >= c[v.first][v.second] - '0') {
        s.insert(v);
        for (int i = -1; i <= 1; ++i) {
          for (int j = -1; j <= 1; ++j) {
            int x = v.first + i;
            int y = v.second + j;
            if ((i != 0 || j != 0) && 0 <= x && x < n && 0 <= y && y < m) {
              if (c[x][y] != '.') {
                t.insert(P(x, y));
              }
            }
          }
        }
      }
    }
    if (!s.empty()) {
      ++a;
    }
    for (auto it = s.begin(); it != s.end(); ++it) {
      c[it->first][it->second] = '.';
      // printf("s %d\\n", s.size());
    }
    for (auto it = t.begin(); it != t.end(); ++it) {
      q.push(*it);
      // printf("t %d\\n", t.size());
    }
  }
  printf("%d\\n", a);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>

#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

typedef pair<int, int> P;

int n, m;
char c[1000][1001];

int main() {
  scanf("%d%d", &n, &m);
  rep(i, n) { scanf("%s", c[i]); }

  queue<P> q;
  rep(i, n) {
    rep(j, m) {
      if (c[i][j] != '.') {
        q.push(P(i, j));
      }
    }
  }

  int a = 0;
  while (!q.empty()) {
    /*
    rep(i, n){
            puts(c[i]);
    }
    puts("");
    */

    vector<P> s;
    int y = q.size();
    rep(k, y) {
      P v = q.front();
      q.pop();
      if (c[v.first][v.second] == '.') {
        continue;
      }
      int z = 0;
      vector<P> t;
      for (int i = -1; i <= 1; ++i) {
        for (int j = -1; j <= 1; ++j) {
          int x = v.first + i;
          int y = v.second + j;
          if ((i != 0 || j != 0) && 0 <= x && x < n && 0 <= y && y < m) {
            if (c[x][y] == '.') {
              ++z;
            } else {
              t.push_back(P(x, y));
            }
          }
        }
      }
      if (z >= c[v.first][v.second] - '0') {
        s.push_back(v);
        rep(i, t.size()) { q.push(t[i]); }
      }
    }
    if (!s.empty()) {
      ++a;
    }
    rep(i, s.size()) { c[s[i].first][s[i].second] = '.'; }
  }
  printf("%d\\n", a);
  return 0;
}
Problem p00535 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fr first
#define sc second
typedef pair<int, int> Pii;
typedef vector<int> Vi;
const int INF = (1 << 30);
const int dx[] = {-1, 0, 1, 1, 1, 0, -1, -1},
          dy[] = {-1, -1, -1, 0, 1, 1, 1, 0};

int main() {
  int h, w;
  int maps[1000][1000];
  int cnt[1000][1000] = {0};
  queue<Pii> q;

  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char c;
      cin >> c;
      if (c == '.') {
        maps[i][j] = 0;
        for (int l = 0; l < 8; l++) {
          int nx = j + dx[l], ny = i + dy[l];
          if (0 <= nx && nx < w && 0 <= ny && ny < h) {
            cnt[ny][nx]++;
          }
        }
      } else {
        maps[i][j] = c - '0';
        q.push(Pii(i, j));
      }
    }
  }

  bool flag = true;
  int ans = 0;
  while (1) {
    vector<Pii> v;
    flag = false;
    int tmp = q.size();
    for (int i = 0; i < tmp; i++) {
      Pii p = q.front();
      q.pop();
      int x = p.sc, y = p.fr;
      if (maps[y][x] && maps[y][x] <= cnt[y][x]) {
        flag = true;
        maps[y][x] = 0;
        v.pb(Pii(y, x));
      }
    }

    if (!flag) {
      break;
    }
    for (int i = 0; i < v.size(); i++) {
      for (int l = 0; l < 8; l++) {
        int nx = v[i].sc + dx[l], ny = v[i].fr + dy[l];
        if (0 <= nx && nx < w && 0 <= ny && ny < h) {
          cnt[ny][nx]++;
          q.push(Pii(ny, nx));
        }
      }
    }

    ans++;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fr first
#define sc second
typedef pair<int, int> Pii;
typedef vector<int> Vi;
const int INF = (1 << 30);
const int dx[] = {-1, 0, 1, 1, 1, 0, -1, -1},
          dy[] = {-1, -1, -1, 0, 1, 1, 1, 0};

int main() {
  int h, w;
  int maps[1000][1000];
  int cnt[1000][1000] = {0};

  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char c;
      cin >> c;
      if (c == '.') {
        maps[i][j] = 0;
        for (int l = 0; l < 8; l++) {
          int nx = j + dx[l], ny = i + dy[l];
          if (0 <= nx && nx < w && 0 <= ny && ny < h) {
            cnt[ny][nx]++;
          }
        }
      } else
        maps[i][j] = c - '0';
    }
  }

  bool flag = true;
  int ans = 0;
  while (1) {
    vector<Pii> v;
    flag = false;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (maps[i][j] && maps[i][j] <= cnt[i][j]) {
          flag = true;
          maps[i][j] = 0;
          v.pb(Pii(i, j));
        }
      }
    }

    if (!flag)
      break;
    for (int i = 0; i < v.size(); i++) {
      for (int l = 0; l < 8; l++) {
        int nx = v[i].sc + dx[l], ny = v[i].fr + dy[l];
        if (0 <= nx && nx < w && 0 <= ny && ny < h) {
          cnt[ny][nx]++;
        }
      }
    }
    ans++;
  }
  cout << ans << endl;
}
Problem p00535 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

#pragma warning(disable : 4996)

#define INF 1000000000

using namespace std;

const vector<int> dx = {0, 1, 1, 1, 0, -1, -1, -1};
const vector<int> dy = {-1, -1, 0, 1, 1, 1, 0, -1};

int H, W;
vector<string> M;

vector<vector<int>> state;

int check(int x, int y, int s) {
  int ret = 0;

  for (int dir = 0; dir < 8; dir++) {
    if (state[y + dy[dir]][x + dx[dir]] < s ||
        M[y + dy[dir]][x + dx[dir]] == '.') {
      ret++;
    }
  }

  return ret;
}

int main() {
  scanf("%d", &H);
  scanf("%d", &W);

  M = vector<string>(H);

  for (int i = 0; i < H; i++)
    cin >> M[i];

  state = vector<vector<int>>(H, vector<int>(W, INF));

  queue<pair<int, int>> que;

  for (int i = 1; i < H - 1; i++) {
    for (int j = 1; j < W - 1; j++) {
      if (check(j, i, 1) >= M[i][j] - 48 && M[i][j] != '.') {
        que.push(make_pair(j, i));
        state[i][j] = 1;
      }
    }
  }

  while (!que.empty()) {
    int x = que.front().first;
    int y = que.front().second;
    que.pop();

    for (int dir = 0; dir < 8; dir++) {
      int x2 = x + dx[dir];
      int y2 = y + dy[dir];

      if (state[y2][x2] == INF && M[y2][x2] != '.' &&
          check(x2, y2, state[y][x] + 1) >= M[y2][x2] - 48) {
        state[y2][x2] = state[y][x] + 1;

        que.push(make_pair(x2, y2));
      }
    }
  }

  int ret = 0;

  for (int i = 1; i < H - 1; i++) {
    for (int j = 1; j < W - 1; j++) {
      if (state[i][j] != INF) {
        ret = max(ret, state[i][j]);
      }
    }
  }

  printf("%d\\n", ret);

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

const int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
const int dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};

int main() {
  int H, W;

  cin >> H >> W;

  vector<vector<char>> D(H, vector<char>(W));

  vector<vector<int>> memo(H, vector<int>(W, 1 << 29));

  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      cin >> D[i][j];

  bool ok = true;

  int count_ = -1;

  while (ok) {
    ok = false;

    count_++;

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (D[i][j] != '.') {
          int count = 0;

          for (int dir = 0; dir < 8; dir++) {
            if (0 <= i + dy[dir] && i + dy[dir] < H && 0 <= j + dx[dir] &&
                j + dx[dir] < W) {
              if (D[i + dy[dir]][j + dx[dir]] == '.' &&
                  memo[i + dy[dir]][j + dx[dir]] != count_) {
                count++;
              }
            }
          }

          if (count >= D[i][j] - 48) {
            D[i][j] = '.';

            memo[i][j] = count_;

            ok = true;
          }
        }
      }
    }
  }

  cout << count_ << endl;

  return 0;
}
Problem p00535 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define INF (1 << 30)
#define ll long long
using namespace std;

const int dy[16] = {0, -1, 0, 1, 1, -1, 1, -1, 0, -2, 0, 2};
const int dx[16] = {1, 0, -1, 0, 1, 1, -1, -1, 2, 0, -2, 0};

ll H, W;
vector<vector<ll>> m(1010, vector<ll>(1010, -1));
vector<vector<ll>> C(1010, vector<ll>(1010, 0));

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> H >> W;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      char c;
      cin >> c;
      if (c != '.') {
        m[i][j] = c - '0';
      } else {
        for (int k = 0; k < 8; k++) {
          C[i + dx[k]][j + dy[k]]++;
        }
      }
    }
  }

  ll res = 0;
  queue<pair<pair<ll, ll>, ll>> q;

  for (int i = 2; i <= H - 1; i++) {
    for (int j = 2; j <= W - 1; j++) {
      if (m[i][j] > 0) {
        if (C[i][j] >= m[i][j]) {
          q.push({{i, j}, 1});
          m[i][j] = -1;
        }
      }
    }
  }

  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    res = p.second;
    for (int i = 0; i < 8; i++) {
      int x = p.first.first + dx[i];
      int y = p.first.second + dy[i];
      if (m[x][y] == -1)
        continue;

      C[x][y]++;

      if (C[x][y] >= m[x][y]) {
        q.push({{x, y}, res + 1});
        m[x][y] = -1;
      }
    }
  }

  cout << res << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define ll long long
using namespace std;

const int dy[16] = {0, -1, 0, 1, 1, -1, 1, -1, 0, -2, 0, 2};
const int dx[16] = {1, 0, -1, 0, 1, 1, -1, -1, 2, 0, -2, 0};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll H, W;
  cin >> H >> W;
  vector<vector<ll>> m(H + 1, vector<ll>(W + 1, -1));
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      char c;
      cin >> c;
      if (c != '.') {
        m[i][j] = c - '0';
      }
    }
  }
  bool f;
  int count = 0;
  while (true) {
    f = false;
    auto temp_m = m;
    for (int i = 2; i <= H - 1; i++) {
      for (int j = 2; j <= W - 1; j++) {
        if (temp_m[i][j] > 0) {
          int count2 = 0;
          for (int k = 0; k < 8; k++) {

            if (temp_m[i + dx[k]][j + dy[k]] == -1) {
              count2++;
            }
          }
          if (count2 >= temp_m[i][j]) {
            m[i][j] = -1;
            f = true;
          }
        }
      }
    }

    /*
    for (int i = 1; i <= H;i++) {
            for (int j = 1; j <= W; j++) {
                    if (m[i][j] == -1) {
                            cout << "# ";continue;
                    }
                    cout << m[i][j] << " ";
            }
            cout << endl;
    }
    cout << endl;
    */

    if (f == true)
      count++;
    else
      break;
  }

  cout << count << endl;
}
Problem p00535 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int h, w;
vector<string> v;
char t[1001];
int a[1000][1000];
int dx[8] = {1, 1, 1, 0, -1, -1, -1, 0};
int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
typedef pair<int, int> P;
queue<P> q[2];
int b = 0;
int ans;

int main() {
  scanf("%d %d", &h, &w);
  for (int i = 0; i < h; i++) {
    scanf("%s", t);
    v.push_back(t);
  }
  for (int i = 1; i < w - 1; i++) {
    for (int j = 1; j < h - 1; j++) {
      int c = 0;
      ;
      for (int k = 0; k < 8; k++) {
        int nx, ny;
        nx = i + dx[k];
        ny = j + dy[k];
        if (v[ny][nx] == '.')
          c++;
      }
      a[j][i] = c;
    }
  }
  for (int i = 1; i < w - 1; i++) {
    for (int j = 1; j < h - 1; j++) {
      if (v[j][i] != '.') {
        if (v[j][i] - '0' <= a[j][i])
          q[0].push(P(i, j));
      }
    }
  }

  while (q[b].size()) {
    ans++;
    while (q[b].size()) {
      P p = q[b].front();
      q[b].pop();
      for (int i = 0; i < 8; i++) {
        int nx, ny;
        nx = p.first + dx[i];
        ny = p.second + dy[i];
        a[ny][nx]++;
        if (v[ny][nx] != '.' && a[ny][nx] == v[ny][nx] - '0')
          q[1 - b].push(P(nx, ny));
      }
    }
    b = 1 - b;
    /*
                    for(int i=0; i<h; i++){
                            for(int j=0; j<w; j++){
                                    printf("%d\\t", a[i][j]);
                            }
                            printf("\\n");
                    }
                    printf("\\n");
    */
  }

  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int h, w;
vector<string> v;
char t[1001];
int a[1000][1000];
int dx[8] = {1, 1, 1, 0, -1, -1, -1, 0};
int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
typedef pair<int, int> P;
queue<P> q[2];
int b = 0;
int ans;

int main() {
  scanf("%d %d", &h, &w);
  for (int i = 0; i < h; i++) {
    scanf("%s", t);
    v.push_back(t);
  }
  for (int i = 1; i < w - 1; i++) {
    for (int j = 1; j < h - 1; j++) {
      int c = 0;
      ;
      for (int k = 0; k < 8; k++) {
        int nx, ny;
        nx = i + dx[k];
        ny = j + dy[k];
        if (v[ny][nx] == '.')
          c++;
      }
      a[j][i] = c;
    }
  }
  for (int i = 1; i < w - 1; i++) {
    for (int j = 1; j < h - 1; j++) {
      if (v[j][i] != '.') {
        if (v[j][i] - '0' <= a[j][i])
          q[0].push(P(i, j));
      }
    }
  }

  while (q[b].size()) {
    while (q[b].size()) {
      ans++;
      P p = q[b].front();
      q[b].pop();
      for (int i = 0; i < 8; i++) {
        int nx, ny;
        nx = p.first + dx[i];
        ny = p.second + dy[i];
        a[ny][nx]++;
        if (v[ny][nx] != '.' && a[ny][nx] >= v[ny][nx] - '0')
          q[1 - b].push(P(nx, ny));
      }
    }
    b = 1 - b;
  }

  printf("%d\\n", ans);
  return 0;
}
Problem p00535 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define REP(i, n) for (int i = 0; i < n; i++)
#define ARR(i, j)                                                              \\
  for (int i = -1; i <= 1; i++)                                                \\
    for (int j = -1; j <= 1; j++)                                              \\
      if (i != 0 || j != 0)
typedef pair<int, int> P;

int h, w;
char a[1002][1002];
int kd[1002][1002];
int sr[1002][1002];
queue<P> now;
queue<P> nxt;

signed main() {
  cin >> h >> w;
  REP(i, h) REP(j, w) cin >> a[i][j];
  REP(i, h) REP(j, w) {
    if (i == 0 || i == h - 1 || j == 0 || j == w - 1)
      continue;
    if (a[i][j] != '.') {
      kd[i][j] = a[i][j] - '0';
      ARR(dx, dy) if (a[i + dx][j + dy] == '.') sr[i][j]++;
      if (sr[i][j] >= kd[i][j])
        nxt.push(P(i, j));
    }
  }
  int res = 0;
  while (!nxt.empty()) {
    res++;
    swap(now, nxt);
    while (!now.empty()) {
      P p = now.front();
      now.pop();
      if (a[p.first][p.second] == '.')
        continue;
      a[p.first][p.second] = '.';
      nxt.push(p);
    }
    swap(now, nxt);
    while (!now.empty()) {
      P p = now.front();
      now.pop();
      ARR(dx, dy) {
        int qx = p.first + dx;
        int qy = p.second + dy;
        sr[qx][qy]++;
        if (a[qx][qy] != '.' && sr[qx][qy] >= kd[qx][qy])
          nxt.push(P(qx, qy));
      }
    }
  }
  cout << res << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define loop(i, n) for (int i = 1; i <= n; i++)
typedef pair<int, int> P;
int h, w;
char sh[1002][1002];
int houkai[1002][1002];
queue<P> q1;
queue<P> q2;
int dir[9] = {0, -1, -1, 0, 1, 1, -1, 1, 0};

bool kuzureru(int i, int j) {
  if (i == 1 || i == h || j == 1 || j == w)
    return 0;
  if (sh[i][j] == '.')
    return 0;
  int kyoudo = (int)(sh[i][j] - '0');
  int sara = 0;
  loop(e, 8) {
    int x = i + dir[e - 1];
    int y = j + dir[e];
    if (sh[x][y] == '.')
      sara++;
  }
  return (sara >= kyoudo);
}

int main() {
  cin >> h >> w;
  loop(i, h) loop(j, w) { cin >> sh[i][j]; }
  loop(i, h) loop(j, w) {
    if (kuzureru(i, j))
      q1.push(P(i, j));
  }
  int nami = 0;
  while (!q1.empty()) {
    nami++;
    while (!q1.empty()) {
      P p = q1.front();
      q1.pop();
      q2.push(p);
      sh[p.first][p.second] = '.';
    }
    while (!q2.empty()) {
      P p = q2.front();
      q2.pop();
      loop(e, 8) {
        int x = p.first + dir[e - 1];
        int y = p.second + dir[e];
        if (kuzureru(x, y))
          q1.push(P(x, y));
      }
    }
  }
  cout << nami << endl;
  return 0;
}