Problem p03436 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h> // ver2.1.9
#define int long long
#define endl "\n"
#define ALL(v) (v).begin(), (v).end()
#define Vi vector<int>
#define Pii pair<int, int>
#define Tiii tuple<int, int, int>
#define PQi priority_queue<int>
#define PQir priority_queue<int, vector<int>, greater<int>>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define cYES cout << "YES" << endl
#define cNO cout << "NO" << endl
#define cYes cout << "Yes" << endl
#define cNo cout << "No" << endl
#define sortr(v) sort(v, greater<>())
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repeq(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = a; i > b; i--)
#define repreq(i, a, b) for (int i = a; i >= b; i--)
#define leng(n) (int)(log10(n) + 1)
#define dem(a, b) ((a + b - 1) / (b))
#define Vout(a)                                                                \
  rep(lZ, 0, a.size() - 1) cout << a[lZ] << ' ';                               \
  cout << a.back() << endl
#define VVout(a)                                                               \
  rep(lW, 0, a.size()) { Vout(a[lW]); }
#define INF 9000000000000000000 //  9.0 * 10^18
#define MOD 1000000007          //  10^9 + 7
using namespace std;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } //  最大公約数gcd
int lcm(int a, int b) { return a / gcd(a, b) * b; }     //  最小公倍数lcm
void uniq(Vi &v) {
  sort(ALL(v));
  v.erase(unique(ALL(v)), v.end());
} //  uniqのやつ

int mypow(int x, int n, int m) { //  累乗x^n(mod m)  O(log(n))
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return mypow(x * x % m, n / 2, m);
  else
    return x * mypow(x, n - 1, m) % m;
}

signed main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);

  int h, w, bl = 0, minn;
  cin >> h >> w;
  minn = min(h, w);
  vector<string> maze(h);
  rep(i, 0, h) cin >> maze[i];

  Pii S = mp(0, 0), G = mp(h - 1, w - 1);

  rep(i, 0, h) {
    rep(j, 0, w) {
      if (maze[i][j] == '#')
        bl++;
    }
  }
  int wh = h * w - bl;

  if (bl < minn) {
    cout << (h - 1) * (w - 1) - bl << endl;
    return 0;
  }

  // ここから

  vector<string> maze2 = maze;
  stack<Pii> st2;
  st2.push(S);
  int x2, y2;
  bool ok = false;

  while (true) {
    while (!st2.empty()) {
      x2 = st2.top().first;
      y2 = st2.top().second;
      st2.pop();
      if (mp(x2, y2) == G) {
        ok = true;
        break;
      }

      if (y2 + 1 < w) {
        if (maze2[x2][y2 + 1] == '.')
          st2.push(mp(x2, y2 + 1));
      }
      if (y2 - 1 >= 0) {
        if (maze2[x2][y2 - 1] == '.')
          st2.push(mp(x2, y2 - 1));
      }
      if (x2 + 1 < h) {
        if (maze2[x2 + 1][y2] == '.')
          st2.push(mp(x2 + 1, y2));
      }
      if (x2 - 1 >= 0) {
        if (maze2[x2 - 1][y2] == '.')
          st2.push(mp(x2 - 1, y2));
      }
      maze2[x2][y2] = '#';
    }
    if (ok)
      break;
    cout << -1 << endl;
    return 0;
  }

  // 　　ここまで

  queue<Pii> st;
  st.push(S);
  int x, y, ct = 0, now;

  while (!st.empty()) {
    now = st.size();
    // if(now >= 40) {cout << (h-1)*(w-1)-bl << endl;return 0;}
    ct++;
    rep(i, 0, now) {
      x = st.front().first;
      y = st.front().second;
      st.pop();
      if (mp(x, y) == G) {
        cout << wh - ct << endl;
        return 0;
      }

      if (y + 1 < w) {
        if (maze[x][y + 1] == '.')
          st.push(mp(x, y + 1));
      }
      if (y - 1 >= 0) {
        if (maze[x][y - 1] == '.')
          st.push(mp(x, y - 1));
      }
      if (x + 1 < h) {
        if (maze[x + 1][y] == '.')
          st.push(mp(x + 1, y));
      }
      if (x - 1 >= 0) {
        if (maze[x - 1][y] == '.')
          st.push(mp(x - 1, y));
      }

      // VVout(maze);
      maze[x][y] = '#';
    }
  }

  cout << -1 << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h> // ver2.1.9
#define int long long
#define endl "\n"
#define ALL(v) (v).begin(), (v).end()
#define Vi vector<int>
#define Pii pair<int, int>
#define Tiii tuple<int, int, int>
#define PQi priority_queue<int>
#define PQir priority_queue<int, vector<int>, greater<int>>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define cYES cout << "YES" << endl
#define cNO cout << "NO" << endl
#define cYes cout << "Yes" << endl
#define cNo cout << "No" << endl
#define sortr(v) sort(v, greater<>())
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repeq(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = a; i > b; i--)
#define repreq(i, a, b) for (int i = a; i >= b; i--)
#define leng(n) (int)(log10(n) + 1)
#define dem(a, b) ((a + b - 1) / (b))
#define Vout(a)                                                                \
  rep(lZ, 0, a.size() - 1) cout << a[lZ] << ' ';                               \
  cout << a.back() << endl
#define VVout(a)                                                               \
  rep(lW, 0, a.size()) { Vout(a[lW]); }
#define INF 9000000000000000000 //  9.0 * 10^18
#define MOD 1000000007          //  10^9 + 7
using namespace std;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } //  最大公約数gcd
int lcm(int a, int b) { return a / gcd(a, b) * b; }     //  最小公倍数lcm
void uniq(Vi &v) {
  sort(ALL(v));
  v.erase(unique(ALL(v)), v.end());
} //  uniqのやつ

int mypow(int x, int n, int m) { //  累乗x^n(mod m)  O(log(n))
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return mypow(x * x % m, n / 2, m);
  else
    return x * mypow(x, n - 1, m) % m;
}

signed main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);

  int h, w, bl = 0, minn;
  cin >> h >> w;
  minn = min(h, w);
  vector<string> maze(h);
  rep(i, 0, h) cin >> maze[i];

  Pii S = mp(0, 0), G = mp(h - 1, w - 1);

  rep(i, 0, h) {
    rep(j, 0, w) {
      if (maze[i][j] == '#')
        bl++;
    }
  }
  int wh = h * w - bl;

  if (bl < minn) {
    cout << (h - 1) * (w - 1) - bl << endl;
    return 0;
  }

  // ここから

  vector<string> maze2 = maze;
  stack<Pii> st2;
  st2.push(S);
  int x2, y2;
  bool ok = false;

  while (true) {
    while (!st2.empty()) {
      x2 = st2.top().first;
      y2 = st2.top().second;
      st2.pop();
      if (mp(x2, y2) == G) {
        ok = true;
        break;
      }

      if (y2 + 1 < w) {
        if (maze2[x2][y2 + 1] == '.')
          st2.push(mp(x2, y2 + 1));
      }
      if (y2 - 1 >= 0) {
        if (maze2[x2][y2 - 1] == '.')
          st2.push(mp(x2, y2 - 1));
      }
      if (x2 + 1 < h) {
        if (maze2[x2 + 1][y2] == '.')
          st2.push(mp(x2 + 1, y2));
      }
      if (x2 - 1 >= 0) {
        if (maze2[x2 - 1][y2] == '.')
          st2.push(mp(x2 - 1, y2));
      }
      maze2[x2][y2] = '#';
    }
    if (ok)
      break;
    cout << -1 << endl;
    return 0;
  }

  // 　　ここまで

  queue<Pii> st;
  st.push(S);
  int x, y, ct = 0, now;

  while (!st.empty()) {
    now = st.size();
    if (now >= 10000) {
      cout << (h - 1) * (w - 1) - bl << endl;
      return 0;
    }
    ct++;
    rep(i, 0, now) {
      x = st.front().first;
      y = st.front().second;
      st.pop();
      if (mp(x, y) == G) {
        cout << wh - ct << endl;
        return 0;
      }

      if (y + 1 < w) {
        if (maze[x][y + 1] == '.')
          st.push(mp(x, y + 1));
      }
      if (y - 1 >= 0) {
        if (maze[x][y - 1] == '.')
          st.push(mp(x, y - 1));
      }
      if (x + 1 < h) {
        if (maze[x + 1][y] == '.')
          st.push(mp(x + 1, y));
      }
      if (x - 1 >= 0) {
        if (maze[x - 1][y] == '.')
          st.push(mp(x - 1, y));
      }

      // VVout(maze);
      maze[x][y] = '#';
    }
  }

  cout << -1 << endl;
  return 0;
}