Problem p03419 (C++)

Buggy submission (status: Memory Limit Exceeded):

#include "bits/stdc++.h"

#define X first
#define Y second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define rep(i, a, b) for (long i = (a); i <= (b); ++i)
#define per(i, a, b) for (long i = (a); i >= (b); --i)

using namespace std;

using I = int;
using L = long;
using F = float;
using S = string;
using SS = stringstream;
using C = char;
using LL = long long;
using VC = vector<char>;
using VI = vector<int>;
using VL = vector<long>;
using VF = vector<float>;
using VS = vector<string>;
using VVI = vector<vector<int>>;
using VVL = vector<vector<long>>;
using PLL = pair<long, long>;
using PLF = pair<long, float>;
using PLS = pair<long, string>;
using PSL = pair<string, long>;
using PSF = pair<string, float>;
using PSS = pair<string, string>;
using MLL = map<long, long>;
using MLF = map<long, float>;
using MLS = map<long, string>;
using MSL = map<string, long>;
using MSF = map<string, float>;
using MSS = map<string, string>;

const long kMax2 = 109;
const long kMax3 = 1009;
const long kMax4 = 10009;
const long kMax5 = 100009;
const long kMax6 = 1000009;
const long kInfI = 0x3F3F3F3F;
const float kInfF = 1e15;

template <class T> void PrintVector(const vector<T> &v) {
  for (const auto &e : v) {
    cout << e << endl;
  }
}

template <class T> void PrintVector(const vector<T> &v, I n) {
  rep(i, 0, n - 1) { cout << v[i] << endl; }
}

I h, w;
VI dx = {-1, 0, 1, 0};
VI dy = {0, 1, 0, -1};
vector<VC> maze(kMax4, VC(kMax4, '.'));
VVL d(kMax4, VL(kMax4, kMax6));
int BFS(int sx, int sy, int gx, int gy) {
  queue<PLL> que;
  que.emplace(sx, sy);
  d[sx][sy] = 0;

  while (!que.empty()) {
    auto p = que.front();
    que.pop();
    if (p.X == gx && p.Y == gy)
      break;
    rep(i, 0, 3) {
      auto nx = p.X + dx[i];
      auto ny = p.Y + dy[i];
      if (nx >= 0 && nx < h && ny >= 0 && ny < w && maze[nx][ny] != '#' &&
          d[nx][ny] == kMax6) {
        que.emplace(nx, ny);
        d[nx][ny] = d[p.X][p.Y] + 1;
      }
    }
  }
  return d[gx][gy];
}
//
// int main(int argc, char const *argv[]) {
//   I w_cnt = 0;
//   cin >> h >> w;
//   rep(i, 0, h - 1) {
//     rep(j, 0, w - 1) {
//       char c;
//       cin >> c;
//       maze[i][j] = c;
//       if (c == '.') { ++w_cnt; }
//     }
//   }
//   auto min_len = BFS(0, 0, h - 1, w - 1);
//   if (min_len != kMax6) { cout << w_cnt - min_len - 1 << endl; }
//   else { cout << -1 << endl; }
//   return 0;
// }

int main(int argc, char const *argv[]) {
  L n, m;
  L ans;
  cin >> n >> m;
  if (n * m == 1)
    ans = 1;
  else if (n == 1)
    ans = m - 2;
  else if (m == 1)
    ans = n - 2;
  else
    ans = (n - 2) * (m - 2);

  cout << ans << endl;
  return 0;
}


Fixed submission:

#include "bits/stdc++.h"

#define X first
#define Y second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define rep(i, a, b) for (long i = (a); i <= (b); ++i)
#define per(i, a, b) for (long i = (a); i >= (b); --i)

using namespace std;

using I = int;
using L = long;
using F = float;
using S = string;
using SS = stringstream;
using C = char;
using LL = long long;
using VC = vector<char>;
using VI = vector<int>;
using VL = vector<long>;
using VF = vector<float>;
using VS = vector<string>;
using VVI = vector<vector<int>>;
using VVL = vector<vector<long>>;
using PLL = pair<long, long>;
using PLF = pair<long, float>;
using PLS = pair<long, string>;
using PSL = pair<string, long>;
using PSF = pair<string, float>;
using PSS = pair<string, string>;
using MLL = map<long, long>;
using MLF = map<long, float>;
using MLS = map<long, string>;
using MSL = map<string, long>;
using MSF = map<string, float>;
using MSS = map<string, string>;

const long kMax2 = 109;
const long kMax3 = 1009;
const long kMax4 = 10009;
const long kMax5 = 100009;
const long kMax6 = 1000009;
const long kInfI = 0x3F3F3F3F;
const float kInfF = 1e15;

template <class T> void PrintVector(const vector<T> &v) {
  for (const auto &e : v) {
    cout << e << endl;
  }
}

template <class T> void PrintVector(const vector<T> &v, I n) {
  rep(i, 0, n - 1) { cout << v[i] << endl; }
}

//
// int main(int argc, char const *argv[]) {
//   I w_cnt = 0;
//   cin >> h >> w;
//   rep(i, 0, h - 1) {
//     rep(j, 0, w - 1) {
//       char c;
//       cin >> c;
//       maze[i][j] = c;
//       if (c == '.') { ++w_cnt; }
//     }
//   }
//   auto min_len = BFS(0, 0, h - 1, w - 1);
//   if (min_len != kMax6) { cout << w_cnt - min_len - 1 << endl; }
//   else { cout << -1 << endl; }
//   return 0;
// }

int main(int argc, char const *argv[]) {
  L n, m;
  L ans;
  cin >> n >> m;
  if (n * m == 1)
    ans = 1;
  else if (n == 1)
    ans = m - 2;
  else if (m == 1)
    ans = n - 2;
  else
    ans = (n - 2) * (m - 2);

  cout << ans << endl;
  return 0;
}