Problem p03436 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep0(i, n) for (ll(i) = 0; (i) < (n); ++(i))
#define rrep0(i, n) for (ll(i) = (n)-1; (i) >= 0; --(i))
#define rep1(i, n) for (ll(i) = 1; (i) <= (n); ++(i))
#define rrep1(i, n) for (ll(i) = (n); (i) >= 1; --(i))
#define nfor(i, a, b) for (ll(i) = (a); (i) < (b); ++(i))
#define rnfor(i, a, b) for (ll(i) = (a)-1; (i) >= (b); --(i))
#define ll long long
#define mod (1000000007)
#define pf(x) cout << (x) << endl
#define all(x) (x).begin(), (x).end()

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}

ll factorial(ll n) {
  ll t = 1;
  rep1(i, n) t *= i;
  return t;
}

ll combinations(ll n, ll k) {
  ll r = 1;
  rep1(i, k) r = r * n-- / i;
  return r;
}

int main() {
  int h, w;
  ll ans = 0;
  int x[] = {1, -1, 0, 0}, y[] = {0, 0, 1, -1};
  cin >> h >> w;
  vector<vector<bool>> g(h + 2, vector<bool>(w + 2, false));
  rep1(i, h) rep1(j, w) {
    char s;
    cin >> s;
    if (s == '.') {
      g[i][j] = true;
      ++ans;
    }
  }

  queue<pair<int, int>> to, del;
  ll cnt = 1;
  to.push(make_pair(1, 1));
  while (!to.empty()) {
    ++cnt;
    queue<pair<int, int>> from;
    from = to;
    to = del;
    while (!from.empty()) {
      g[from.front().first][from.front().second] = false;
      rep0(i, 4) {
        if (g[from.front().first + x[i]][from.front().second + y[i]]) {
          if (from.front().first + x[i] == h &&
              from.front().second + y[i] == w) {
            pf(ans - cnt);
            return 0;
          } else {
            to.push(make_pair(from.front().first + x[i],
                              from.front().second + y[i]));
          }
        }
      }
      from.pop();
    }
  }
  pf(-1);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep0(i, n) for (ll(i) = 0; (i) < (n); ++(i))
#define rrep0(i, n) for (ll(i) = (n)-1; (i) >= 0; --(i))
#define rep1(i, n) for (ll(i) = 1; (i) <= (n); ++(i))
#define rrep1(i, n) for (ll(i) = (n); (i) >= 1; --(i))
#define nfor(i, a, b) for (ll(i) = (a); (i) < (b); ++(i))
#define rnfor(i, a, b) for (ll(i) = (a)-1; (i) >= (b); --(i))
#define ll long long
#define mod (1000000007)
#define pf(x) cout << (x) << endl
#define all(x) (x).begin(), (x).end()

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}

ll factorial(ll n) {
  ll t = 1;
  rep1(i, n) t *= i;
  return t;
}

ll combinations(ll n, ll k) {
  ll r = 1;
  rep1(i, k) r = r * n-- / i;
  return r;
}

int main() {
  int h, w;
  ll ans = 0;
  int x[] = {1, -1, 0, 0}, y[] = {0, 0, 1, -1};
  cin >> h >> w;
  vector<vector<bool>> g(h + 2, vector<bool>(w + 2, false));
  rep1(i, h) rep1(j, w) {
    char s;
    cin >> s;
    if (s == '.') {
      g[i][j] = true;
      ++ans;
    }
  }

  queue<pair<int, int>> to, del;
  ll cnt = 1;
  to.push(make_pair(1, 1));
  while (!to.empty()) {
    ++cnt;
    queue<pair<int, int>> from;
    from = to;
    to = del;
    while (!from.empty()) {
      g[from.front().first][from.front().second] = false;
      rep0(i, 4) {
        if (g[from.front().first + x[i]][from.front().second + y[i]]) {
          if (from.front().first + x[i] == h &&
              from.front().second + y[i] == w) {
            pf(ans - cnt);
            return 0;
          } else {
            to.push(make_pair(from.front().first + x[i],
                              from.front().second + y[i]));
            g[from.front().first + x[i]][from.front().second + y[i]] = false;
          }
        }
      }
      from.pop();
    }
  }
  pf(-1);
  return 0;
}