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

#include <bits/stdc++.h>
#define FU(i, a, b) for (register int i = a; i <= b; ++i)
#define FD(i, a, b) for (register int i = a; i >= b; --i)
#define set_max(a) memset(a, 0x3f, sizeof a)
#define set_min(a) memset(a, ~0x3f, sizeof a)
#define clear(a) memset(a, 0, sizeof a)

using namespace std;

template <typename T> inline void chkmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <typename T> inline void chkmin(T &a, T b) {
  if (a > b)
    a = b;
}

template <typename T> inline T read() {
  int res = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      flag = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    res = (res << 3) + (res << 1) + ch - '0';
    ch = getchar();
  }
  return res * flag;
}

template <typename T> inline void write(T x) {
  if (x / 10)
    write(x / 10);
  putchar(x % 10 + '0');
}

const int INF = 0x3f3f3f3f;
const int N = 4e1;
int n, m, hx, hy;
int ice[N + 5][N + 5], dp[N + 5][N + 5][N + 5][N + 5];

inline void Input() {
  n = read<int>();
  m = read<int>();

  char ch;
  for (register int i = 1; i <= n; ++i)
    for (register int j = 1; j <= m; ++j) {
      cin >> ch;
      ice[i][j] =
          ice[i - 1][j] + ice[i][j - 1] - ice[i - 1][j - 1] + (ch == '#');
      if (ch == 'P') {
        hx = i;
        hy = j;
      }
    }
}

inline int thick(int dx, int dy, int rx, int ry) {
  // if(dx > rx || dy > ry) return 0;
  return ice[rx][ry] - ice[rx][dy - 1] - ice[dx - 1][ry] + ice[dx - 1][dy - 1];
}

inline int MIN(int a, int b, int c, int d) { return min(min(a, b), min(c, d)); }

inline void Solve() {
  int ans = INF;
  set_max(dp);
  dp[1][1][n][m] = 0;
  for (register int i = 1; i <= hx; ++i)
    for (register int j = 1; j <= hy; ++j)
      for (register int k = n; k >= hx; --k)
        for (register int p = m; p >= hy; --p) {
          chkmin(ans, dp[i][j][k][p] +
                          MIN(thick(i, j, hx, hy), thick(hx, hy, k, p),
                              thick(hx, j, k, hy), thick(i, hy, hx, p)));
          for (register int s = i; s <= k; ++s)
            for (register int t = j; t <= p; ++t) {
              if (s >= hx && t >= hy)
                chkmin(dp[i][j][s][t], dp[i][j][k][p] + thick(s + 1, j, k, t) +
                                           thick(i, t + 1, s, p));
              if (s <= hx && t <= hy)
                chkmin(dp[s][t][k][p], dp[i][j][k][p] + thick(s, j, k, t - 1) +
                                           thick(i, t, s - 1, p));
              if (s >= hx && t <= hy)
                chkmin(dp[i][t][s][p], dp[i][j][k][p] + thick(i, j, s, t - 1) +
                                           thick(s + 1, t, k, p));
              if (s <= hx && t >= hy)
                chkmin(dp[s][j][k][t], dp[i][j][k][p] + thick(i, j, s - 1, t) +
                                           thick(s, t + 1, k, p));
            }
        }

  write(ans);
  putchar('\\n');
}

inline void File() {
  freopen("haner.in", "r", stdin);
  freopen("haner.out", "w", stdout);
}

int main() {
  // File();

  Input();
  Solve();

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define FU(i, l, r)                                                            \\
  for (register int i = (l), _end_ = (int)(r); i <= _end_; ++i)
#define FD(i, r, l)                                                            \\
  for (register int i = (r), _end_ = (int)(l); i >= _end_; --i)
#define X first
#define Y second
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define SZ(a) ((int)(a).size())
#define lowbit(x) x & (-x)
#define pii pair<int, int>
#define LL long long
#define INF 0x3f3f3f3f
#define file(a) freopen(a ".in", "r", stdin), freopen(a ".out", "w", stdout);
using namespace std;

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_;
  static bool flag_;
  __ = 0;
  c_ = getchar();
  flag_ = false;
  while (c_ > '9' || c_ < '0') {
    flag_ |= c_ == '-';
    c_ = getchar();
  }
  while (c_ >= '0' && c_ <= '9') {
    __ = (__ << 1) + (__ << 3) + (c_ ^ 48);
    c_ = getchar();
  }
  return flag_ ? -__ : __;
}

template <typename T> inline T write(T a) {
  if (a < 0) {
    putchar('-');
    a = -a;
  }
  if (a > 9)
    write(a / 10);
  putchar(a % 10 + '0');
  return 0;
}

const int maxN = 40 + 4;

int n, m, sum[maxN][maxN], dp[maxN][maxN][maxN][maxN], dx, dy, ans;
char tmp;

inline int getsum(int x, int y, int u, int v) {
  if (x > u || y > v)
    return 0;
  return sum[u][v] - sum[x - 1][v] - sum[u][y - 1] + sum[x - 1][y - 1];
}

inline void Input() {
  ios::sync_with_stdio(false);
  file("haner") cin >> n >> m;
  ans = INF;
  for (register int i = 1; i <= n; ++i) {
    for (register int j = 1; j <= m; ++j) {
      cin >> tmp;
      sum[i][j] =
          sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + (tmp == '#');
      if (tmp == 'H')
        dx = i, dy = j;
    }
  }
  memset(dp, 0x3f, sizeof(dp));
  return;
}

inline void Solve() {
  dp[1][1][n][m] = 0;
  for (register int i = 1; i <= dx; ++i)
    for (register int j = 1; j <= dy; ++j)
      for (register int k = n; k >= dx; --k)
        for (register int l = m; l >= dy; --l) {
          chkmin(ans, dp[i][j][k][l] +
                          min(min(getsum(i, j, dx, dy), getsum(i, dy, dx, l)),
                              min(getsum(dx, j, k, dy), getsum(dx, dy, k, l))));
          for (register int s = i; s <= k; ++s)
            for (register int t = j; t <= l; ++t) {
              if (s <= dx && t <= dy)
                chkmin(dp[i][j][s][t], dp[i][j][k][l] + getsum(i, t, s - 1, l) +
                                           getsum(s, j, k, t - 1));
              if (s >= dx && t <= dy)
                chkmin(dp[i][j][s][t], dp[i][j][k][l] + getsum(i, j, s, t - 1) +
                                           getsum(s + 1, t, k, l));
              if (s >= dx && t >= dy)
                chkmin(dp[i][j][s][t], dp[i][j][k][l] + getsum(i, t + 1, s, l) +
                                           getsum(s + 1, j, k, t));
              if (s <= dx && t >= dy)
                chkmin(dp[i][j][s][t], dp[i][j][k][l] + getsum(i, j, s - 1, t) +
                                           getsum(s, t + 1, k, l));
            }
        }
  return;
}

inline void Output() {
  write(ans);
  putchar('\\n');
  return;
}

int main() {
  Input();
  Solve();
  Output();
  return 0;
}