Problem p03436 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1},
          dy[] = {0, -1, 0, 1, 1, -1, -1, 1};

bool isvalid(vector<vector<int>> &dist, int x, int y) {
  int H = dist.size();
  int W = dist[0].size();
  return x >= 0 && x < W && y >= 0 && y < H;
}

void dfs(vector<string> &M, vector<vector<int>> &dist, int x, int y) {
  queue<pair<int, pair<int, int>>> q;
  q.push(mp(0, mp(0, 0)));

  while (!q.empty()) {
    pair<int, pair<int, int>> p = q.front();
    q.pop();
    int d = p.first;
    int x = p.second.first;
    int y = p.second.second;
    if (dist[y][x] < d)
      continue;
    dist[y][x] = d;
    for (int k = 0; k < 4; k++) {
      int tx = x + dx[k];
      int ty = y + dy[k];
      if (!isvalid(dist, tx, ty))
        continue;
      if (M[ty][tx] == '#')
        continue;
      if (dist[ty][tx] > d + 1) {
        q.push(mp(d + 1, mp(tx, ty)));
      }
    }
  }
}

int main() {
  int H, W;
  cin >> H >> W;
  vector<string> M;
  for (int i = 0; i < H; i++) {
    string s;
    cin >> s;
    M.push_back(s);
  }

  vector<vector<int>> dist(H, vector<int>(W, INF));
  dfs(M, dist, 0, 0);

  int k = dist[H - 1][W - 1];
  if (k == INF) {
    cout << -1 << endl;
    return 0;
  }
  int cnt = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (M[i][j] == '#')
        cnt++;
    }
  }
  cout << H * W - (k + 1) - cnt << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1},
          dy[] = {0, -1, 0, 1, 1, -1, -1, 1};

bool isvalid(vector<vector<int>> &dist, int x, int y) {
  int H = dist.size();
  int W = dist[0].size();
  return x >= 0 && x < W && y >= 0 && y < H;
}

void dfs(vector<string> &M, vector<vector<int>> &dist, int x, int y) {
  queue<pair<int, pair<int, int>>> q;
  q.push(mp(0, mp(0, 0)));

  while (!q.empty()) {
    pair<int, pair<int, int>> p = q.front();
    q.pop();
    int d = p.first;
    int x = p.second.first;
    int y = p.second.second;
    if (dist[y][x] <= d)
      continue;
    dist[y][x] = d;
    for (int k = 0; k < 4; k++) {
      int tx = x + dx[k];
      int ty = y + dy[k];
      if (!isvalid(dist, tx, ty))
        continue;
      if (M[ty][tx] == '#')
        continue;
      if (dist[ty][tx] > d + 1) {
        q.push(mp(d + 1, mp(tx, ty)));
      }
    }
  }
}

int main() {
  int H, W;
  cin >> H >> W;
  vector<string> M;
  for (int i = 0; i < H; i++) {
    string s;
    cin >> s;
    M.push_back(s);
  }

  vector<vector<int>> dist(H, vector<int>(W, INF));
  dfs(M, dist, 0, 0);

  int k = dist[H - 1][W - 1];
  if (k == INF) {
    cout << -1 << endl;
    return 0;
  }
  int cnt = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (M[i][j] == '#')
        cnt++;
    }
  }
  cout << H * W - (k + 1) - cnt << endl;

  return 0;
}