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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define FOR(v, c) for (auto &v : c)

#define EACH(it, c) for (auto it = c.begin(); it != c.end(); ++it)
#define ALL(c) (c).begin(), (c).end()
#define DRANGE(c, p) (c).begin(), (c).begin() + p, (c).end()

#define PB(n) push_back(n)
#define MP(a, b) make_pair((a), (b))
#define EXIST(c, e) ((c).find(e) != (c).end())

#define fst first
#define snd second

#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \\
  cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << (x) << endl

const int INF = INT_MAX / 2;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

struct Solver {
  const int H, W;
  VS Board;

  Solver(int h, int w) : H(h), W(w) {
    Board.resize(H);
    FOR(line, Board) { cin >> line; }

    return;
  }

  operator int() {
    vector<PII> posX;
    REP(i, 0, H) {
      REP(j, 0, W) {
        if (Board[i][j] == 'o') {
          posX.PB(MP(i, j));
        }
      }
    }
    REP(i, 0, H) {
      REP(j, 0, W) {
        if (Board[i][j] == '*') {
          posX.PB(MP(i, j));
        }
      }
    }

    map<PII, int> posToIdx;
    REP(i, 0, posX.size()) { posToIdx[posX[i]] = i; }

    const int N = posX.size();
    VVI G(N, VI(N, INF));
    REP(i, 0, N) { G[i][i] = 0; }

    REP(i, 0, posX.size()) {
      map<PII, int> dists(bfs(Board, posX[i]));
      FOR(dist, dists) { G[posToIdx[posX[i]]][posToIdx[dist.fst]] = dist.snd; }
    }

    VVI dp(1 << N, VI(N, INF));
    dp[1][0] = 0;

    REP(mask, 0, 1 << N) {
      REP(i, 0, N) {
        REP(j, 0, N) {
          dp[mask | 1 << j][j] =
              min(dp[mask | 1 << j][j], dp[mask][i] + G[i][j]);
        }
      }
    }

    int res = *min_element(ALL(dp[(1 << N) - 1]));
    return res == INF ? -1 : res;
  }

  map<PII, int> bfs(VS board, PII s) {
    VVI dist(H, VI(W, INF));
    dist[s.fst][s.snd] = 0;

    queue<PII> que;
    que.push(s);

    map<PII, int> res;
    while (!que.empty()) {
      PII cur = que.front();
      que.pop();

      if (board[cur.fst][cur.snd] == 'x') {
        continue;
      }
      if (board[cur.fst][cur.snd] == '*') {
        res[cur] = dist[cur.fst][cur.snd];
      }

      board[cur.fst][cur.snd] = 'x';

      REP(d, 0, 4) {
        PII next(cur);
        next.fst += dy[d];
        next.snd += dx[d];

        if (!(0 <= next.fst && next.fst < H && 0 <= next.snd && next.snd < W) ||
            board[next.fst][next.snd] == 'x') {
          continue;
        }

        dist[next.fst][next.snd] = dist[cur.fst][cur.snd] + 1;
        que.push(next);
      }
    }

    return res;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (true) {
    int w, h;
    cin >> w >> h;

    if (!(w | h)) {
      break;
    }

    cout << Solver(h, w) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define FOR(v, c) for (auto &v : c)

#define EACH(it, c) for (auto it = c.begin(); it != c.end(); ++it)
#define ALL(c) (c).begin(), (c).end()
#define DRANGE(c, p) (c).begin(), (c).begin() + p, (c).end()

#define PB(n) push_back(n)
#define MP(a, b) make_pair((a), (b))
#define EXIST(c, e) ((c).find(e) != (c).end())

#define fst first
#define snd second

#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \\
  cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << (x) << endl

const int INF = INT_MAX / 2;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

struct Solver {
  const int H, W;
  VS Board;

  Solver(int h, int w) : H(h), W(w) {
    Board.resize(H);
    FOR(line, Board) { cin >> line; }

    return;
  }

  operator int() {
    vector<PII> posX;
    REP(i, 0, H) {
      REP(j, 0, W) {
        if (Board[i][j] == 'o') {
          posX.PB(MP(i, j));
        }
      }
    }
    REP(i, 0, H) {
      REP(j, 0, W) {
        if (Board[i][j] == '*') {
          posX.PB(MP(i, j));
        }
      }
    }

    map<PII, int> posToIdx;
    REP(i, 0, posX.size()) { posToIdx[posX[i]] = i; }

    const int N = posX.size();
    VVI G(H, VI(H, INF));
    REP(i, 0, N) { G[i][i] = 0; }

    REP(i, 0, posX.size()) {
      map<PII, int> dists(bfs(Board, posX[i]));
      FOR(dist, dists) { G[posToIdx[posX[i]]][posToIdx[dist.fst]] = dist.snd; }
    }

    VVI dp(1 << N, VI(N, INF));
    dp[1][0] = 0;

    REP(mask, 0, 1 << N) {
      REP(i, 0, N) {
        REP(j, 0, N) {
          dp[mask | 1 << j][j] =
              min(dp[mask | 1 << j][j], dp[mask][i] + G[i][j]);
        }
      }
    }

    int res = *min_element(ALL(dp[(1 << N) - 1]));
    return res == INF ? -1 : res;
  }

  map<PII, int> bfs(VS board, PII s) {
    VVI dist(H, VI(W, INF));
    dist[s.fst][s.snd] = 0;

    queue<PII> que;
    que.push(s);

    map<PII, int> res;
    while (!que.empty()) {
      PII cur = que.front();
      que.pop();

      if (board[cur.fst][cur.snd] == 'x') {
        continue;
      }
      if (board[cur.fst][cur.snd] == '*') {
        res[cur] = dist[cur.fst][cur.snd];
      }

      board[cur.fst][cur.snd] = 'x';

      REP(d, 0, 4) {
        PII next(cur);
        next.fst += dx[d];
        next.snd += dy[d];

        if (!(0 <= next.fst && next.fst < H && 0 <= next.snd && next.snd < W) ||
            board[next.fst][next.snd] == 'x') {
          continue;
        }

        dist[next.fst][next.snd] = dist[cur.fst][cur.snd] + 1;
        que.push(next);
      }
    }

    return res;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (true) {
    int w, h;
    cin >> w >> h;

    if (!(w | h)) {
      break;
    }

    cout << Solver(h, w) << endl;
  }

  return 0;
}