Problem p00721 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include "bits/stdc++.h"
#define REP(i, a, b) for (i = a; i < b; ++i)
#define rep(i, n) REP(i, 0, n)
#define ll long long
#define ull unsigned ll
typedef long double ld;
#define ALL(a) (a).begin(), (a).end()
#define ifnot(a) if (not a)
#define dump(x) cerr << #x << " = " << (x) << endl
using namespace std;

void reader(int &a) { scanf("%d", &a); }
void reader(double &a) { scanf("%lf", &a); }
void reader(char a[]) { scanf("%s", a); }
void reader(char &a) { scanf(" %c", &a); }
void reader(ll &a) { scanf("%lld", &a); }
void reader(ull &a) { scanf("%llu", &a); }
// void reader(string& a){cin >> a;};
template <class T, class U> void reader(T &t, U &u) {
  reader(t);
  reader(u);
}
template <class T, class U, class V> void reader(T &t, U &u, V &v) {
  reader(t);
  reader(u);
  reader(v);
}

void writer(const int a, char c) {
  printf("%d", a);
  putchar(c);
}
void writer(const ll a, char c) {
  printf("%lld", a);
  putchar(c);
}
void writer(const ull a, char c) {
  printf("%llu", a);
  putchar(c);
}
void writer(const double a, char c) {
  printf("%.20lf", a);
  putchar(c);
}
void writer(const char a[]) { printf("%s", a); };
void writer(const char a[], char c) {
  printf("%s", a);
  putchar(c);
};
void writer(const char a, char c) {
  putchar(a);
  putchar(c);
};
template <class T> void writerLn(T t) { writer(t, '\n'); }
template <class T, class U> void writerLn(T t, U u) {
  writer(t, ' ');
  writer(u, '\n');
}
template <class T, class U, class V> void writerLn(T t, U u, V v) {
  writer(t, ' ');
  writer(u, ' ');
  writer(v, '\n');
}
template <class T> void writerArr(T x[], int n) {
  int i;
  if (!n) {
    putchar('\n');
    return;
  }
  rep(i, n - 1) writer(x[i], ' ');
  writer(x[n - 1], '\n');
}
template <class T> void writerVec(vector<T> x) {
  int n = x.size();
  int i;
  if (!n) {
    putchar('\n');
    return;
  }
  rep(i, n - 1) writer(x[i], ' ');
  writer(x[n - 1], '\n');
}

vector<string> split(const string &str, char sep) {
  vector<string> v;
  stringstream ss(str);
  string buffer;
  while (getline(ss, buffer, sep)) {
    v.push_back(buffer);
  }
  return v;
}
// #define int ll
bool test = 0;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
#define INF (1 << 28)
ull mod = (int)1e9 + 7;
//.....................
#define MAX 22

struct Node {
  int y, x;
};
int w, h;
char c[MAX][MAX];
int dm[MAX][MAX];
vector<Node> nodes;
int start;
bool create_distace_matrix() {
  int i, j, k;
  int y, x;
  int cf[MAX][MAX];
  rep(i, nodes.size()) {
    rep(y, h) rep(x, w) {
      if (c[y][x] == 'x')
        cf[y][x] = -2;
      else
        cf[y][x] = -1;
    }

    cf[nodes[i].y][nodes[i].x] = 0;
    bool changed = true;
    int current = 0;
    for (; changed; current += 1) {
      changed = false;
      rep(y, h) rep(x, w) {
        if (cf[y][x] == current) {
          rep(j, 4) {
            int ny = y + dy[j];
            int nx = x + dx[j];
            if (ny < 0 || h <= ny)
              continue;
            if (nx < 0 || w <= nx)
              continue;
            if (cf[ny][nx] == -1)
              cf[ny][nx] = current + 1;
            changed = true;
          }
        }
      }
    }
    rep(j, nodes.size()) {
      int distance = cf[nodes[j].y][nodes[j].x];
      if (distance < 0)
        return false;
      dm[i][j] = distance;
    }
  }
  return true;
}
bool done[MAX];
int ans;
void rec(int n, int idx, int sum) {
  int i;
  if (sum >= ans)
    return;
  if (n == nodes.size()) {
    if (sum < ans)
      ans = sum;
    return;
  }
  rep(i, nodes.size()) {
    if (done[i])
      continue;
    done[i] = true;
    rec(n + 1, i, sum + dm[idx][i]);
    done[i] = false;
  }
}
signed main(void) {
  int i, j, k, l;
  while (1) {
    cin >> w >> h;
    if (w == 0)
      return 0;
    rep(i, h) reader(c[i]);
    nodes.clear();
    rep(i, h) rep(j, w) {
      if (c[i][j] == 'o')
        start = nodes.size();
      if (c[i][j] == 'o' || c[i][j] == '*') {
        nodes.push_back({i, j});
      }
    }
    if (!create_distace_matrix()) {
      cout << -1 << endl;
      cerr << "????????????" << endl;
    }
    ans = INT_MAX;
    done[start] = true;
    rec(1, start, 0);
    cout << ans << endl;
    done[start] = false;
  }
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include "bits/stdc++.h"
#define REP(i, a, b) for (i = a; i < b; ++i)
#define rep(i, n) REP(i, 0, n)
#define ll long long
#define ull unsigned ll
typedef long double ld;
#define ALL(a) (a).begin(), (a).end()
#define ifnot(a) if (not a)
#define dump(x) cerr << #x << " = " << (x) << endl
using namespace std;

void reader(int &a) { scanf("%d", &a); }
void reader(double &a) { scanf("%lf", &a); }
void reader(char a[]) { scanf("%s", a); }
void reader(char &a) { scanf(" %c", &a); }
void reader(ll &a) { scanf("%lld", &a); }
void reader(ull &a) { scanf("%llu", &a); }
// void reader(string& a){cin >> a;};
template <class T, class U> void reader(T &t, U &u) {
  reader(t);
  reader(u);
}
template <class T, class U, class V> void reader(T &t, U &u, V &v) {
  reader(t);
  reader(u);
  reader(v);
}

void writer(const int a, char c) {
  printf("%d", a);
  putchar(c);
}
void writer(const ll a, char c) {
  printf("%lld", a);
  putchar(c);
}
void writer(const ull a, char c) {
  printf("%llu", a);
  putchar(c);
}
void writer(const double a, char c) {
  printf("%.20lf", a);
  putchar(c);
}
void writer(const char a[]) { printf("%s", a); };
void writer(const char a[], char c) {
  printf("%s", a);
  putchar(c);
};
void writer(const char a, char c) {
  putchar(a);
  putchar(c);
};
template <class T> void writerLn(T t) { writer(t, '\n'); }
template <class T, class U> void writerLn(T t, U u) {
  writer(t, ' ');
  writer(u, '\n');
}
template <class T, class U, class V> void writerLn(T t, U u, V v) {
  writer(t, ' ');
  writer(u, ' ');
  writer(v, '\n');
}
template <class T> void writerArr(T x[], int n) {
  int i;
  if (!n) {
    putchar('\n');
    return;
  }
  rep(i, n - 1) writer(x[i], ' ');
  writer(x[n - 1], '\n');
}
template <class T> void writerVec(vector<T> x) {
  int n = x.size();
  int i;
  if (!n) {
    putchar('\n');
    return;
  }
  rep(i, n - 1) writer(x[i], ' ');
  writer(x[n - 1], '\n');
}

vector<string> split(const string &str, char sep) {
  vector<string> v;
  stringstream ss(str);
  string buffer;
  while (getline(ss, buffer, sep)) {
    v.push_back(buffer);
  }
  return v;
}
// #define int ll
bool test = 0;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
#define INF (1 << 28)
ull mod = (int)1e9 + 7;
//.....................
#define MAX 22

struct Node {
  int y, x;
};
int w, h;
char c[MAX][MAX];
int dm[MAX][MAX];
vector<Node> nodes;
int start;
bool create_distace_matrix() {
  int i, j, k;
  int y, x;
  int cf[MAX][MAX];
  rep(i, nodes.size()) {
    rep(y, h) rep(x, w) {
      if (c[y][x] == 'x')
        cf[y][x] = -2;
      else
        cf[y][x] = -1;
    }

    cf[nodes[i].y][nodes[i].x] = 0;
    bool changed = true;
    int current = 0;
    for (; changed; current += 1) {
      changed = false;
      rep(y, h) rep(x, w) {
        if (cf[y][x] == current) {
          rep(j, 4) {
            int ny = y + dy[j];
            int nx = x + dx[j];
            if (ny < 0 || h <= ny)
              continue;
            if (nx < 0 || w <= nx)
              continue;
            if (cf[ny][nx] == -1)
              cf[ny][nx] = current + 1;
            changed = true;
          }
        }
      }
    }
    rep(j, nodes.size()) {
      int distance = cf[nodes[j].y][nodes[j].x];
      if (distance < 0)
        return false;
      dm[i][j] = distance;
    }
  }
  return true;
}
bool done[MAX];
int ans;
void rec(int n, int idx, int sum) {
  int i;
  if (sum >= ans)
    return;
  if (n == nodes.size()) {
    if (sum < ans)
      ans = sum;
    return;
  }
  rep(i, nodes.size()) {
    if (done[i])
      continue;
    done[i] = true;
    rec(n + 1, i, sum + dm[idx][i]);
    done[i] = false;
  }
}
signed main(void) {
  int i, j, k, l;
  while (1) {
    cin >> w >> h;
    if (w == 0)
      return 0;
    rep(i, h) reader(c[i]);
    nodes.clear();
    rep(i, h) rep(j, w) {
      if (c[i][j] == 'o')
        start = nodes.size();
      if (c[i][j] == 'o' || c[i][j] == '*') {
        nodes.push_back({i, j});
      }
    }
    if (!create_distace_matrix()) {
      cout << -1 << endl;
      continue;
    }
    ans = INT_MAX;
    done[start] = true;
    rec(1, start, 0);
    cout << ans << endl;
    done[start] = false;
  }
  return 0;
}