Problem p03540 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ADD(a, b) a = (a + ll(b)) % mod
#define MUL(a, b) a = (a * ll(b)) % mod
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
#define rer(i, a, b) for (int i = int(a) - 1; i >= int(b); i--)
#define all(a) (a).begin(), (a).end()
#define sz(v) (int)(v).size()
#define pb push_back
#define sec second
#define fst first
#define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<int, pi> ppi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> mat;
typedef complex<double> comp;
void Debug() { cout << '\n'; }
template <class FIRST, class... REST> void Debug(FIRST arg, REST... rest) {
  cout << arg << " ";
  Debug(rest...);
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "[";
  if (!v.empty()) {
    rep(i, 0, sz(v) - 1) out << v[i] << ", ";
    out << v.back();
  }
  out << "]";
  return out;
}
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &v) {
  out << "(" << v.first << ", " << v.second << ")";
  return out;
}
const int MAX_N = 200010;
const int MAX_V = 100010;
const double eps = 1e-6;
const ll mod = 1000000007;
const int inf = 1 << 29;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;
///////////////////////////////////////////////////////////////////////////////////////////////////

int N, M;
int X, Y;
int A[50][50];
int S[50][50];
int dp[50][50][50][50];

int get_sum(int x1, int y1, int x2, int y2) {
  return S[x2 + 1][y2 + 1] - S[x2 + 1][y1] - S[x1][y2 + 1] + S[x1][y1];
}

void solve() {
  cin >> N >> M;
  rep(i, 0, N) {
    rep(j, 0, M) {
      char c;
      cin >> c;
      if (c == '+')
        A[i][j] = 0;
      else if (c == '#')
        A[i][j] = 1;
      else {
        A[i][j] = 0;
        X = i;
        Y = j;
      }
    }
  }
  rep(i, 0, N) {
    rep(j, 0, M) {
      S[i + 1][j + 1] = A[i][j] + S[i + 1][j] + S[i][j + 1] - S[i][j];
    }
  }
  rep(x1, 0, N) {
    rep(y1, 0, M) {
      rep(x2, x1, N) {
        rep(y2, y1, M) { dp[x1][y1][x2][y2] = N * M; }
      }
    }
  }
  dp[0][0][N - 1][M - 1] = 0;
  rep(x1, 0, N) {
    rep(y1, 0, M) {
      rer(x2, N, x1) {
        rer(y2, M, y1) {
          int left = get_sum(x1, y1, x1, y2);
          int up = get_sum(x1, y1, x2, y1);
          int right = get_sum(x2, y1, x2, y2);
          int down = get_sum(x1, y2, x2, y2);
          MIN(dp[x1 + 1][y1][x2][y2], left + dp[x1][y1][x2][y2]);
          MIN(dp[x1][y1 + 1][x2][y2], up + dp[x1][y1][x2][y2]);
          MIN(dp[x1][y1][x2 - 1][y2], right + dp[x1][y1][x2][y2]);
          MIN(dp[x1][y1][x2][y2 - 1], down + dp[x1][y1][x2][y2]);
          if (A[x1][y1])
            MIN(dp[x1 + 1][y1 + 1][x2][y2], left + up - 2 + dp[x1][y1][x2][y2]);
          if (A[x1][y2])
            MIN(dp[x1 + 1][y1][x2][y2 - 1],
                left + down - 2 + dp[x1][y1][x2][y2]);
          if (A[x2][y1])
            MIN(dp[x1][y1 + 1][x2 - 1][y2],
                right + up - 2 + dp[x1][y1][x2][y2]);
          if (A[x2][y2])
            MIN(dp[x1][y1][x2 - 1][y2 - 1],
                right + down - 2 + dp[x1][y1][x2][y2]);
        }
      }
    }
  }
  int ans = N * M;
  rep(x1, 0, N) {
    rep(y1, 0, M) {
      rep(x2, x1, N) {
        rep(y2, y1, M) {
          if (!(x1 <= X && X <= x2 && y1 <= Y && Y <= y2))
            continue;
          MIN(ans, dp[x1][y1][x2][y2] + get_sum(x1, y1, X, Y));
          MIN(ans, dp[x1][y1][x2][y2] + get_sum(X, y1, x2, Y));
          MIN(ans, dp[x1][y1][x2][y2] + get_sum(x1, Y, X, y2));
          MIN(ans, dp[x1][y1][x2][y2] + get_sum(X, Y, x2, y2));
        }
      }
    }
  }
  cout << ans << "\n";
}

int main() {
#ifndef LOCAL
  ios::sync_with_stdio(false);
  cin.tie(0);
#endif
  cout << fixed;
  cout.precision(20);
  srand((unsigned int)time(NULL));
#ifdef LOCAL
  // freopen("in.txt", "wt", stdout); //for tester
  freopen("in.txt", "rt", stdin);
#endif
  solve();
#ifdef LOCAL
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ADD(a, b) a = (a + ll(b)) % mod
#define MUL(a, b) a = (a * ll(b)) % mod
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
#define rer(i, a, b) for (int i = int(a) - 1; i >= int(b); i--)
#define all(a) (a).begin(), (a).end()
#define sz(v) (int)(v).size()
#define pb push_back
#define sec second
#define fst first
#define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<int, pi> ppi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> mat;
typedef complex<double> comp;
void Debug() { cout << '\n'; }
template <class FIRST, class... REST> void Debug(FIRST arg, REST... rest) {
  cout << arg << " ";
  Debug(rest...);
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "[";
  if (!v.empty()) {
    rep(i, 0, sz(v) - 1) out << v[i] << ", ";
    out << v.back();
  }
  out << "]";
  return out;
}
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &v) {
  out << "(" << v.first << ", " << v.second << ")";
  return out;
}
const int MAX_N = 200010;
const int MAX_V = 100010;
const double eps = 1e-6;
const ll mod = 1000000007;
const int inf = 1 << 29;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;
///////////////////////////////////////////////////////////////////////////////////////////////////

int N, M;
int X, Y;
int A[50][50];
int S[50][50];
int dp[50][50][50][50];

int get_sum(int x1, int y1, int x2, int y2) {
  return S[x2 + 1][y2 + 1] - S[x2 + 1][y1] - S[x1][y2 + 1] + S[x1][y1];
}

void solve() {
  cin >> N >> M;
  rep(i, 0, N) {
    rep(j, 0, M) {
      char c;
      cin >> c;
      if (c == '+')
        A[i][j] = 0;
      else if (c == '#')
        A[i][j] = 1;
      else {
        A[i][j] = 0;
        X = i;
        Y = j;
      }
    }
  }
  rep(i, 0, N) {
    rep(j, 0, M) {
      S[i + 1][j + 1] = A[i][j] + S[i + 1][j] + S[i][j + 1] - S[i][j];
    }
  }
  rep(x1, 0, N) {
    rep(y1, 0, M) {
      rep(x2, x1, N) {
        rep(y2, y1, M) { dp[x1][y1][x2][y2] = N * M; }
      }
    }
  }
  dp[0][0][N - 1][M - 1] = 0;
  rep(x1, 0, N) {
    rep(y1, 0, M) {
      rer(x2, N, x1) {
        rer(y2, M, y1) {
          rep(x, 0, x1 + 1) {
            rep(y, 0, y1 + 1) {
              MIN(dp[x1][y1][x2][y2], dp[x][y][x2][y2] +
                                          get_sum(x, y1, x1 - 1, y2) +
                                          get_sum(x1, y, x2, y1 - 1));
            }
          }

          rep(x, x2, N) {
            rep(y, 0, y1 + 1) {
              MIN(dp[x1][y1][x2][y2], dp[x1][y][x][y2] +
                                          get_sum(x1, y, x2, y1 - 1) +
                                          get_sum(x2 + 1, y1, x, y2));
            }
          }

          rep(x, 0, x1 + 1) {
            rep(y, y2, M) {
              MIN(dp[x1][y1][x2][y2], dp[x][y1][x2][y] +
                                          get_sum(x, y1, x1 - 1, y2) +
                                          get_sum(x1, y2 + 1, x2, y));
            }
          }

          rep(x, x2, N) {
            rep(y, y2, M) {
              MIN(dp[x1][y1][x2][y2], dp[x1][y1][x][y] +
                                          get_sum(x2 + 1, y1, x, y2) +
                                          get_sum(x1, y2 + 1, x2, y));
            }
          }
        }
      }
    }
  }
  int ans = N * M;
  rep(x1, 0, N) {
    rep(y1, 0, M) {
      rep(x2, x1, N) {
        rep(y2, y1, M) {
          if (!(x1 <= X && X <= x2 && y1 <= Y && Y <= y2))
            continue;
          MIN(ans, dp[x1][y1][x2][y2] + get_sum(x1, y1, X, Y));
          MIN(ans, dp[x1][y1][x2][y2] + get_sum(X, y1, x2, Y));
          MIN(ans, dp[x1][y1][x2][y2] + get_sum(x1, Y, X, y2));
          MIN(ans, dp[x1][y1][x2][y2] + get_sum(X, Y, x2, y2));
        }
      }
    }
  }
  cout << ans << "\n";
}

int main() {
#ifndef LOCAL
  ios::sync_with_stdio(false);
  cin.tie(0);
#endif
  cout << fixed;
  cout.precision(20);
  srand((unsigned int)time(NULL));
#ifdef LOCAL
  // freopen("in.txt", "wt", stdout); //for tester
  freopen("in.txt", "rt", stdin);
#endif
  solve();
#ifdef LOCAL
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}