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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

char ba[10][10];
int w, h;
struct State {
  int x, y;
  int px[3], py[3], cx[3], cy[3];
  int cost;
  State() {
    REP(i, 3)
    px[i] = py[i] = cx[i] = cy[i] = -1;
  }
  bool move(int d) {
    int xx = x + dx[d];
    int yy = y + dy[d];

    if (xx < 0 || xx >= w || yy < 0 || yy >= h)
      return 0;
    if (ba[xx][yy] == '#')
      return 0;
    REP(i, 3) {
      if (xx == cx[i] && yy == cy[i])
        return 0;
      if (xx == px[i] && yy == py[i])
        return 0;
    }
    x = xx;
    y = yy;
    cost++;
    return 1;
  }
  bool
  push(int d) { // ツ陛サツ古シ d ツづーツ可淞つキツ　ツ可淞つケツつスツづァtrue
    int xx = x + dx[d];
    int yy = y + dy[d];

    if (xx < 0 || xx >= w || yy < 0 || yy >= h)
      return 0;
    REP(i, 3) {
      if (xx == cx[i] && yy == cy[i]) {
        while (1) {
          xx += dx[d];
          yy += dy[d];

          REP(j, 3) {
            if (xx == px[j] && yy == py[j]) {
              cx[i] = cy[i] = px[j] = py[j] = -1;
              return 1;
            }
            if (xx == cx[j] && yy == cy[j])
              return 1;
          }
          if (ba[xx][yy] == '#')
            return 1;
          cx[i] = xx;
          cy[i] = yy;
        }
        return 1;
      }
    }
    return 0;
  }
};
const bool operator<(const State &a, const State &b) {
  if (a.x != b.x)
    return a.x < b.x;
  if (a.y != b.y)
    return a.y < b.y;
  REP(i, 3) {
    if (a.px[i] != b.px[i])
      return a.px[i] < b.px[i];
    if (a.py[i] != b.py[i])
      return a.py[i] < b.py[i];
    if (a.cx[i] != b.cx[i])
      return a.cx[i] < b.cx[i];
    if (a.cy[i] != b.cy[i])
      return a.cy[i] < b.cy[i];
  }
  return false;
}

int main() {
  while (cin >> h >> w, h || w) {
    State S;
    int cc = 0, pc = 0;
    S.cost = 0;
    REP(y, h) {
      REP(x, w) {
        char c;
        cin >> c;
        ba[x][y] = c;
        if (c == '@' || c == 'c' || c == 'w')
          ba[x][y] = '.';
        if (c == '@') {
          S.x = x;
          S.y = y;
        } else if (c == 'c') {
          S.cx[cc] = x;
          S.cy[cc++] = y;
        } else if (c == 'w') {
          S.px[pc] = x;
          S.py[pc++] = y;
        }
      }
    }
    int res = -1;
    queue<State> Q;
    set<State> visited;
    Q.push(S);
    while (!Q.empty()) {
      State now = Q.front();
      Q.pop();
      if (visited.count(now))
        continue;
      visited.insert(now);

      // printf("(%d,%d)\\n",now.x,now.y);
      // REP(y,h) {
      //   REP(x,w) {
      //     bool f = 0;
      //     REP(i,3) {
      //       if (x==now.cx[i]&&y==now.cy[i]) {
      //         cout << "c";
      //         f = 1;
      //       } else if (x==now.px[i]&&y==now.py[i]) {
      //         cout << "w";
      //         f = 1;
      //       }
      //     }
      //     if (!f)
      //         cout << ba[x][y];
      //   }
      //   cout << endl;
      // }
      if (ba[now.x][now.y] == 'E') {
        res = now.cost;
        break;
      }
      REP(i, 4) {
        State tmp = now;
        if (tmp.push(i) && !visited.count(tmp))
          Q.push(tmp);
      }
      REP(i, 4) {
        State tmp = now;
        if (tmp.move(i) && !visited.count(tmp))
          Q.push(tmp);
      }
    }
    cout << res << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

char ba[10][10];
int w, h;
struct State {
  int x, y;
  int px[3], py[3], cx[3], cy[3];
  int cost;
  State() {
    REP(i, 3)
    px[i] = py[i] = cx[i] = cy[i] = -1;
  }
  bool move(int d) {
    int xx = x + dx[d];
    int yy = y + dy[d];

    if (xx < 0 || xx >= w || yy < 0 || yy >= h)
      return 0;
    if (ba[xx][yy] == '#')
      return 0;
    REP(i, 3) {
      if (xx == cx[i] && yy == cy[i])
        return 0;
      if (xx == px[i] && yy == py[i])
        return 0;
    }
    x = xx;
    y = yy;
    cost++;
    return 1;
  }
  bool
  push(int d) { // ツ陛サツ古シ d ツづーツ可淞つキツ　ツ可淞つケツつスツづァtrue
    int xx = x + dx[d];
    int yy = y + dy[d];

    if (xx < 0 || xx >= w || yy < 0 || yy >= h)
      return 0;
    REP(i, 3) {
      if (xx == cx[i] && yy == cy[i]) {
        while (ba[xx][yy] != '#') {
          xx += dx[d];
          yy += dy[d];
          REP(j, 3) {
            if (xx == px[j] && yy == py[j]) {
              cx[i] = cy[i] = px[j] = py[j] = -1;
              return 1;
            }
            if (xx == cx[j] && yy == cy[j])
              return 1;
          }
          cx[i] == xx;
          cy[i] == yy;
        }
        return 1;
      }
    }
    return 0;
  }
};
const bool operator<(const State &a, const State &b) {
  if (a.x != b.x)
    return a.x < b.x;
  if (a.y != b.y)
    return a.y < b.y;
  REP(i, 3) {
    if (a.px[i] != b.px[i])
      return a.px[i] < b.px[i];
    if (a.py[i] != b.py[i])
      return a.py[i] < b.py[i];
    if (a.cx[i] != b.cx[i])
      return a.cx[i] < b.cx[i];
    if (a.cy[i] != b.cy[i])
      return a.cy[i] < b.cy[i];
  }
  return false;
}

int main() {
  while (cin >> h >> w, h || w) {
    memset(ba, 0, sizeof(ba));
    State S;
    int cc = 0, pc = 0;
    S.cost = 0;
    REP(y, h) {
      REP(x, w) {
        char c;
        cin >> c;
        ba[x][y] = c;
        if (c == '@' || c == 'c' || c == 'w')
          ba[x][y] = '.';
        if (c != '#')
          ba[w][h] = 1;
        if (c == '@') {
          S.x = x;
          S.y = y;
        } else if (c == 'c') {
          S.cx[cc] = x;
          S.cy[cc++] = y;
        }
        if (c == 'w') {
          S.px[pc] = x;
          S.py[pc++] = y;
        }
      }
    }
    int res = -1;
    queue<State> Q;
    set<State> visited;
    Q.push(S);
    while (!Q.empty()) {
      State now = Q.front();
      Q.pop();
      if (visited.count(now))
        continue;
      visited.insert(now);

      // printf("(%d,%d)\\n",now.x,now.y);
      if (ba[now.x][now.y] == 'E') {
        res = now.cost;
        break;
      }

      REP(i, 4) {
        State tmp = now;
        if (tmp.move(i) && !visited.count(tmp))
          Q.push(tmp);
        tmp = now;
        if (tmp.push(i) && !visited.count(tmp))
          Q.push(tmp);
      }
    }
    cout << res << endl;
  }
}