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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
struct State {
  int S;
  int x, y;
  int c;
  State() {}
  State(int S, int x, int y, int c) : S(S), x(x), y(y), c(c) {}
  bool operator<(const State &s) const {
    return c > s.c; // reverse
  }
};

int getidx(string s, char c) {
  REP(i, s.size()) if (s[i] == c) return i;
  assert(false);
  return -1;
}
int dist[1 << 13][30][30];
int main() {
  int W, H;
  while (cin >> W >> H && W) {
    vector<string> grid(H);
    REP(i, H) cin >> grid[i];
    int sx, sy, gx, gy;
    set<char> rock;
    REP(y, H) REP(x, W) {
      if (grid[y][x] == '@') {
        sx = x, sy = y;
      }
      if (grid[y][x] == '<') {
        gx = x, gy = y;
      }
      if (isalpha(grid[y][x])) {
        rock.insert(tolower(grid[y][x]));
      }
    }
    string rstr;
    FORIT(it, rock) rstr += *it;
    REP(i, 1 << 13) REP(j, 30) REP(k, 30) dist[i][j][k] = INF;
    dist[0][sy][sx] = 0;
    priority_queue<State> que;
    que.push(State(0, sx, sy, 0));
    int ans = -1;
    while (!que.empty()) {
      State s = que.top();
      que.pop();
      if (dist[s.S][s.y][s.x] < s.c)
        continue;
      if (s.x == gx && s.y == gy) {
        ans = s.c;
        break;
      }
      REP(r, 4) {
        int nx = s.x + dx[r];
        int ny = s.y + dy[r];
        if (nx >= 0 && ny >= 0 && nx < W && ny < H && grid[ny][nx] != '#') {
          if (isupper(grid[ny][nx])) {
            int idx = getidx(rstr, tolower(grid[ny][nx]));
            if (s.S & 1 << idx && dist[s.S][ny][nx] > s.c + 1) {
              dist[s.S][ny][nx] = s.c + 1;
              que.push(State(s.S, nx, ny, s.c + 1));
            }
          } else if (islower(grid[ny][nx])) {
            int nS = s.S ^ (1 << getidx(rstr, grid[ny][nx]));
            if (dist[nS][ny][nx] > s.c + 1) {
              dist[nS][ny][nx] = s.c + 1;
              que.push(State(nS, nx, ny, s.c + 1));
            }
          } else if (dist[s.S][ny][nx] > s.c + 1) {
            dist[s.S][ny][nx] = s.c + 1;
            que.push(State(s.S, nx, ny, s.c + 1));
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
struct State {
  int S;
  int x, y;
  int c;
  State() {}
  State(int S, int x, int y, int c) : S(S), x(x), y(y), c(c) {}
  bool operator<(const State &s) const {
    return c > s.c; // reverse
  }
};

int getidx(string s, char c) {
  REP(i, s.size()) if (s[i] == c) return i;
  return -1;
}
int main() {
  int W, H;
  while (cin >> W >> H && W) {
    vector<string> grid(H);
    REP(i, H) cin >> grid[i];
    int sx, sy, gx, gy;
    set<char> rock;
    REP(y, H) REP(x, W) {
      if (grid[y][x] == '@') {
        sx = x, sy = y;
      }
      if (grid[y][x] == '<') {
        gx = x, gy = y;
      }
      if (isalpha(grid[y][x])) {
        rock.insert(tolower(grid[y][x]));
      }
    }
    string rstr;
    FORIT(it, rock) rstr += *it;
    int dist[1 << 9][30][30];
    REP(i, 1 << 9) REP(j, 30) REP(k, 30) dist[i][j][k] = INF;
    dist[0][sy][sx] = 0;
    priority_queue<State> que;
    que.push(State(0, sx, sy, 0));
    int ans = -1;
    while (!que.empty()) {
      State s = que.top();
      que.pop();
      if (dist[s.S][s.y][s.x] < s.c)
        continue;
      if (s.x == gx && s.y == gy) {
        ans = s.c;
        break;
      }
      REP(r, 4) {
        int nx = s.x + dx[r];
        int ny = s.y + dy[r];
        if (nx >= 0 && ny >= 0 && nx < W && ny < H && grid[ny][nx] != '#') {
          if (isupper(grid[ny][nx])) {
            int idx = getidx(rstr, tolower(grid[ny][nx]));
            if (s.S & 1 << idx && dist[s.S][ny][nx] > s.c + 1) {
              dist[s.S][ny][nx] = s.c + 1;
              que.push(State(s.S, nx, ny, s.c + 1));
            }
          } else if (islower(grid[ny][nx])) {
            int nS = s.S ^ (1 << getidx(rstr, grid[ny][nx]));
            if (dist[nS][ny][nx] > s.c + 1) {
              dist[nS][ny][nx] = s.c + 1;
              que.push(State(nS, nx, ny, s.c + 1));
            }
          } else if (dist[s.S][ny][nx] > s.c + 1) {
            dist[s.S][ny][nx] = s.c + 1;
            que.push(State(s.S, nx, ny, s.c + 1));
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}