Problem p03530 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 45;
const int INF = 1e9;

int ice[MAXN][MAXN], dp[MAXN][MAXN][MAXN][MAXN];
int n, m, hx, hy, ans = INF;

inline int poi(int lx, int ly, int rx, int ry) {
  if (lx > rx || ly > ry)
    return 0;
  return ice[rx][ry] - ice[rx][ly - 1] - ice[lx - 1][ry] + ice[lx - 1][ly - 1];
}

int main() {
  ios::sync_with_stdio(0);
  cin >> n >> m;
  memset(dp, 0x3f, sizeof dp);
  dp[1][1][n][m] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char c;
      cin >> c;
      if (c == 'P')
        hx = i, hy = j;
      ice[i][j] =
          ice[i - 1][j] + ice[i][j - 1] - ice[i - 1][j - 1] + (c == '#');
    }
  }
  for (int i = 1; i <= hx; i++)
    for (int j = 1; j <= hy; j++)
      for (int k = n; k >= hx; k--)
        for (int l = m; l >= hy; l--) {
          ans = min(ans, dp[i][j][k][l] +
                             min(min(poi(i, j, hx, hy), poi(i, hy, hx, l)),
                                 min(poi(hx, j, k, hy), poi(hx, hy, k, l))));
          for (int s = i; s <= k; s++)
            for (int t = j; t <= l; t++) {
              if (s >= hx && t >= hy)
                dp[i][j][s][t] =
                    min(dp[i][j][s][t], dp[i][j][k][l] + poi(s + 1, j, k, t) +
                                            poi(i, t + 1, s, l));
              if (s >= hx && t <= hy)
                dp[i][t][s][l] =
                    min(dp[i][t][s][l], dp[i][j][k][l] + poi(i, j, s, t - 1) +
                                            poi(s + 1, t, k, l));
              if (s <= hx && t <= hy)
                dp[s][t][k][l] =
                    min(dp[s][t][k][l], dp[i][j][k][l] + poi(s, j, k, t - 1) +
                                            poi(i, t, s - 1, l));
              if (s <= hx && t >= hy)
                dp[s][j][k][t] =
                    min(dp[s][j][k][t], dp[i][j][k][l] + poi(i, j, s - 1, t) +
                                            poi(s, t + 1, k, l));
            }
        }
  cout << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

#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(void) {

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

  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 == 'H')
        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;
}