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

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

struct vec2 {
  int x;
  int y;
  vec2(int x, int y) {
    this->x = x;
    this->y = y;
  }
};

short s[1000][1000];

int main() {

  int w, h;

  cin >> w >> h;

  short table[w][h];

  int dirx[8] = {-1, -1, -1, 0, 0, 1, 1, 1},
      diry[8] = {-1, 0, 1, -1, 1, -1, 0, 1};

  char c;

  for (int i = 0; i < w; i++) {
    for (int j = 0; j < h; j++) {
      cin >> c;
      if (c == '.') {
        table[i][j] = -1;
      } else {
        stringstream ss;
        ss << c;
        ss >> table[i][j];
      }
    }
  }

  int ans = 0, com = -1, rep = -3;

  queue<vec2> q1, q2;

  for (int i = 1; i < w - 1; i++) {
    for (int j = 1; j < h - 1; j++) {

      int count = 0;
      for (int k = 0; k < 8; k++) {
        int di = i + dirx[k], dj = j + diry[k];
        if (table[di][dj] == -1) {
          count++;
        }
      }

      s[i][j] = count;

      if (table[i][j] > 0 && count >= table[i][j]) {
        vec2 tmpvec(i, j);
        q1.push(tmpvec);
        // table[i][j] = com;
      }
    }
  }

  /*for(int i=0;i<w;i++){
    for(int j=0;j<h;j++){
      cout << s[i][j] << " ";
    }
    cout << endl;
  }*/

  //  cout << endl;

  /*for(int i=1;i<w-1;i++){
    for(int j=1;j<h-1;j++){

        int count = 0;
        if(table[i+1][j] == com || table[i+1][j] == rep) count++;
        if(table[i+1][j-1] == com || table[i+1][j-1] == rep) count++;
        if(table[i+1][j+1] == com || table[i+1][j+1] == rep) count++;
        if(table[i][j-1] == com) count++;
        if(table[i][j+1] == com || table[i][j+1] == rep) count++;
        if(table[i-1][j] == com) count++;
        if(table[i-1][j+1] == com) count++;
        if(table[i-1][j-1] == com) count++;

        s[i][j] = count;

    }
  }*/

  /*for(int i=1;i<w-1;i++){
    for(int j=1;j<h-1;j++){

      if(table[i][j] == rep){
        table[i][j] = com;

      }
    }
  }*/

  /*  while(!q1.empty()){
      cout << q1.front().x << " " << q1.front().y << endl;
      q1.pop();
    }
    while(1){}*/

  /*  for(int i=0;i<w;i++){
      for(int j=0;j<h;j++){
        cout << table[i][j] << " ";
      }
      cout << endl;
    }
    cout << endl;*/

  if (q1.empty())
    ans = -1;

  while (1) {

    if (q1.empty()) {
      /*  for(int i=0;i<w;i++){
          for(int j=0;j<h;j++){
            cout << table[i][j] << " ";
          }
          cout << endl;
        }
        cout << endl;*/
      ans++;
      if (q2.empty()) {
        break;
      } else {
        while (!q2.empty()) {
          // cout << ans << " " <<  q2.front().x << " " << q2.front().y << endl;
          q1.push(q2.front());
          q2.pop();
        }

        /*for(int i=1;i<w-1;i++){
          for(int j=1;j<h-1;j++){
            if(table[i][j] == rep){
              table[i][j] = com;
            }
          }
        }*/
      }
    }

    vec2 tmpvec = q1.front();
    q1.pop();
    int i = tmpvec.x;
    int j = tmpvec.y;

    if (i != 0 && i != w - 1 && j != 0 && j != h - 1) {

      table[i][j] = com;
      for (int k = 0; k < 8; k++) {
        int vi = i + dirx[k];
        int vj = j + diry[k];
        s[vi][vj]++;
        //  cout << ans  << " " << vi << " " << vj << " " << table[vi][vj] << "
        //  " << s[vi][vj] << endl;
        if (table[vi][vj] > 0 && s[vi][vj] == table[vi][vj]) {
          vec2 tmpvecs(vi, vj);
          q2.push(tmpvecs);
          // table[vi][vj] = com;
        }

        /*
                  s[i+1][j]++;
                  s[i+1][j-1]++;
                  s[i+1][j+1]++;
                  s[i][j-1]++;
                  s[i][j+1]++;
                  s[i-1][j]++;
                  s[i-1][j+1]++;
                  s[i-1][j-1]++;

                  table[i][j] = com;
                  vec2 tmpvec(i-1,j-1);
                  q2.push(tmpvec);
                  tmpvec.y++;
                  q2.push(tmpvec);
                  tmpvec.y++;
                  q2.push(tmpvec);
                  tmpvec.x++;tmpvec.y-=2;
                  q2.push(tmpvec);
                  tmpvec.y+=2;
                  q2.push(tmpvec);
                  tmpvec.x++;tmpvec.y-=2;
                  q2.push(tmpvec);
                  tmpvec.y++;
                  q2.push(tmpvec);
                  tmpvec.y++;
                  q2.push(tmpvec);*/
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

struct vec2 {
  int x;
  int y;
  vec2(int x, int y) {
    this->x = x;
    this->y = y;
  }
};

int main() {

  int w, h;

  cin >> w >> h;

  int table[w][h], copy[w][h];

  char c;

  for (int i = 0; i < w; i++) {
    for (int j = 0; j < h; j++) {
      cin >> c;
      if (c == '.') {
        table[i][j] = -1;
      } else {
        stringstream ss;
        ss << c;
        ss >> table[i][j];
      }
    }
  }

  bool flag = true;

  int ans = 0, com = -1, sub, rep = -3;

  queue<vec2> q1, q2;

  for (int i = 0; i < w; i++) {
    for (int j = 0; j < h; j++) {

      if (table[i][j] != com && i != 0 && i != w - 1 && j != 0 && j != h - 1) {

        int count = 0;
        if (table[i + 1][j] == com || table[i + 1][j] == rep)
          count++;
        if (table[i + 1][j - 1] == com || table[i + 1][j - 1] == rep)
          count++;
        if (table[i + 1][j + 1] == com || table[i + 1][j + 1] == rep)
          count++;
        if (table[i][j - 1] == com)
          count++;
        if (table[i][j + 1] == com || table[i][j + 1] == rep)
          count++;
        if (table[i - 1][j] == com)
          count++;
        if (table[i - 1][j + 1] == com)
          count++;
        if (table[i - 1][j - 1] == com)
          count++;

        if (count >= table[i][j]) {
          table[i][j] = rep;
          vec2 tmpvec(i - 1, j - 1);
          q1.push(tmpvec);
          tmpvec.y++;
          q1.push(tmpvec);
          tmpvec.y++;
          q1.push(tmpvec);
          tmpvec.x++;
          tmpvec.y -= 2;
          q1.push(tmpvec);
          tmpvec.y += 2;
          q1.push(tmpvec);
          tmpvec.x++;
          tmpvec.y -= 2;
          q1.push(tmpvec);
          tmpvec.y++;
          q1.push(tmpvec);
          tmpvec.y++;
          q1.push(tmpvec);
        }

      } else {
        table[i][j] = com;
      }
    }
  }

  for (int i = 1; i < w - 1; i++) {
    for (int j = 1; j < h - 1; j++) {

      if (table[i][j] == rep) {
        table[i][j] = com;
      }
    }
  }

  /*  while(!q1.empty()){
      cout << q1.front().x << " " << q1.front().y << endl;
      q1.pop();
    }
    while(1){}*/

  /*for(int i=0;i<w;i++){
    for(int j=0;j<h;j++){
      cout << table[i][j] << " ";
    }
    cout << endl;
  }
  cout << endl;*/

  while (1) {

    if (q1.empty()) {
      /*for(int i=0;i<w;i++){
        for(int j=0;j<h;j++){
          cout << table[i][j] << " ";
        }
        cout << endl;
      }
      cout << endl;*/
      ans++;
      if (q2.empty()) {
        break;
      } else {

        while (!q2.empty()) {
          q1.push(q2.front());
          q2.pop();
        }

        for (int i = 1; i < w - 1; i++) {
          for (int j = 1; j < h - 1; j++) {
            if (table[i][j] == rep) {
              table[i][j] = com;
            }
          }
        }
      }
    }

    vec2 tmpvec = q1.front();
    q1.pop();
    int i = tmpvec.x;
    int j = tmpvec.y;

    if (table[i][j] > 0 && i != 0 && i != w - 1 && j != 0 && j != h - 1) {

      int count = 0;

      if (table[i + 1][j] == com /*|| table[i+1][j] == rep*/)
        count++;
      if (table[i + 1][j - 1] == com /*|| table[i+1][j-1] == rep*/)
        count++;
      if (table[i + 1][j + 1] == com /*|| table[i+1][j+1] == rep*/)
        count++;
      if (table[i][j - 1] == com)
        count++;
      if (table[i][j + 1] == com /*|| table[i][j+1] == rep*/)
        count++;
      if (table[i - 1][j] == com)
        count++;
      if (table[i - 1][j + 1] == com)
        count++;
      if (table[i - 1][j - 1] == com)
        count++;

      if (count >= table[i][j]) {
        // cout << count << " " << table[i][j] << " " << i << " " << j << endl;
        table[i][j] = rep;
        vec2 tmpvec(i - 1, j - 1);
        q2.push(tmpvec);
        tmpvec.y++;
        q2.push(tmpvec);
        tmpvec.y++;
        q2.push(tmpvec);
        tmpvec.x++;
        tmpvec.y -= 2;
        q2.push(tmpvec);
        tmpvec.y += 2;
        q2.push(tmpvec);
        tmpvec.x++;
        tmpvec.y -= 2;
        q2.push(tmpvec);
        tmpvec.y++;
        q2.push(tmpvec);
        tmpvec.y++;
        q2.push(tmpvec);
      }
    }
  }

  cout << ans << endl;

  return 0;
}