Problem p03053 (C++)

Buggy submission (status: Time Limit Exceeded):
/*

    author : s@if

*/

#include <bits/stdc++.h>
using namespace std;
#define NIL -1
#define fi first
#define sec second
#define MAX INT_MAX
#define INF 99999999
#define ll long long
#define PI acos(-1.0)
#define MOD 1000000007
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define ull unsigned long long
#define triplell pair<pair<ll, ll>, ll>
#define triple pair<pair<int, int>, int>
#define For(i, n) for (int i = 0; i < (int)n; i++)
#define Forn(i, a, b) for (int i = (int)a; i <= (int)b; i++)
#define forba(i, a, b) for (int i = (int)b; i >= (int)a; i--)

bool Check(int N, int pos) { return (bool)(N & (1 << pos)); }
int Set(int N, int pos) { return N = N | (1 << pos); }

int fx[] = {+0, +0, +1, -1, -1, +1, -1, +1};
int fy[] = {-1, +1, +0, +0, +1, +1, -1, -1};
int hr[] = {-2, -2, -1, +1, +2, +2, -1, +1};
int hc[] = {+1, -1, +2, +2, -1, +1, -2, -2};
int dx[] = {+1, -1, +0, +0};
int dy[] = {+0, +0, +1, -1};

const int MAXN = (int)1e5 + 9;
char ch[1009][1009];
int n, m, cnt;

bool chekh(int x, int y) {
  if (x >= 0 && x < n && y >= 0 && y < m)
    return true;

  return false;
}

void solve(vector<PII> v1) {
  int i, j;

  while (1) {
    /*  for(int i = 0;i<v1.size();i++)
          cout<<v1[i].fi<<" "<<v1[i].sec<<endl;

          cout<<endl<<endl; */

    vector<PII> v2;

    bool flag = false;

    for (int i = 0; i < v1.size(); i++) {
      PII u = v1[i];

      for (int j = 0; j < 4; j++) {
        int nx = u.fi + dx[j];
        int ny = u.sec + dy[j];

        if (chekh(nx, ny) && ch[nx][ny] == '.') {
          if (!flag) {
            cnt++;
            flag = true;
          }
          ch[nx][ny] = '#';
          v2.push_back(PII(nx, ny));
        }
      }
    }

    for (int i = 0; i < v2.size(); i++)
      v1.push_back(v2[i]);

    if (v2.size() == 0)
      return;
  }

  return;
}

int main() {
  /*	freopen("input.txt","r",stdin);
      freopen("output.txt","w",stdout);
      ios_base::sync_with_stdio(false);
      cin.tie(0);
      cout.tie(0);  */

  int i, j, k, l, p, q, x, y, u, v, r, tc, t;

  scanf("%d %d", &n, &m);

  for (i = 0; i < n; i++)
    scanf("%s", ch[i]);

  cnt = 0;

  vector<PII> v1;

  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      if (ch[i][j] == '#') {
        v1.push_back(PII(i, j));
      }
    }
  }

  // memset(vis,0,sizeof(vis));
  solve(v1);

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

  return 0;
}


Fixed submission:
/*

    author : s@if

*/

#include <bits/stdc++.h>
using namespace std;
#define NIL -1
#define fi first
#define sec second
#define MAX INT_MAX
#define INF 99999999
#define ll long long
#define PI acos(-1.0)
#define MOD 1000000007
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define ull unsigned long long
#define triplell pair<pair<ll, ll>, ll>
#define triple pair<pair<int, int>, int>
#define For(i, n) for (int i = 0; i < (int)n; i++)
#define Forn(i, a, b) for (int i = (int)a; i <= (int)b; i++)
#define forba(i, a, b) for (int i = (int)b; i >= (int)a; i--)

bool Check(int N, int pos) { return (bool)(N & (1 << pos)); }
int Set(int N, int pos) { return N = N | (1 << pos); }

int fx[] = {+0, +0, +1, -1, -1, +1, -1, +1};
int fy[] = {-1, +1, +0, +0, +1, +1, -1, -1};
int hr[] = {-2, -2, -1, +1, +2, +2, -1, +1};
int hc[] = {+1, -1, +2, +2, -1, +1, -2, -2};
int dx[] = {+1, -1, +0, +0};
int dy[] = {+0, +0, +1, -1};

const int MAXN = (int)1e5 + 9;
char ch[1009][1009];
int n, m, cnt;

bool chekh(int x, int y) {
  if (x >= 0 && x < n && y >= 0 && y < m)
    return true;

  return false;
}

void solve(vector<PII> v1) {
  int i, j;

  while (1) {
    /*  for(int i = 0;i<v1.size();i++)
          cout<<v1[i].fi<<" "<<v1[i].sec<<endl;

          cout<<endl<<endl; */

    vector<PII> v2;

    bool flag = false;

    for (int i = 0; i < v1.size(); i++) {
      PII u = v1[i];

      for (int j = 0; j < 4; j++) {
        int nx = u.fi + dx[j];
        int ny = u.sec + dy[j];

        if (chekh(nx, ny) && ch[nx][ny] == '.') {
          if (!flag) {
            cnt++;
            flag = true;
          }
          ch[nx][ny] = '#';
          v2.push_back(PII(nx, ny));
        }
      }
    }

    v1 = v2;

    if (v2.size() == 0)
      return;
  }

  return;
}

int main() {
  /*	freopen("input.txt","r",stdin);
      freopen("output.txt","w",stdout);
      ios_base::sync_with_stdio(false);
      cin.tie(0);
      cout.tie(0);  */

  int i, j, k, l, p, q, x, y, u, v, r, tc, t;

  scanf("%d %d", &n, &m);

  for (i = 0; i < n; i++)
    scanf("%s", ch[i]);

  cnt = 0;

  vector<PII> v1;

  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      if (ch[i][j] == '#') {
        v1.push_back(PII(i, j));
      }
    }
  }

  // memset(vis,0,sizeof(vis));
  solve(v1);

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

  return 0;
}