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


#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define INF 1000000000
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;

int main() {
  int h, w;
  cin >> h >> w;
  vector<int> area(h * w);
  vector<int> ch(h * w, 0);
  queue<pair<int, int>> lis;
  rep(i, h) {
    rep(j, w) {
      char hoge;
      cin >> hoge;
      if (hoge == '.') {
        area[i * w + j] = -1;
        for (int k = -1; k < 2; k++) {
          for (int l = -1; l < 2; l++) {
            if (k == 0 and l == 0)
              continue;
            if (i + k > 0 and i + k < h - 1 and j + l > 0 and j + l < w - 1) {
              ch[(i + k) * w + j + l]++;
            }
          }
        }
      } else {
        area[i * w + j] = (int)hoge - (int)'0';
      }
    }
  }
  rep(i, h) {
    rep(j, w) {
      if (area[i * w + j] != -1 and ch[i * w + j] >= area[i * w + j]) {
        lis.push({i, j});
      }
    }
  }
  int ans = 0;
  int flag = 1;
  while (flag > 0) {
    int si = lis.size();
    flag = 0;
    rep(p, si) {
      if (ans == 0)
        ans = 1;
      int i = lis.front().first;
      int j = lis.front().second;
      lis.pop();
      area[i * w + j] = -1;
      for (int k = -1; k < 2; k++) {
        for (int l = -1; l < 2; l++) {
          if (k == 0 and l == 0)
            continue;
          if (i + k > 0 and i + k < h - 1 and j + l > 0 and j + l < w - 1) {
            ch[(i + k) * w + j + l]++;
            if (area[(i + k) * w + j + l] != -1 and
                ch[(i + k) * w + j + l] == area[(i + k) * w + j + l]) {
              lis.push({i + k, j + l});
              flag++;
            }
          }
        }
      }
    }
    if (flag > 0)
      ans++;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define INF 1000000000
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;

int main() {
  int h, w;
  cin >> h >> w;
  vector<int> area(h * w);
  vector<int> ch(h * w, 0);
  queue<pair<int, int>> lis;
  rep(i, h) {
    rep(j, w) {
      char hoge;
      cin >> hoge;
      if (hoge == '.') {
        area[i * w + j] = -1;
        for (int k = -1; k < 2; k++) {
          for (int l = -1; l < 2; l++) {
            if (k == 0 and l == 0)
              continue;
            if (i + k > 0 and i + k < h - 1 and j + l > 0 and j + l < w - 1) {
              ch[(i + k) * w + j + l]++;
            }
          }
        }
      } else {
        area[i * w + j] = (int)hoge - (int)'0';
      }
    }
  }
  rep(i, h) {
    rep(j, w) {
      if (area[i * w + j] != -1 and ch[i * w + j] >= area[i * w + j]) {
        lis.push({i, j});
      }
    }
  }
  int ans = 0;
  int flag = 1;
  while (flag > 0) {
    vector<int> oldc = ch;
    int si = lis.size();
    flag = 0;
    rep(p, si) {
      if (ans == 0)
        ans = 1;
      int i = lis.front().first;
      int j = lis.front().second;
      lis.pop();
      area[i * w + j] = -1;
      for (int k = -1; k < 2; k++) {
        for (int l = -1; l < 2; l++) {
          if (k == 0 and l == 0)
            continue;
          if (i + k > 0 and i + k < h - 1 and j + l > 0 and j + l < w - 1) {
            ch[(i + k) * w + j + l]++;
            if (area[(i + k) * w + j + l] != -1 and
                ch[(i + k) * w + j + l] == area[(i + k) * w + j + l]) {
              lis.push({i + k, j + l});
              flag++;
            }
          }
        }
      }
    }
    if (flag > 0)
      ans++;
  }
  cout << ans << endl;
}