Problem p03053 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <numeric>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_set>
#include <vector>
using namespace std;

using INT = long long;
using UINT = unsigned long long;

unsigned gcd(unsigned a, unsigned b) {
  while (1) {
    if (a < b)
      swap(a, b);
    if (!b)
      break;
    a %= b;
  }
  return a;
}

int main() {
  int h, w;

  cin >> h >> w;

  vector<vector<char>> a(h + 2, vector<char>(w + 2));

  int bc = 0;
  for (size_t i = 1; i < h + 1; i++) {
    cin >> (a[i].data() + 1);
    for (size_t j = 1; j < w + 1; j++) {
      bc += (a[i][j] == '#') ? 1 : 0;
    }
  }

  if (bc < h * w / 4) {
    vector<vector<INT>> d(h, vector<INT>(w, 10000));

    for (INT x = 0; x < w; x++) {
      for (INT y = 0; y < h; y++) {
        if (a[y + 1][x + 1] == '#') {
          for (INT x2 = 0; x2 < w; x2++) {
            for (INT y2 = 0; y2 < h; y2++) {
              d[y2][x2] = min(d[y2][x2], abs(x - x2) + abs(y - y2));
            }
          }
        }
      }
    }

    INT ans = 0;
    for (size_t x = 0; x < w; x++) {
      for (size_t y = 0; y < h; y++) {
        ans = max(d[y][x], ans);
      }
    }

    cout << ans << endl;
  } else {
    bool con;
    int ans = 0;

    while (true) {
      con = false;
      for (size_t x = 1; x < w + 1; x++) {
        for (size_t y = 1; y < h + 1; y++) {
          if (a[y][x] == '.') {
            con = true;
          }
          if (a[y][x] == '+') {
            a[y][x] = '#';
          }
        }
      }
      if (!con) {
        break;
      }
      for (size_t x = 1; x < w + 1; x++) {
        for (size_t y = 1; y < h + 1; y++) {
          if (a[y][x] == '#') {
            a[y - 1][x] = a[y - 1][x] == '.' ? '+' : a[y - 1][x];
            a[y + 1][x] = a[y + 1][x] == '.' ? '+' : a[y + 1][x];
            a[y][x - 1] = a[y][x - 1] == '.' ? '+' : a[y][x - 1];
            a[y][x + 1] = a[y][x + 1] == '.' ? '+' : a[y][x + 1];
          }
        }
      }
      ans++;
    }

    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <numeric>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_set>
#include <vector>
using namespace std;

using INT = long long;
using UINT = unsigned long long;

unsigned gcd(unsigned a, unsigned b) {
  while (1) {
    if (a < b)
      swap(a, b);
    if (!b)
      break;
    a %= b;
  }
  return a;
}

int main() {
  int h, w;

  cin >> h >> w;

  vector<vector<char>> a(h + 2, vector<char>(w + 2));

  int bc = 0;
  for (size_t i = 1; i < h + 1; i++) {
    cin >> (a[i].data() + 1);
    for (size_t j = 1; j < w + 1; j++) {
      bc += (a[i][j] == '#') ? 1 : 0;
    }
  }

  if (bc < h * w / 10) {
    vector<vector<INT>> d(h, vector<INT>(w, 10000));

    for (INT x = 0; x < w; x++) {
      for (INT y = 0; y < h; y++) {
        if (a[y + 1][x + 1] == '#') {
          for (INT x2 = 0; x2 < w; x2++) {
            for (INT y2 = 0; y2 < h; y2++) {
              d[y2][x2] = min(d[y2][x2], abs(x - x2) + abs(y - y2));
            }
          }
        }
      }
    }

    INT ans = 0;
    for (size_t x = 0; x < w; x++) {
      for (size_t y = 0; y < h; y++) {
        ans = max(d[y][x], ans);
      }
    }

    cout << ans << endl;
  } else {
    bool con;
    int ans = 0;

    while (true) {
      con = false;
      for (size_t x = 1; x < w + 1; x++) {
        for (size_t y = 1; y < h + 1; y++) {
          if (a[y][x] == '.') {
            con = true;
          }
          if (a[y][x] == '+') {
            a[y][x] = '#';
          }
        }
      }
      if (!con) {
        break;
      }
      for (size_t x = 1; x < w + 1; x++) {
        for (size_t y = 1; y < h + 1; y++) {
          if (a[y][x] == '#') {
            a[y - 1][x] = a[y - 1][x] == '.' ? '+' : a[y - 1][x];
            a[y + 1][x] = a[y + 1][x] == '.' ? '+' : a[y + 1][x];
            a[y][x - 1] = a[y][x - 1] == '.' ? '+' : a[y][x - 1];
            a[y][x + 1] = a[y][x + 1] == '.' ? '+' : a[y][x + 1];
          }
        }
      }
      ans++;
    }

    cout << ans << endl;
  }
  return 0;
}