Problem p03389 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define For(i, l, r)                                                           \
  for (register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l)                                                       \
  for (register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
using namespace std;

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

inline int read() {
  int x = 0, fh = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fh = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x * 10) + (ch ^ 48);
  return x * fh;
}

struct node {
  int a, b, c, dis;
};

node S;
queue<node> Q;

struct App {
  int a, b, c;
  bool operator<(const App &rhs) const {
    if (a ^ rhs.a)
      return a < rhs.a;
    if (b ^ rhs.b)
      return b < rhs.b;
    return c < rhs.c;
  }
};
map<App, bool> M;

inline void Insert(int a, int b, int c, int dis) {
  App now = (App){a, b, c};
  if (M[now])
    return;
  M[now] = true;
  Q.push(node{a, b, c, dis});
}

int main() {
#ifdef zjp_shadow
  freopen("c.in", "r", stdin);
  freopen("c.out", "w", stdout);
#endif
  int a, b, c;
  cin >> a >> b >> c;
  Insert(a, b, c, 0);
  while (!Q.empty()) {
    node u = Q.front();
    Q.pop();
    if (u.a == u.b && u.b == u.c)
      return printf("%d\n", u.dis);
    Insert(u.a + 1, u.b + 1, u.c, u.dis + 1);
    Insert(u.a, u.b + 1, u.c + 1, u.dis + 1);
    Insert(u.a + 1, u.b, u.c + 1, u.dis + 1);
    Insert(u.a + 2, u.b, u.c, u.dis + 1);
    Insert(u.a, u.b + 2, u.c, u.dis + 1);
    Insert(u.a, u.b, u.c + 2, u.dis + 1);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define For(i, l, r)                                                           \
  for (register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l)                                                       \
  for (register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
using namespace std;

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

inline int read() {
  int x = 0, fh = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fh = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x * 10) + (ch ^ 48);
  return x * fh;
}

struct node {
  int a, b, c, dis;
};

node S;
queue<node> Q;

struct App {
  int a, b, c;
  bool operator<(const App &rhs) const {
    if (a ^ rhs.a)
      return a < rhs.a;
    if (b ^ rhs.b)
      return b < rhs.b;
    return c < rhs.c;
  }
};
map<App, bool> M;

inline void Insert(int a, int b, int c, int dis) {
  App now = (App){a, b, c};
  if (M[now])
    return;
  M[now] = true;
  Q.push(node{a, b, c, dis});
}

int main() {
#ifdef zjp_shadow
  freopen("c.in", "r", stdin);
  freopen("c.out", "w", stdout);
#endif
  int a, b, c;
  cin >> a >> b >> c;
  Insert(a, b, c, 0);
  while (!Q.empty()) {
    node u = Q.front();
    Q.pop();
    if (u.a == u.b && u.b == u.c)
      return printf("%d\n", u.dis), 0;
    Insert(u.a + 1, u.b + 1, u.c, u.dis + 1);
    Insert(u.a, u.b + 1, u.c + 1, u.dis + 1);
    Insert(u.a + 1, u.b, u.c + 1, u.dis + 1);
    Insert(u.a + 2, u.b, u.c, u.dis + 1);
    Insert(u.a, u.b + 2, u.c, u.dis + 1);
    Insert(u.a, u.b, u.c + 2, u.dis + 1);
  }
  return 0;
}