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

/************************************************
 * 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() {

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

  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) {
    printf("0\\n");
    return 0;
  }
  clac();
  if (imap[endx][endy] == 0) {
    printf("0\\n");
    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;
}