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

#include <iostream>
#include <vector>
using namespace std;
#define DIM 1100

// field
int f[DIM * DIM];
// the count of . around f[i][j]
int m[DIM * DIM];
int count(int h, int w) {
  int a[9] = {0, 1, 1, 0, -1, -1, 1, -1, 0}, sum = 0;
  for (int i = 0; i < 8; i++) {
    if (f[(h + a[i]) * DIM + w + a[i + 1]] == -1)
      sum++;
  }
  return sum;
}
void change(int h, int w) {
  int a[9] = {0, 1, 1, 0, -1, -1, 1, -1, 0};
  for (int i = 0; i < 8; i++) {
    m[(h + a[i]) * DIM + w + a[i + 1]]++;
  }
}
void add(int h, int w, vector<int> &v) {
  int a[9] = {0, 1, 1, 0, -1, -1, 1, -1, 0};
  for (int i = 0; i < 8; i++) {
    if (m[(h + a[i]) * DIM + w + a[i + 1]] >=
            f[(h + a[i]) * DIM + w + a[i + 1]] &&
        f[(h + a[i]) * DIM + w + a[i + 1]] >= 0 &&
        f[(h + a[i]) * DIM + w + a[i + 1]] < 9) {
      //	cout<<f[(h+a[i])*DIM+w+a[i+1]]<<endl;
      f[(h + a[i]) * DIM + w + a[i + 1]] = -10;
      v.push_back((h + a[i]) * DIM + w + a[i + 1]);
    }
  }
}

int main() {
  int h, w;
  cin >> h >> w;
  vector<int> v;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char t;
      cin >> t;
      if (t == '.')
        f[i * DIM + j] = -1;
      else
        f[i * DIM + j] = t - '0';
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      m[i * DIM + j] = count(i, j);
      if (m[i * DIM + j] >= f[i * DIM + j] && f[i * DIM + j] != -1) {
        v.push_back(i * DIM + j);
        f[i * DIM + j] = -10;
      }
    }
  }
  int c = 1;
  int cnt;
  while (1) {
    cnt = v.size();
    /*for(int i = 0;i < cnt;i++){
      f[v[i]] = e;
      }*/
    for (int i = 0; i < cnt; i++) {
      change(v[i] / DIM, v[i] % DIM);
    }
    for (int i = 0; i < cnt; i++) {
      add(v[i] / DIM, v[i] % DIM, v);
    }
    v.erase(v.begin(), v.begin() + cnt);
    if (v.size() == 0) {
      break;
    }
    c++;
  }
  cout << c << endl;
  return 0;
}

Error: TLE

Fixed submission:



#include <iostream>
using namespace std;

int f[1100][1100];
int m[1100][1100];
int count(int h, int w) {
  int a[9] = {0, 1, 1, 0, -1, -1, 1, -1, 0}, sum = 0;
  for (int i = 0; i < 8; i++) {
    if (f[h + a[i]][w + a[i + 1]] == -1)
      sum++;
  }
  return sum;
}
void change(int h, int w) {
  int a[9] = {0, 1, 1, 0, -1, -1, 1, -1, 0};
  for (int i = 0; i < 8; i++) {
    m[h + a[i]][w + a[i + 1]]++;
  }
}

int main() {
  int h, w;
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char t;
      cin >> t;
      if (t == '.')
        f[i][j] = -1;
      else
        f[i][j] = t - '0';
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      m[i][j] = count(i, j);
    }
  }
  int c = 0;
  while (1) {
    bool end = true;
    for (int i = 1; i < h - 1; i++) {
      for (int j = 1; j < w - 1; j++) {
        if (f[i][j] != -1 && f[i][j] <= m[i][j]) {
          f[i][j] = -10;
          end = false;
        }
      }
    }
    for (int i = 1; i < h - 1; i++) {
      for (int j = 1; j < w - 1; j++) {
        if (f[i][j] == -10) {
          f[i][j] = -1;
          change(i, j);
        }
      }
    }
    if (end)
      break;
    c++;
  }
  cout << c << endl;
  return 0;
}