Problem p03436 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <fstream>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

#define repeat(i, n) for (int i = 0; (i) < (n); ++(i))

using namespace std;

typedef long long ll;
typedef pair<int, int> Pii;
typedef vector<int> vint;
const ll INF = LONG_LONG_MAX;
const int MAX_N = 112345;

int dirx[4] = {0, 1, 0, -1};
int diry[4] = {1, 0, -1, 0};

struct point {
  int x, y, d;
  point(int _x, int _y, int _d) : x(_x), y(_y), d(_d) {}
};

int bfs(vector<vector<int>> &fld, int w, int h) {
  int ret = -1;
  queue<point> Q;
  Q.push(point(1, 1, 1));
  while (!Q.empty()) {
    point p = Q.front();
    Q.pop();
    if (p.x == w && p.y == h) {
      ret = p.d;
      break;
    }
    fld[p.x][p.y] = -1;
    repeat(d, 4) {
      int nx = p.x + dirx[d];
      int ny = p.y + diry[d];
      if (fld[nx][ny] > 0) {
        Q.push(point(nx, ny, p.d + 1));
      }
    }
  }
  return ret;
}

int main() {
  // insert code here...    return 0;
  //////
  // input from txt
  /*
   std::ifstream in("input.txt");
   std::cin.rdbuf(in.rdbuf());
   std::ofstream out("output.txt");
   std::cout.rdbuf(out.rdbuf());
   ////
   */
  int H, W;
  cin >> H >> W;
  vector<vector<int>> fld(W + 2, vector<int>(H + 2, -1));
  int dot_num = 0;
  repeat(i, H) {
    repeat(j, W) {
      char c;
      cin >> c;
      if (c == '.') {
        fld[j + 1][i + 1] = 1;
        dot_num++;
      }
    }
  }
  int ans = dot_num - bfs(fld, W, H);
  if (ans > dot_num) {
    ans = -1;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <fstream>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

#define repeat(i, n) for (int i = 0; (i) < (n); ++(i))

using namespace std;

typedef long long ll;
typedef pair<int, int> Pii;
typedef vector<int> vint;
const ll INF = LONG_LONG_MAX;
const int MAX_N = 112345;

int dirx[4] = {0, 1, 0, -1};
int diry[4] = {1, 0, -1, 0};

struct point {
  int x, y, d;
  point(int _x, int _y, int _d) : x(_x), y(_y), d(_d) {}
};

int bfs(vector<vector<int>> &fld, int w, int h) {
  int ret = -1;
  queue<point> Q;
  Q.push(point(1, 1, 1));
  while (!Q.empty()) {
    point p = Q.front();
    Q.pop();
    if (p.x == w && p.y == h) {
      ret = p.d;
      break;
    }
    if (fld[p.x][p.y] < 0)
      continue;
    fld[p.x][p.y] = -1;
    repeat(d, 4) {
      int nx = p.x + dirx[d];
      int ny = p.y + diry[d];
      if (fld[nx][ny] > 0) {
        Q.push(point(nx, ny, p.d + 1));
      }
    }
  }
  return ret;
}

int main() {
  // insert code here...    return 0;
  //////
  // input from txt
  /*
   std::ifstream in("input.txt");
   std::cin.rdbuf(in.rdbuf());
   std::ofstream out("output.txt");
   std::cout.rdbuf(out.rdbuf());
   ////
   */
  int H, W;
  cin >> H >> W;
  vector<vector<int>> fld(W + 2, vector<int>(H + 2, -1));
  int dot_num = 0;
  repeat(i, H) {
    repeat(j, W) {
      char c;
      cin >> c;
      if (c == '.') {
        fld[j + 1][i + 1] = 1;
        dot_num++;
      }
    }
  }
  int ans = dot_num - bfs(fld, W, H);
  if (ans > dot_num) {
    ans = -1;
  }
  cout << ans << endl;
  return 0;
}