Problem p03436 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex> // complex<double> a(1.2 , 2.3);// real(): 1.2, imag()2.3
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

#define MOD 1000000007
#define ll long long
#define ld long double
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define rep(i, n) FOR(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define rmsame(a) sort(all(a)), a.erase(unique(all(a)), a.end())
#define rmvector(a, b)                                                         \
  rep(i, a.size()) rep(j, b.size()) if (a[i] == b[j]) {                        \
    a.erase(a.begin() + i);                                                    \
    i--;                                                                       \
    break;                                                                     \
  }
template <typename X> bool exist(vector<X> vec, X item) {
  return find(all(vec), item) != vec.end();
}

char masu[55][55];
ll moves[55][55];
ll H, W;

void M(int h, int w, int deepth) {
  if (h >= H || w >= W)
    return;
  if (h < 0 || w < 0)
    return;
  if (masu[h][w] != '.')
    return;
  if (moves[h][w] < deepth && moves[h][w] != 0)
    return;

  moves[h][w] = deepth;
  M(h + 1, w, deepth + 1);
  M(h - 1, w, deepth + 1);
  M(h, w + 1, deepth + 1);
  M(h, w - 1, deepth + 1);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> H >> W;
  ll d = 0, s = 0; // dot, sharpe
  rep(i, H) {
    string inp;
    cin >> inp;
    rep(j, W) {
      masu[i][j] = inp[j];
      if (inp[j] == '#')
        s++;
      else
        d++;
    }
  }

  M(0, 0, 1);

  if (moves[H - 1][W - 1] == 0)
    cout << "-1" << endl;
  else
    cout << d - moves[H - 1][W - 1] << endl;
  ;

  // cout << fixed << setprecision(16) << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex> // complex<double> a(1.2 , 2.3);// real(): 1.2, imag()2.3
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

#define MOD 1000000007
#define ll long long
#define ld long double
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define rep(i, n) FOR(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define rmsame(a) sort(all(a)), a.erase(unique(all(a)), a.end())
#define rmvector(a, b)                                                         \
  rep(i, a.size()) rep(j, b.size()) if (a[i] == b[j]) {                        \
    a.erase(a.begin() + i);                                                    \
    i--;                                                                       \
    break;                                                                     \
  }
template <typename X> bool exist(vector<X> vec, X item) {
  return find(all(vec), item) != vec.end();
}

char masu[55][55];
ll moves[55][55];
ll H, W;

void M(int h, int w, int deepth) {
  if (h >= H || w >= W)
    return;
  if (h < 0 || w < 0)
    return;
  if (masu[h][w] != '.')
    return;
  // if(moves[h][w] < deepth && moves[h][w]!=0) return;
  if (moves[h][w] > deepth || moves[h][w] == 0) {
    moves[h][w] = deepth;
    M(h + 1, w, deepth + 1);
    M(h - 1, w, deepth + 1);
    M(h, w + 1, deepth + 1);
    M(h, w - 1, deepth + 1);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> H >> W;
  ll d = 0, s = 0; // dot, sharpe
  rep(i, H) {
    string inp;
    cin >> inp;
    rep(j, W) {
      masu[i][j] = inp[j];
      if (inp[j] == '#')
        s++;
      else
        d++;
    }
  }

  M(0, 0, 1);

  if (moves[H - 1][W - 1] == 0)
    cout << "-1" << endl;
  else
    cout << d - moves[H - 1][W - 1] << endl;
  ;

  // cout << fixed << setprecision(16) << ans << endl;
  return 0;
}