Problem p03436 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n - 1); i >= 0; --i)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> P;

const int MAX = 200005;
const int INF = 1001001001;
const int MOD = 1000000007;

int main() {
  int H, W;
  cin >> H >> W;

  vector<string> S(H);
  rep(i, H) cin >> S[i];

  queue<P> q;
  q.emplace(0, 0);

  bool res = false;
  int used[60][60];
  rep(i, H) rep(j, W) used[i][j] = INF;
  used[0][0] = 0;

  while (!q.empty()) {
    P p = q.front();
    q.pop();
    int y = p.first, x = p.second;
    if (y == H - 1 && x == W - 1) {
      res = true;
      break;
    }
    if (y + 1 < H && used[y + 1][x] == INF && S[y + 1][x] == '.') {
      used[y + 1][x] = used[y][x] + 1;
      q.emplace(y + 1, x);
    }
    if (0 <= y - 1 && used[y - 1][x] == INF && S[y - 1][x] == '.') {
      used[y - 1][x] = used[y][x] + 1;
      q.emplace(y - 1, x);
    }
    if (x + 1 < W && used[y][x + 1] == INF && S[y][x + 1] == '.') {
      used[y][x + 1] = used[y][x] + 1;
      q.emplace(y, x + 1);
    }
    if (0 <= x - 1 && used[y][x - 1] == INF && S[y][x - 1] == '.') {
      used[y][x - 1] = used[y][x] + 1;
      q.emplace(y, x - 1);
    }
  }

  if (!res) {
    cout << -1 << endl;
    return 0;
  }

  vector<P> root;
  root.emplace_back(H - 1, W - 1);
  int y = H - 1, x = W - 1;
  while (0 < y || 0 < x) {
    if (0 < y && used[y - 1][x] < used[y][x]) {
      y--;
    } else if (0 < x && used[y][x - 1] < used[y][x]) {
      x--;
    }
    root.emplace_back(y, x);
  }

  int ans = 0;
  rep(i, H) rep(j, W) {
    if (S[i][j] == '#')
      continue;
    P pos = P(i, j);
    bool res = true;
    for (auto &p : root) {
      if (pos == p)
        res = false;
    }
    if (res)
      ans++;
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n - 1); i >= 0; --i)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> P;

const int MAX = 200005;
const int INF = 1001001001;
const int MOD = 1000000007;

int main() {
  int H, W;
  cin >> H >> W;

  vector<string> S(H);
  rep(i, H) cin >> S[i];

  queue<P> q;
  q.emplace(0, 0);

  bool res = false;
  int used[60][60];
  rep(i, H) rep(j, W) used[i][j] = INF;
  used[0][0] = 0;

  while (!q.empty()) {
    P p = q.front();
    q.pop();
    int y = p.first, x = p.second;
    if (y == H - 1 && x == W - 1) {
      res = true;
      break;
    }
    if (y + 1 < H && used[y + 1][x] == INF && S[y + 1][x] == '.') {
      used[y + 1][x] = used[y][x] + 1;
      q.emplace(y + 1, x);
    }
    if (0 <= y - 1 && used[y - 1][x] == INF && S[y - 1][x] == '.') {
      used[y - 1][x] = used[y][x] + 1;
      q.emplace(y - 1, x);
    }
    if (x + 1 < W && used[y][x + 1] == INF && S[y][x + 1] == '.') {
      used[y][x + 1] = used[y][x] + 1;
      q.emplace(y, x + 1);
    }
    if (0 <= x - 1 && used[y][x - 1] == INF && S[y][x - 1] == '.') {
      used[y][x - 1] = used[y][x] + 1;
      q.emplace(y, x - 1);
    }
  }

  if (!res) {
    cout << -1 << endl;
    return 0;
  }

  vector<P> root;
  root.emplace_back(H - 1, W - 1);
  int y = H - 1, x = W - 1;
  while (0 < y || 0 < x) {
    if (0 < y && used[y - 1][x] < used[y][x]) {
      y--;
    } else if (0 < x && used[y][x - 1] < used[y][x]) {
      x--;
    } else if (y + 1 < H && used[y + 1][x] < used[y][x]) {
      y++;
    } else if (x + 1 < W && used[y][x + 1] < used[y][x]) {
      x++;
    }
    root.emplace_back(y, x);
  }

  int ans = 0;
  rep(i, H) rep(j, W) {
    if (S[i][j] == '#')
      continue;
    P pos = P(i, j);
    bool res = true;
    for (auto &p : root) {
      if (pos == p)
        res = false;
    }
    if (res)
      ans++;
  }

  cout << ans << endl;
}