Problem p03436 (C++)

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

#ifdef LOCAL
#include "../lib/dump.hpp"
#else
#define dump(...)
#define dumpv(...)
#endif

typedef long long ll;
typedef pair<int, int> P;
const int MOD = 1e9 + 7;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;

typedef tuple<int, int, int> T;

void solve() {
  int H, W;
  cin >> H >> W;
  vector<vector<int>> s(H, vector<int>(W));
  int sum = 0;
  for (int i = 0; i < H; i++) {
    string ss;
    cin >> ss;
    for (int j = 0; j < W; j++) {
      if (ss[j] == '#') {
        s[i][j] = 1;
        sum++;
      }
    }
  }
  dumpv(s, H, W);
  deque<T> q;
  vector<vector<int>> used(H, vector<int>(W, INF));
  q.push_back(T(0, 0, 0));
  while (!q.empty()) {
    int x, y, len;
    tie(x, y, len) = q.front();
    q.pop_front();
    dump(x, y);
    used[y][x] = len;
    for (int i = 0; i < 4; i++) {
      int nx, ny;
      if (i == 0)
        nx = x - 1, ny = y;
      else if (i == 1)
        nx = x + 1, ny = y;
      else if (i == 2)
        nx = x, ny = y - 1;
      else
        nx = x, ny = y + 1;
      if (nx < 0 || W <= nx || ny < 0 || H <= ny)
        continue;
      if (s[ny][nx])
        continue;
      if (used[ny][nx] <= len + 1)
        continue;
      q.push_back(T(nx, ny, len + 1));
    }
  }
  dumpv(used, H, W);
  if (used[H - 1][W - 1] == INF) {
    cout << -1 << endl;
    return;
  }
  int ans = H * W - sum - (used[H - 1][W - 1] + 1);
  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // freopen("temp.1", "r", stdin);
  solve();
  return 0;
}

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

#ifdef LOCAL
#include "../lib/dump.hpp"
#else
#define dump(...)
#define dumpv(...)
#endif

typedef long long ll;
typedef pair<int, int> P;
const int MOD = 1e9 + 7;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;

typedef tuple<int, int, int> T;

void solve() {
  int H, W;
  cin >> H >> W;
  vector<vector<int>> s(H, vector<int>(W));
  int sum = 0;
  for (int i = 0; i < H; i++) {
    string ss;
    cin >> ss;
    for (int j = 0; j < W; j++) {
      if (ss[j] == '#') {
        s[i][j] = 1;
        sum++;
      }
    }
  }
  dumpv(s, H, W);
  deque<T> q;
  vector<vector<int>> used(H, vector<int>(W, INF));
  q.push_back(T(0, 0, 0));
  while (!q.empty()) {
    int x, y, len;
    tie(x, y, len) = q.front();
    q.pop_front();
    dump(x, y);
    if (used[y][x] <= len + 1)
      continue;
    used[y][x] = len;
    for (int i = 0; i < 4; i++) {
      int nx, ny;
      if (i == 0)
        nx = x - 1, ny = y;
      else if (i == 1)
        nx = x + 1, ny = y;
      else if (i == 2)
        nx = x, ny = y - 1;
      else
        nx = x, ny = y + 1;
      if (nx < 0 || W <= nx || ny < 0 || H <= ny)
        continue;
      if (s[ny][nx])
        continue;
      if (used[ny][nx] <= len + 1)
        continue;
      q.push_back(T(nx, ny, len + 1));
    }
  }
  dumpv(used, H, W);
  if (used[H - 1][W - 1] == INF) {
    cout << -1 << endl;
    return;
  }
  int ans = H * W - sum - (used[H - 1][W - 1] + 1);
  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // freopen("temp.1", "r", stdin);
  solve();
  return 0;
}