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

// Code by muq
#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) {
  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: 0

Fixed submission:

#include <bits/stdc++.h>

#define MAXN 41
#define re register
#define xx x + fx[i][0]
#define yy y + fx[i][1]
#define INF 0x7ffffff
#define mp(a, b) make_pair(a, b)

using namespace std;

struct node {
  int step;
  pair<int, int> p[MAXN * 10];
  int cnt;
  bool used[MAXN * 10];
};

int n, m, endx, endy, hardcnt, specflag = true;
int imap[MAXN][MAXN], vis[MAXN][MAXN], minn[MAXN][MAXN];
int fx[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
pair<int, int> hard[MAXN * MAXN];

queue<node> q;

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

inline int read() {
  re int res = 0;
  re bool f = 1;
  re char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = 0;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    res = (res << 3) + (res << 1) + (ch & 15);
    ch = getchar();
  }
  return f ? res : -res;
}

inline bool disap(re int x, re int y) {
  return imap[x][y] == 1 && !((imap[x - 1][y] && imap[x + 1][y]) ||
                              (imap[x][y - 1] && imap[x][y + 1]));
}

void killp(re int x, re int y) {
  // printf("<%d,%d> killed\\n",x,y);
  imap[x][y] = 0;
  for (re int i = 0; i < 4; ++i)
    if (xx > 0 && xx <= n && yy > 0 && yy <= m && disap(xx, yy))
      killp(xx, yy);
}

inline void clac() {
  for (re int i = 1; i <= n; ++i)
    for (re int j = 1; j <= m; ++j)
      if (disap(i, j))
        killp(i, j);
}

inline int bfs() {
  re node start;
  start.step = 0;
  start.cnt = 0;
  memset(start.used, 0, sizeof start.used);
  // memset(start.p,0,sizeof start.p);
  q.push(start);
  while (!q.empty()) {
    re node now = q.front();
    q.pop();
    for (re int j = 1; j <= now.cnt; ++j)
      imap[now.p[j].first][now.p[j].second] = 1;
    for (re int i = 1; i <= hardcnt; ++i) {
      if (now.used[i])
        continue;
      imap[hard[i].first][hard[i].second] = 1;
      clac();
      if (imap[endx][endy] == 0)
        return now.step + 1;
      imap[hard[i].first][hard[i].second] = 2;
      re node t = now;
      ++t.step;
      t.p[++t.cnt] = hard[i];
      t.used[i] = true;
      q.push(t);
    }
    for (re int j = 1; j <= now.cnt; ++j)
      imap[now.p[j].first][now.p[j].second] = 2;
  }
}

int main() {
  char ch;
  n = read(), m = read();
  for (re int i = 1; i <= n; ++i) {
    for (re int j = 1; j <= m; ++j) {
      do
        ch = getchar();
      while (ch == '\\n' || ch == ' ');
      if (ch == '+')
        imap[i][j] = 1;
      else if (ch == '#') {
        imap[i][j] = 2;
        hard[++hardcnt] = mp(i, j);
        specflag = false;
      } else if (ch == 'P') {
        imap[i][j] = 1;
        endx = i, endy = j;
      } else
        puts("ZYH && ALSaoBen && wzr AK IOI");
    }
  }
  if (specflag) {
    return 0;
  }
  clac();
  if (imap[endx][endy] == 0) {
    return 0;
  }
  /*for(re int i=1;i<=n;++i)
  {
          for(re int j=1;j<=m;++j)
                  printf("%d ",imap[i][j]);
          printf("\\n");
  }*/
  printf("%d\\n", bfs());
  return 0;
}