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

#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF 1000000000
typedef pair<int, int> P;
typedef long long int ll;
queue<P> que[2];
char mas[1001][1001];
int cou[1001][1001] = {{0}};

int main() {
  int h, w;
  int ans = 0;
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    scanf("%s", mas[i]);
  }
  for (int i = 1; i < h - 1; i++) {
    for (int j = 1; j < w - 1; j++) {
      for (int k = -1; k <= 1; k++) {
        for (int o = -1; o <= 1; o++) {
          int nx = i + k;
          int ny = j + o;
          if (mas[nx][ny] == '.')
            cou[i][j]++;
        }
      }
      if (mas[i][j] != '.' && cou[i][j] >= mas[i][j] - '0')
        que[ans].push(P(i, j));
    }
  }
  while (!que[ans % 2].empty()) {
    while (!que[ans % 2].empty()) {
      P p = que[ans % 2].front();
      que[ans % 2].pop();
      int x = p.first;
      int y = p.second;
      for (int i = -1; i <= 1; i++) {
        for (int j = -1; j <= 1; j++) {
          int nx = x + i;
          int ny = y + j;
          cou[nx][ny]++;
          if (mas[nx][ny] != '.' && cou[nx][ny] == mas[nx][ny] - '0')
            que[(ans + 1) % 2].push(P(nx, ny));
        }
      }
    }
    ans++;
  }
  cout << ans << endl;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF 1000000000
typedef pair<int, int> P;
typedef long long int ll;
queue<P> que1, que2;
char mas[1001][1001];

int main() {
  int h, w;
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> mas[i][j];
      if (mas[i][j] != '.' && mas[i][j] != '9') {
        que1.push(P(j, i));
      }
    }
  }
  int ans = 0;
  while (1) {
    while (!que1.empty()) {
      P p = que1.front();
      que1.pop();
      int x = p.first;
      int y = p.second;
      int power = mas[y][x] - '0';
      int cou = 0;
      for (int i = -1; i <= 1; i++) {
        if (power == cou)
          break;
        for (int j = -1; j <= 1; j++) {
          if (power == cou)
            break;
          int nx = x + i;
          int ny = y + j;
          if (mas[ny][nx] == '.') {
            cou++;
          }
        }
      }
      if (power == cou) {
        que2.push(P(x, y));
      }
    }
    if (que2.empty())
      break;
    ans++;
    while (!que2.empty()) {
      P p = que2.front();
      que2.pop();
      int x = p.first;
      int y = p.second;
      mas[y][x] = '.';
      for (int i = -1; i <= 1; i++) {
        for (int j = -1; j <= 1; j++) {
          int nx = x + i;
          int ny = y + j;
          if (mas[ny][nx] != '.' && mas[ny][nx] != '9') {
            que1.push(P(nx, ny));
          }
        }
      }
    }
  }
  cout << ans << endl;
}
Problem p00535 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <queue>
using namespace std;

int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  static char M[1000][1001];
  for (int i = 0; i < h; i++) {
    scanf("%s", M[i]);
  }
  static int K[1000][1001] = {0};
  int dx[8] = {1, 1, 1, 0, 0, -1, -1, -1}, dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
  queue<pair<int, int>> Q[2];
  bool b = 0;
  for (int i = 1; i < h - 1; i++) {
    for (int j = 1; j < w - 1; j++) {
      for (int v = 0; v < 8; v++) {
        int x = i + dx[v], y = j + dy[v];
        if (M[x][y] == '.') {
          K[i][j]++;
        }
      }
      if (M[i][j] != '.' && K[i][j] >= M[i][j] - '0') {
        Q[b].push(make_pair(i, j));
      }
    }
  }
  int t = 0;
  while (!Q[b].empty()) {
    while (!Q[b].empty()) {
      pair<int, int> p = Q[b].front();
      Q[b].pop();
      int i = p.first, j = p.second;
      for (int v = 0; v < 8; v++) {
        int x = i + dx[v], y = j + dy[v];
        K[x][y]++;
        if (M[x][y] != '.' && K[x][y] == M[x][y] - '0') {
          Q[!b].push(make_pair(x, y));
        }
      }
    }
    t++;
    b = !b;
  }
  printf("%d\\n", t);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef int ll;
typedef pair<ll, ll> P;
typedef double db;
string s[1000];
ll vx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
ll vy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
ll H, W;
queue<P> q;
queue<P> yaru;
bool pushed[1000][1000];
ll N, nx, ny;
ll ans, cnt;
int main() {
  scanf("%d%d", &H, &W);
  for (ll i = 0; i < H; i++)
    cin >> s[i];
  for (ll i = 0; i < H; i++)
    for (ll j = 0; j < W; j++)
      if (s[i][j] != '.' && s[i][j] != '9')
        q.push(P(i, j));

  bool update = 0;
  while (q.size()) {
    N = q.size();
    update = 0;
    memset(pushed, 0, sizeof(pushed));
    for (ll I = 0; I < N; I++) {
      P p = q.front();
      q.pop();
      if (s[p.first][p.second] == '.' || s[p.first][p.second] == '9')
        continue;
      cnt = 0;
      for (ll i = 0; i < 8; i++) {
        ny = p.first + vy[i];
        nx = p.second + vx[i];
        if (0 <= nx && nx < W && 0 <= ny && ny < H && s[ny][nx] == '.')
          cnt++;
      }
      if (cnt >= s[p.first][p.second] - '0') {
        yaru.push(P(p.first, p.second));
        update = 1;
      }
    }
    while (yaru.size()) {
      P p = yaru.front();
      yaru.pop();
      s[p.first][p.second] = '.';
      for (ll i = 0; i < 8; i++) {
        ny = p.first + vy[i];
        nx = p.second + vx[i];
        if (0 <= nx && nx < W && 0 <= ny && ny < H && s[ny][nx] != '.' &&
            s[ny][nx] != '9' && !pushed[ny][nx]) {
          pushed[ny][nx] = 1;
          q.push(P(ny, nx));
        }
      }
    }
    if (update)
      ans++;
  }
  // for(ll i=0;i<H;i++) c
  printf("%d\\n", ans);
}
Problem p00535 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#define F first
#define S second
#define MP make_pair
using namespace std;
typedef pair<int, int> P;
typedef pair<P, int> PP;
char c[1000][1000];
bool flag[1000][1000];
int w, h, ans;
int cdt[1000][1000];
queue<PP> que, que2;

void fanc(PP p) {
  for (int i = -1; i < 2; i++) {
    for (int j = -1; j < 2; j++) {
      if (!i && !j)
        continue;
      if (p.F.F + i < h && p.F.F + i >= 0 && p.F.S + j < w && p.F.S + j >= 0) {
        cdt[p.F.F + i][p.F.S + j]++;
        if (cdt[p.F.F + i][p.F.S + j] == c[p.F.F + i][p.F.S + j] - '0') {
          que.push(MP(MP(p.F.F + i, p.F.S + j), p.S));
        }
      }
    }
  }
}
void bfs() {
  while (!que.empty()) {
    fanc(MP(que.front().F, que.front().S + 1));
    ans = max(ans, que.front().S);
    que.pop();
  }
}
int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> c[i][j];
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (c[i][j] == '.') {
        fanc(MP(MP(i, j), 1));
      }
    }
  }
  bfs();
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#define F first
#define S second
#define MP make_pair
using namespace std;
typedef pair<int, int> P;
typedef pair<P, int> PP;
char c[1000][1000];
bool flag[1000][1000];
int w, h, ans;
int cdt[1000][1000];
queue<PP> que, que2;

void fanc(PP p) {
  for (int i = -1; i < 2; i++) {
    for (int j = -1; j < 2; j++) {
      if (!i && !j)
        continue;
      if (p.F.F + i < h && p.F.F + i < h && p.F.S + j < w && p.F.S + j < w) {
        cdt[p.F.F + i][p.F.S + j]++;
        if (cdt[p.F.F + i][p.F.S + j] == c[p.F.F + i][p.F.S + j] - '0') {
          que.push(MP(MP(p.F.F + i, p.F.S + j), p.S));
        }
      }
    }
  }
}
void bfs() {
  while (!que.empty()) {
    fanc(MP(que.front().F, que.front().S + 1));
    ans = max(ans, que.front().S);
    que.pop();
  }
}
int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> c[i][j];
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (c[i][j] == '.') {
        fanc(MP(MP(i, j), 1));
      }
    }
  }
  bfs();
  cout << ans << endl;
}
Problem p00535 (C++) — buggy submission, verdict: Time Limit Exceeded

// q5.cpp

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

typedef long long ll;

using namespace std;

long long h, w, ans, flag = 0;
char m[1020][1020];
int md[1020][1020];
bool mc[1020][1020];
queue<pair<int, int>> s, s1, che;
pair<int, int> p;

int check(int y, int x) {
  int c = 0;

  for (int i = -1; i <= 1; i++)
    for (int j = -1; j <= 1; j++)
      if (m[y + i][x + j] == '.')
        c++;

  if ((int)(m[y][x] - '0') <= c && !mc[y][x]) {
    mc[y][x] = true;
    che.push(make_pair(y, x));
    s1.push(make_pair(y, x));
    return 1;
  }

  return 0;
}

int main() {
  scanf("%lld%lld", &h, &w);

  for (int i = 0; i < h; i++)
    scanf("%s", m[i]);

  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (m[i][j] == '.')
        md[i][j] = 0;
      else
        md[i][j] = m[i][j] - '0';

  // 1
  flag = 0;

  for (int i = 1; i < h - 1; i++)
    for (int j = 1; j < w - 1; j++)
      if (m[i][j] != '.')
        flag += check(i, j);

  while (!che.empty()) {
    p = che.front();
    che.pop();
    m[p.first][p.second] = '.';
  }

  if (flag)
    ans++;
  else {
    puts("0");
    return 0;
  }

  // 2-
  do {
    s = s1;

    while (!s1.empty())
      s1.pop();

    flag = 0;

    for (int i = 0; !s.empty(); i++) {
      p = s.front();
      s.pop();

      for (int j = -1; j <= 1; j++)
        for (int k = -1; k <= 1; k++)
          if (m[p.first + j][p.second + k] != '.' &&
              !mc[p.first + j][p.second + k])
            flag += check(p.first + j, p.second + k);
    }

    while (!che.empty()) {
      p = che.front();
      che.pop();
      m[p.first][p.second] = '.';
    }

    if (flag)
      ans++;

  } while (flag);

  printf("%lld\\n", ans);

  return 0;
}

Error: TLE

Fixed submission:

// q5.cpp

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

typedef long long ll;

using namespace std;

long long h, w, ans, flag = 0;
char m[1020][1020];
bool mc[1020][1020];

int check(int y, int x) {
  int c = 0;

  for (int i = -1; i <= 1; i++)
    for (int j = -1; j <= 1; j++)
      if (m[y + i][x + j] == '.')
        c++;

  if ((int)(m[y][x] - '0') <= c) {
    mc[y][x] = true;
    return 1;
  }

  return 0;
}

int main() {
  scanf("%lld%lld", &h, &w);

  for (int i = 0; i < h; i++)
    scanf("%s", m[i]);

  do {
    flag = 0;

    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        mc[i][j] = false;

    for (int i = 1; i < h - 1; i++)
      for (int j = 1; j < w - 1; j++)
        if (m[i][j] != '.')
          flag += check(i, j);

    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        if (mc[i][j])
          m[i][j] = '.';

    if (flag)
      ans++;

  } while (flag);

  printf("%lld\\n", ans);

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

#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int h, w;
int map[1005][1005];
int d[9] = {-1, 0, 1};
class S {
public:
  int x, y, z;
  S(int a, int b, int c) { x = a, y = b, z = c; }
};
int main() {
  int ans = 0;
  queue<S> q;
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int i2 = 0; i2 < w; i2++) {
      char c;
      cin >> c;
      if (c == '.') {
        map[i][i2] = 0;
        q.push(S(i2, i, 0));
      } else {
        map[i][i2] = atoi(&c);
      }
    }
  }
  while (!q.empty()) {
    S s = q.front();
    int x = s.x, y = s.y, z = s.z;
    ans = z;
    q.pop();
    for (int i = 0; i < 3; i++) {
      for (int i2 = 0; i2 < 3; i2++) {
        int xx = x + d[i2];
        int yy = y + d[i];
        if (xx >= 0 && xx < w && yy >= 0 && yy < h && --map[yy][xx] == 0) {
          q.push(S(xx, yy, z + 1));
          /*for (int i3 = 0; i3 < h; i3++){
                  for (int i4 = 0; i4 < w; i4++){
                          printf("%3d", map[i3][i4]);
                  }cout << endl;
          }
          cout << endl;*/
        }
      }
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
using namespace std;

int x, y;
char map[1024][1024];
queue<pair<int, int>> q;

bool check_map(void) {
  bool flg = true;
  int dx[8] = {0, 0, 1, -1, 1, -1, 1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, -1, 1};
  for (int i = 0; i < y; i++) {
    for (int i2 = 0; i2 < x; i2++) {
      int cnt = 0;
      if (map[i][i2] != '.') {
        for (int i3 = 0; i3 < 8; i3++) {
          if (map[i + dy[i3]][i2 + dx[i3]] == '.') {
            cnt++;
          }
        }
        char temp = map[i][i2];
        if (cnt >= atoi(&temp)) {
          q.push(make_pair(i, i2));
          flg = false;
        }
      }
    }
  }
  return flg;
}

int main() {
  int cnt = 0;
  cin >> y >> x;

  for (int i = 0; i < y; i++) {
    for (int i2 = 0; i2 < x; i2++) {
      cin >> map[i][i2];
    }
  }

  while (!check_map()) {
    while (!q.empty()) {
      pair<int, int> temp = q.front();
      q.pop();
      map[temp.first][temp.second] = '.';
    }
    cnt++;
  }
  cout << cnt << endl;
  return 0;
}
Problem p00535 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> ip;
const int MAX = 1010;
int cnt[MAX][MAX];
int mx[] = {1, 1, 1, 0, 0, -1, -1, -1}, my[] = {1, 0, -1, 1, -1, 1, 0, -1};
int main() {
  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  for (int i = 0; i < h; i++)
    cin >> s[i];
  queue<ip> q;
  for (int i = 0; i < h; i++) { // 各地点の周りにいくつ更地があるかカウント
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '.')
        continue;
      for (int k = 0; k < 8; k++) {
        if (s[i + my[k]][j + mx[k]] == '.')
          cnt[i][j]++;
      }
      if (cnt[i][j] >= s[i][j] - '0') { // 壊せるなら壊す
        s[i][j] = '0';
        q.push(make_pair(i, j));
      }
    }
  }
  if (q.empty()) {
    cout << 0 << endl;
    return 0;
  }
  int ans = 1;
  while (true) {
    queue<pair<int, int>> broken;
    while (!q.empty()) {
      auto now = q.front();
      q.pop();
      int y = now.first, x = now.second;
      for (int i = 0; i < 8; i++) { // 壊れた地点の周りのカウントを＋１。
        int yy = y + my[i], xx = x + mx[i];
        if (s[yy][xx] == '.' || s[yy][xx] == '0')
          continue;
        cnt[yy][xx]++;
        if (cnt[yy][xx] >= s[yy][xx] - '0') {
          s[yy][xx] = '0';
          broken.push(make_pair(yy, xx));
        }
      }
    }
    if (broken.empty()) {
      cout << ans << endl;
      return 0;
    }
    while (!broken.empty()) {
      q.push(broken.front());
      broken.pop();
    }
    ans++;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int grd[1010][1010];
int mx[] = {1, 1, 1, 0, 0, 0, -1, -1, -1},
    my[] = {1, 0, -1, 1, 0, -1, 1, 0, -1};
int main() {
  int h, w;
  cin >> h >> w;
  queue<pair<int, int>> ls;
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < w; j++) {
      if (s[j] == '.')
        grd[i][j] = -1;
      else {
        grd[i][j] = s[j] - '0';
        ls.push(make_pair(i, j));
      }
    }
  }
  int ans = 0;
  while (true) {
    // cout<<ans<<endl;
    // for(int i=0;i<h;i++){
    //	for(int j=0;j<w;j++)	cout<<grd[i][j]<<" ";
    //	cout<<endl;
    // }
    // cout<<endl;
    queue<pair<int, int>> next;
    queue<pair<int, int>> broken;
    while (!ls.empty()) {
      auto now = ls.front();
      ls.pop();
      int y = now.first, x = now.second;
      if (grd[y][x] == -1)
        continue;
      int cnt = 0;
      for (int i = 0; i < 9; i++)
        if (grd[y + my[i]][x + mx[i]] == -1)
          cnt++;
      if (cnt >= grd[y][x])
        broken.push(make_pair(y, x));
    }
    if (broken.empty()) {
      cout << ans << endl;
      return 0;
    }
    while (!broken.empty()) {
      auto now = broken.front();
      broken.pop();
      int y = now.first, x = now.second;
      grd[y][x] = -1;
      for (int i = 0; i < 9; i++) {
        int xx = x + mx[i], yy = y + my[i];
        next.push(make_pair(yy, xx));
      }
    }
    while (!next.empty()) {
      ls.push(next.front());
      next.pop();
    }
    ans++;
  }
}
Problem p00535 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
#include <queue>
struct QUE {
  int y;
  int x;
  int count;
  QUE(int a, int b, int c) { y = a, x = b, count = c; }
};

int map[1024][1024];
queue<QUE> q;
int y_move[] = {-1, -1, -1, 0, 0, +1, +1, +1},
    x_move[] = {-1, 0, +1, -1, +1, -1, 0, +1};
int main() {
  int h, w;
  cin >> h >> w;

  for (int i = 1; i <= h; ++i) {
    for (int i2 = 1; i2 <= w; ++i2) {
      char str;
      cin >> str;
      if (str == '.') {
        map[i][i2] = 0;
        q.push(QUE(i, i2, 1));
      } else
        map[i][i2] = str - '0';
    }
  }

  int ans;
  while (!q.empty()) {
    int y = q.front().y, x = q.front().x;
    ans = q.front().count;
    q.pop();
    for (int k = 0; k < 8; ++k) {
      if (map[y + y_move[k]][x + x_move[k]] > 0) {
        map[y + y_move[k]][x + x_move[k]]--;
        if (map[y + y_move[k]][x + x_move[k]] == 0) {
          q.push(QUE(y + y_move[k], x + x_move[k], ans + 1));
        }
      }
    }
  }
  cout << ans - 1 << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
struct MAP {
  int a[1024][1024];
};

MAP map, update;
int y_move[] = {-1, -1, -1, 0, 0, +1, +1, +1},
    x_move[] = {-1, 0, +1, -1, +1, -1, 0, +1};
int main() {
  int h, w;
  cin >> h >> w;

  for (int i = 1; i <= h; ++i) {
    for (int i2 = 1; i2 <= w; ++i2) {
      char str;
      cin >> str;
      if (str == '.')
        map.a[i][i2] = 0;
      else
        map.a[i][i2] = str - '0';
    }
  }
  update = map;

  for (int count = 0;; ++count) {
    map = update;
    bool flg = false;
    for (int i = 1; i <= h; i++) {
      for (int i2 = 1; i2 <= w; i2++) {
        if (map.a[i][i2] > 0) {
          int cnt = 0;
          for (int k = 0; k < 8; k++) {
            if (map.a[i + y_move[k]][i2 + x_move[k]] == 0) {
              cnt++;
            }
          }
          if (map.a[i][i2] <= cnt) {
            update.a[i][i2] = 0;
            flg = true;
          }
        }
      }
    }
    if (!flg) {
      cout << count << endl;
      break;
    }
  }
}
Problem p00535 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <queue>
using namespace std;

int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  static char M[1000][1001];
  for (int i = 0; i < h; i++) {
    scanf("%s", M[i]);
  }
  static int K[1000][1001] = {0};
  int dx[8] = {1, 1, 1, 0, 0, -1, -1, -1}, dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
  queue<pair<int, int>> Q[2];
  bool b = 0;
  for (int i = 1; i < h - 1; i++) {
    for (int j = 1; j < w - 1; j++) {
      for (int v = 0; v < 8; v++) {
        int x = i + dx[v], y = j + dy[v];
        if (M[x][y] == '.') {
          K[i][j]++;
        }
      }
      if (M[i][j] != '.' && K[i][j] >= M[i][j] - '0') {
        Q[b].push(make_pair(i, j));
      }
    }
  }
  int t = 0;
  while (!Q[b].empty()) {
    while (!Q[b].empty()) {
      pair<int, int> p = Q[b].front();
      Q[b].pop();
      int i = p.first, j = p.second;
      for (int v = 0; v < 8; v++) {
        int x = i + dx[v], y = j + dy[v];
        K[x][y]++;
        if (M[x][y] != '.' && K[x][y] == M[x][y] - '0') {
          Q[!b].push(make_pair(x, y));
        }
      }
    }
    t++;
    b = !b;
  }
  printf("%d\\n", t);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;
typedef long long ll;

int h, w;
char ca[1000][1001];
char cpy[1000][1001];
int cnt[1000][1000];
bool b = 0;
queue<P> que[2];

int main() {
  scanf("%d%d", &h, &w);
  rep(i, h) {
    scanf("%s", ca[i]);
    strcpy(cpy[i], ca[i]);
  }
  rep(i, h) {
    rep(j, w) {
      if (ca[i][j] == '.')
        continue;
      for (int t = -1; t <= 1; t++)
        for (int k = -1; k <= 1; k++)
          if (ca[i + t][j + k] == '.')
            cnt[i][j]++;
      if (cpy[i][j] != '.' && ca[i][j] - '0' <= cnt[i][j]) {
        cpy[i][j] = '.';
        que[b].push(P(i, j));
      }
    }
  }
  int res = 0;
  while (!que[b].empty()) {
    res++;
    rep(i, h) strcpy(ca[i], cpy[i]);
    while (!que[b].empty()) {
      P p = que[b].front();
      que[b].pop();
      int f = p.first, s = p.second;
      for (int i = -1; i <= 1; i++) {
        for (int j = -1; j <= 1; j++) {
          int t = f + i, k = s + j;
          if (ca[t][k] == '.')
            continue;
          cnt[t][k]++;
          if (cpy[t][k] != '.' && ca[t][k] - '0' <= cnt[t][k]) {
            cpy[t][k] = '.';
            que[!b].push(P(t, k));
          }
        }
      }
    }
    b = !b;
  }
  printf("%d\\n", res);
  return 0;
}
Problem p00535 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;

int h, w;
string s[1111];
int data[1111][1111];
typedef pair<int, int> P;
typedef pair<int, P> PP;
queue<PP> que;

void cnt(int x, int y) {

  for (int i = -1; i < 2; i++)
    for (int j = -1; j < 2; j++)
      if (i + y >= 0 && j + x >= 0)
        data[i + y][j + x]++;
}

int main() {

  cin >> h >> w;
  for (int i = 0; i < h; i++)
    cin >> s[i];

  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (s[i][j] == '.')
        cnt(j, i);

  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (s[i][j] != '.' && data[i][j] >= s[i][j] - '0')
        que.push(PP(1, P(j, i)));

  int ans = 0;
  while (!que.empty()) {
    PP p = que.front();
    que.pop();
    int x = p.S.F, y = p.S.S;

    if (s[y][x] == '.')
      continue;
    ans = max(ans, p.F);

    s[y][x] = '.';
    cnt(x, y);

    for (int i = -1; i < 2; i++) {
      for (int j = -1; j < 2; j++) {
        int xx = j + x, yy = i + y;
        if (xx >= 0 && yy >= 0 && s[yy][xx] != '.' &&
            data[yy][xx] >= s[yy][xx] - '0')
          que.push(PP(p.F + 1, P(xx, yy)));
      }
    }
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int data[1111][1111];

void cnt(int x, int y) {

  for (int i = -1; i < 2; i++) {
    for (int j = -1; j < 2; j++) {
      if (y + i < 0 || x + j < 0)
        continue;
      data[y + i][x + j]++;
    }
  }
}

int main() {

  int h, w, ans = -1;

  string s[1111];
  typedef pair<int, int> P;
  queue<P> que;

  cin >> h >> w;
  for (int i = 0; i < h; i++)
    cin >> s[i];

  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (s[i][j] == '.')
        cnt(j, i);

  // for(int i=0;i<h;i++, puts("")) for(int j=0;j<w;j++) cout << data[i][j] << "
  // ";
  while (!que.empty())
    que.pop();

  do {

    while (!que.empty()) {
      P p = que.front();
      que.pop();
      cnt(p.first, p.second);
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (s[i][j] != '.') {
          int num = s[i][j] - '0';
          if (num <= data[i][j]) {
            que.push(P(j, i));
            s[i][j] = '.';
          }
        }
      }
    }

    ans++;
  } while (!que.empty());

  cout << ans << endl;
}