Problem p03530 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#define ll long long
#define inf 2147483647
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define repp(i, a, b, c) for (int i = a; i <= b; i += c)
#define reph(i, a, b) for (i = a; i <= b; ++i)
#define reg(i, head, d, a) for (int i = head[d]; i; i = a[i].next)
#define rap(i, a, b) for (int i = a; i >= b; --i)
#define me(a, b) memset(a, b, sizeof(a));
#define poi(lx, ly, rx, ry)                                                    \\
  ((lx > rx or ly > ry) ? 0                                                    \\
                        : (ice[rx][ry] + ice[lx - 1][ly - 1] -                 \\
                           ice[rx][ly - 1] - ice[lx - 1][ry]))
int n, m, sx, sy;
int ice[45][45], ans = inf, f[45][45][45][45];
char temp;

using namespace std;

int main() {
  ios::sync_with_stdio(0);

  cin >> n >> m;
  me(f, 0x7f);
  f[1][1][n][m] = 0;
  rep(i, 1, n) {
    rep(j, 1, m) {
      cin >> temp;
      if (temp == 'P')
        sx = i, sy = j;
      ice[i][j] = ice[i - 1][j] + ice[i][j - 1] - ice[i - 1][j - 1] +
                  ((temp == '#') ? 1 : 0);
    }
  }
  rep(i, 1, sx) {
    rep(j, 1, sy) {
      rap(o, n, sx) {
        rap(p, m, sy) {
          ans = min(ans, f[i][j][o][p] +
                             min(min(poi(i, j, sx, sy), poi(i, sy, sx, p)),
                                 min(poi(sx, j, o, sy), poi(sx, sy, o, p))));
          rep(s, i, o) {
            rep(t, j, p) {
              if (sx <= s and sy <= t)
                f[i][j][s][t] =
                    min(f[i][j][s][t], f[i][j][o][p] + poi(i, t + 1, s, p) +
                                           poi(s + 1, j, o, t));
              if (sx <= s and sy >= t)
                f[i][t][s][p] =
                    min(f[i][t][s][p], f[i][j][o][p] + poi(i, j, s, t - 1) +
                                           poi(s + 1, t, o, p));
              if (sx >= s and sy >= t)
                f[s][t][o][p] =
                    min(f[s][t][o][p], f[i][j][o][p] + poi(i, t, s - 1, p) +
                                           poi(s, j, o, t - 1));
              if (sx >= s and sy <= t)
                f[s][j][o][t] =
                    min(f[s][j][o][t], f[i][j][o][p] + poi(i, j, s - 1, t) +
                                           poi(s, t + 1, o, p));
            }
          }
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

/************************************************
 * Au: Hany01
 * Prob: haner
 * Email: hany01dxx@gmail.com & hany01@foxmail.com
 * Inst: Yali High School
 ************************************************/

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
#define Rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++i)
#define For(i, j, k)                                                           \\
  for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++i)
#define Fordown(i, j, k)                                                       \\
  for (register int i = (j), i##_end_ = (k); i >= i##_end_; --i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define X first
#define Y second
#define PB(a) push_back(a)
#define MP(a, b) make_pair(a, b)
#define SZ(a) ((int)(a).size())
#define ALL(a) a.begin(), a.end()
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmin(T &a, T b) {
  return b < a ? a = b, 1 : 0;
}

template <typename T> inline T read() {
  static T _, __;
  static char c_;
  for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar())
    if (c_ == '-')
      __ = -1;
  for (; c_ >= '0' && c_ <= '9'; c_ = getchar())
    _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
  return _ * __;
}
// EOT

const int maxn = 45;

int n, m;
int sum[maxn][maxn], dp[maxn][maxn][maxn][maxn], x, y,
    vis[maxn][maxn][maxn][maxn];

inline void Init() {
  static char str[maxn];
  n = read<int>(), m = read<int>();
  For(i, 1, n) {
    scanf("%s", str + 1);
    For(j, 1, m) if (str[j] == 'p') x = i, y = j;
    else if (str[j] == '#') sum[i][j] = 1;
  }
  For(i, 1, n) For(j, 1, m) sum[i][j] +=
      sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
}

inline int getsum(int x1, int y1, int x2, int y2) {
  // if (sum[x2][y2] - sum[x2][y1 - 1] - sum[x1 - 1][y2] + sum[x1 - 1][y1 - 1] <
  // 0) cout << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 << endl;
  return sum[x2][y2] - sum[x2][y1 - 1] - sum[x1 - 1][y2] + sum[x1 - 1][y1 - 1];
}

int DFS(int x1, int y1, int x2, int y2) {
  if (!chkmax(vis[x1][y1][x2][y2], 1))
    return dp[x1][y1][x2][y2];
  int &ans = dp[x1][y1][x2][y2] = INF;
  chkmin(ans, getsum(x1, y1, x, y));
  chkmin(ans, getsum(x1, y, x, y2));
  chkmin(ans, getsum(x, y1, x2, y));
  chkmin(ans, getsum(x, y, x2, y2));
  For(i, x1 + 1, x) For(j, y1 + 1, y) if (i != x1 || j != y1)
      chkmin(ans, DFS(i, j, x2, y2) + getsum(i, y1, x2, j - 1) +
                      getsum(x1, j, i - 1, y2));
  For(i, x, x2 - 1) For(j, y, y2 - 1) if (i != x2 || j != y2)
      chkmin(ans, DFS(x1, y1, i, j) + getsum(i + 1, y1, x2, j) +
                      getsum(x1, j + 1, i, y2));
  For(i, x, x2 - 1) For(j, y1 + 1, y) if (i != x2 || j != y1)
      chkmin(ans, DFS(x1, j, i, y2) + getsum(x1, y1, i, j - 1) +
                      getsum(i + 1, j, x2, y2));
  For(i, x1 + 1, x) For(j, y, y2 - 1) if (i != x1 || j != y2)
      chkmin(ans, DFS(i, y1, x2, j) + getsum(x1, y1, i - 1, j) +
                      getsum(i, j + 1, x2, y2));
  return ans;
}

int main() {
  freopen("haner.in", "r", stdin);
  freopen("haner.out", "w", stdout);

  Init();
  printf("%d\\n", DFS(1, 1, n, m));

  return 0;
}