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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 1000000000
#define LLINF 1000000000000000ll
#define sz(x) ((int)(x).size())
#define sq(x) ((x) * (x))
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()

int h, w;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
vector<string> f;
vector<int> pos;
int start;
int d[402][402];
int dp[1 << 11][11];
int K;
int id(int x, int y) { return x * w + y; }
string binary(int a) {
  string ret;
  for (int i = 0; i < 4; i++) {
    if ((1 << i) & a)
      ret += "1";
    else
      ret += "0";
  }
  return ret;
}
int rec(int S, int v) {
  if (dp[S][v] != -1)
    return dp[S][v];
  if (S == 0)
    return 0;
  int ret = INF;
  for (int i = 0; i < K; i++) {
    if ((S & (1 << i)) != 0)
      ret = min(ret, rec(S - (1 << i), i) + d[pos[i]][pos[v]]);
  }
  // cout << S << ' ' << v << ' ' << ret << endl;
  return dp[S][v] = ret;
}
int solve() {
  f.clear();
  pos.clear();
  scanf("%d %d", &w, &h);
  int N = w * h;
  if (h + w == 0)
    return 1;
  f.resize(h);
  for (int i = 0; i < h; i++)
    cin >> f[i];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      d[i][j] = INF;
      if (i == j)
        d[i][j] = 0;
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      for (int dir = 0; dir < 4; dir++) {
        int nx = i + dx[dir];
        int ny = j + dy[dir];
        if (nx < 0 || nx >= h || ny < 0 || ny >= w)
          continue;
        if (f[i][j] == 'x' || f[nx][ny] == 'x')
          continue;
        // cout << id(i,j) << ' ' << id(nx,ny) << endl;
        d[id(nx, ny)][id(i, j)] = 1;
        d[id(i, j)][id(nx, ny)] = 1;
      }
    }
  }
  for (int k = 0; k < N; k++) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (f[i][j] == 'o')
        start = pos.size();
      if (f[i][j] == 'o' || f[i][j] == '*')
        pos.pb(id(i, j));
    }
  }
  K = pos.size();
  for (int i = 0; i < (1 << K); i++) {
    for (int j = 0; j < K; j++) {
      dp[i][j] = -1;
    }
  }
  int ans = rec((1 << K) - 1 - (1 << start), start);
  if (ans == INF)
    ans = -1;
  printf("%d\\n", ans);
  return 0;
}

int main() {
  while (1) {
    if (solve())
      return 0;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 1000000000
#define LLINF 1000000000000000ll
#define sz(x) ((int)(x).size())
#define sq(x) ((x) * (x))
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()

int h, w;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
vector<string> f;
vector<int> pos;
int start;
int d[402][402];
int dp[1 << 11][11];
int K;
int id(int x, int y) { return x * w + y; }
string binary(int a) {
  string ret;
  for (int i = 0; i < 4; i++) {
    if ((1 << i) & a)
      ret += "1";
    else
      ret += "0";
  }
  return ret;
}
int rec(int S, int v) {
  if (dp[S][v] != -1)
    return dp[S][v];
  if (S == 0)
    return 0;
  int ret = INF;
  for (int i = 0; i < K; i++) {
    if ((S & (1 << i)) != 0)
      ret = min(ret, rec(S - (1 << i), i) + d[pos[i]][pos[v]]);
  }
  // cout << binary(S) << ' ' << v << ' ' << ret << endl;
  return dp[S][v] = ret;
}
int solve() {
  f.clear();
  scanf("%d %d", &w, &h);
  int N = w * h;
  if (h + w == 0)
    return 1;
  f.resize(h);
  for (int i = 0; i < h; i++)
    cin >> f[i];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      d[i][j] = INF;
      if (i == j)
        d[i][j] = 0;
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      for (int dir = 0; dir < 4; dir++) {
        int nx = i + dx[dir];
        int ny = j + dy[dir];
        if (nx < 0 || nx >= h || ny < 0 || ny >= w)
          continue;
        if (f[i][j] == 'x' || f[nx][ny] == 'x')
          continue;
        // cout << id(i,j) << ' ' << id(nx,ny) << endl;
        d[id(nx, ny)][id(i, j)] = 1;
        d[id(i, j)][id(nx, ny)] = 1;
      }
    }
  }
  for (int k = 0; k < N; k++) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (f[i][j] == 'o')
        start = pos.size();
      if (f[i][j] == 'o' || f[i][j] == '*')
        pos.pb(id(i, j));
    }
  }
  K = pos.size();
  for (int i = 0; i < (1 << K); i++) {
    for (int j = 0; j < K; j++) {
      dp[i][j] = -1;
    }
  }
  int ans = rec((1 << K) - (1 << start), start);
  if (ans == INF)
    ans = -1;
  printf("%d\\n", ans);
  return 0;
}

int main() {
  while (1) {
    if (solve())
      return 0;
  }
  return 0;
}