Problem p03053 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <queue>
#include <set>

using namespace std;

typedef long long ll;

struct cell {
  ll i, j;
  cell(ll _i, ll _j) {
    i = _i;
    j = _j;
  }
};

int main(void) {
  ll H, W;
  ll A[1000][1000];
  cin >> H >> W;
  for (ll i = 0; i < H; i++) {
    for (ll j = 0; j < W; j++) {
      char c;
      cin >> c;
      if (c == '.') {
        A[i][j] = 0;
      } else if (c == '#') {
        A[i][j] = 1;
      } else
        break;
    }
  }

  ll d[H][W];
  queue<cell> que;
  for (ll i = 0; i < H; i++) {
    for (ll j = 0; j < W; j++) {
      d[i][j] = 2000;
      if (A[i][j] == 1) {
        d[i][j] = 0;
        if (i > 0)
          que.push(cell(i - 1, j));
        if (i < H - 1)
          que.push(cell(i + 1, j));
        if (j > 0)
          que.push(cell(i, j - 1));
        if (j < W - 1)
          que.push(cell(i, j + 1));
      }
    }
  }

  while (!que.empty()) {
    cell c = que.front();
    ll i = c.i, j = c.j;
    if (i > 0) {
      if (d[i][j] > d[i - 1][j] + 1) {
        d[i][j] = d[i - 1][j] + 1;
      }
    }
    if (i < H - 1) {
      if (d[i][j] > d[i + 1][j] + 1) {
        d[i][j] = d[i + 1][j] + 1;
      }
    }
    if (j > 0) {
      if (d[i][j] > d[i][j - 1] + 1) {
        d[i][j] = d[i][j - 1] + 1;
      }
    }
    if (j < W - 1) {
      if (d[i][j] > d[i][j + 1] + 1) {
        d[i][j] = d[i][j + 1] + 1;
      }
    }
    if (i > 0) {
      if (d[i][j] + 1 < d[i - 1][j]) {
        que.push(cell(i - 1, j));
      }
    }
    if (i < H - 1) {
      if (d[i][j] + 1 < d[i + 1][j]) {
        que.push(cell(i + 1, j));
      }
    }
    if (j > 0) {
      if (d[i][j] + 1 < d[i][j - 1]) {
        que.push(cell(i, j - 1));
      }
    }
    if (j < W - 1) {
      if (d[i][j] + 1 < d[i][j + 1]) {
        que.push(cell(i, j + 1));
      }
    }
    que.pop();
  }

  ll ans = 0;
  for (ll i = 0; i < H; i++) {
    for (ll j = 0; j < W; j++) {
      ans = max(ans, d[i][j]);
    }
  }

  cout << ans << endl;
}


Fixed submission:
#include <iostream>
#include <queue>
#include <set>

using namespace std;

typedef long long ll;

struct cell {
  ll i, j;
  cell(ll _i, ll _j) {
    i = _i;
    j = _j;
  }
};

int main(void) {
  ll H, W;
  ll A[1000][1000];
  cin >> H >> W;
  for (ll i = 0; i < H; i++) {
    for (ll j = 0; j < W; j++) {
      char c;
      cin >> c;
      if (c == '.') {
        A[i][j] = 0;
      } else if (c == '#') {
        A[i][j] = 1;
      } else
        break;
    }
  }

  ll d[H][W];
  queue<cell> que;
  for (ll i = 0; i < H; i++) {
    for (ll j = 0; j < W; j++) {
      d[i][j] = 2000;
      if (A[i][j] == 1) {
        d[i][j] = 0;
        if (i > 0)
          que.push(cell(i - 1, j));
        if (i < H - 1)
          que.push(cell(i + 1, j));
        if (j > 0)
          que.push(cell(i, j - 1));
        if (j < W - 1)
          que.push(cell(i, j + 1));
      }
    }
  }

  while (!que.empty()) {
    cell c = que.front();
    ll i = c.i, j = c.j;
    if (d[i][j] < 2000) {
      que.pop();
      continue;
    }
    if (i > 0) {
      if (d[i][j] > d[i - 1][j] + 1) {
        d[i][j] = d[i - 1][j] + 1;
      }
    }
    if (i < H - 1) {
      if (d[i][j] > d[i + 1][j] + 1) {
        d[i][j] = d[i + 1][j] + 1;
      }
    }
    if (j > 0) {
      if (d[i][j] > d[i][j - 1] + 1) {
        d[i][j] = d[i][j - 1] + 1;
      }
    }
    if (j < W - 1) {
      if (d[i][j] > d[i][j + 1] + 1) {
        d[i][j] = d[i][j + 1] + 1;
      }
    }
    if (i > 0) {
      if (d[i][j] + 1 < d[i - 1][j]) {
        que.push(cell(i - 1, j));
      }
    }
    if (i < H - 1) {
      if (d[i][j] + 1 < d[i + 1][j]) {
        que.push(cell(i + 1, j));
      }
    }
    if (j > 0) {
      if (d[i][j] + 1 < d[i][j - 1]) {
        que.push(cell(i, j - 1));
      }
    }
    if (j < W - 1) {
      if (d[i][j] + 1 < d[i][j + 1]) {
        que.push(cell(i, j + 1));
      }
    }
    que.pop();
  }

  ll ans = 0;
  for (ll i = 0; i < H; i++) {
    for (ll j = 0; j < W; j++) {
      ans = max(ans, d[i][j]);
    }
  }

  cout << ans << endl;
}