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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int h, w, k[31][31], cnt;
long long cx[21], cy[21], y[21][21], z[21][21], bfs[31][31], a, b, c[10], sum,
    minx, sum2;
bool s[31][31];
char p;

int main() {
  while (true) {
    memset(z, 0, sizeof(z));
    cin >> w >> h;
    cnt = 1;
    if (!w) {
      break;
    }
    for (int i = 0; i < 22; i++) {
      for (int j = 0; j < 22; j++) {
        k[i][j] = 1;
      }
    }
    cnt = 1;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        cin >> p;
        if (p != 'x') {
          k[i][j] = 0;
        }
        if (p == '*') {
          cx[cnt] = j;
          cy[cnt] = i;
          cnt++;
        }
        if (p == 'o') {
          cx[0] = j;
          cy[0] = i;
        }
      }
    }

    for (int i = 0; i < 10; i++) {
      c[i] = i;
    }

    for (int i = 0; i < cnt; i++) {
      // bfs.
      queue<int> Q;
      memset(bfs, 10, sizeof(bfs));
      memset(s, false, sizeof(s));
      bfs[cy[i]][cx[i]] = 0;

      Q.push(cy[i] * 30 + cx[i]);
      s[cy[i]][cx[i]] = true;
      while (!Q.empty()) {
        a = Q.front() / 30;
        b = Q.front() % 30;
        if (k[a - 1][b] == 0 && s[a - 1][b] == false) {
          s[a - 1][b] = true;
          Q.push((a - 1) * 30 + b);
          bfs[a - 1][b] = bfs[a][b] + 1;
        }
        if (k[a + 1][b] == 0 && s[a + 1][b] == false) {
          s[a + 1][b] = true;
          Q.push((a + 1) * 30 + b);
          bfs[a + 1][b] = bfs[a][b] + 1;
        }
        if (k[a][b - 1] == 0 && s[a][b - 1] == false) {
          s[a][b - 1] = true;
          Q.push(a * 30 + b - 1);
          bfs[a][b - 1] = bfs[a][b] + 1;
        }
        if (k[a][b + 1] == 0 && s[a][b + 1] == false) {
          s[a][b + 1] = true;
          Q.push(a * 30 + b + 1);
          bfs[a][b + 1] = bfs[a][b] + 1;
        }
        Q.pop();
      }

      for (int j = 0; j < cnt; j++) {
        z[i][j] = bfs[cy[j]][cx[j]];
      }
    }
    minx = 114514;
    do {
      sum = z[0][c[0] + 1];
      for (int i = 0; i < cnt - 2; i++) {
        sum += z[c[i] + 1][c[i + 1] + 1];
      }
      minx = min(minx, sum);
    } while (next_permutation(c, c + cnt - 1));
    if (minx == 114514) {
      minx = -1;
    }
    cout << minx << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int h, w, k[21][21], cnt;
long long cx[11], cy[11], y[11][11], z[11][11], bfs[21][21], a, b, c[10], sum,
    minx, sum2;
bool s[21][21];
char p;

int main() {
  while (true) {
    memset(z, 0, sizeof(z));
    cin >> w >> h;
    cnt = 1;
    if (!w) {
      break;
    }
    for (int i = 0; i < 21; i++) {
      for (int j = 0; j < 21; j++) {
        k[i][j] = 1;
      }
    }
    cnt = 1;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        cin >> p;
        if (p != 'x') {
          k[i][j] = 0;
        }
        if (p == '*') {
          cx[cnt] = j;
          cy[cnt] = i;
          cnt++;
        }
        if (p == 'o') {
          cx[0] = j;
          cy[0] = i;
        }
      }
    }

    for (int i = 0; i < 10; i++) {
      c[i] = i;
    }

    for (int i = 0; i < cnt; i++) {
      // bfs.
      queue<int> Q;
      memset(bfs, 10, sizeof(bfs));
      memset(s, false, sizeof(s));
      bfs[cy[i]][cx[i]] = 0;

      Q.push(cy[i] * 30 + cx[i]);
      s[cy[i]][cx[i]] = true;
      while (!Q.empty()) {
        a = Q.front() / 30;
        b = Q.front() % 30;
        if (k[a - 1][b] == 0 && s[a - 1][b] == false) {
          s[a - 1][b] = true;
          Q.push((a - 1) * 30 + b);
          bfs[a - 1][b] = bfs[a][b] + 1;
        }
        if (k[a + 1][b] == 0 && s[a + 1][b] == false) {
          s[a + 1][b] = true;
          Q.push((a + 1) * 30 + b);
          bfs[a + 1][b] = bfs[a][b] + 1;
        }
        if (k[a][b - 1] == 0 && s[a][b - 1] == false) {
          s[a][b - 1] = true;
          Q.push(a * 30 + b - 1);
          bfs[a][b - 1] = bfs[a][b] + 1;
        }
        if (k[a][b + 1] == 0 && s[a][b + 1] == false) {
          s[a][b + 1] = true;
          Q.push(a * 30 + b + 1);
          bfs[a][b + 1] = bfs[a][b] + 1;
        }
        Q.pop();
      }

      for (int j = 0; j < cnt; j++) {
        z[i][j] = bfs[cy[j]][cx[j]];
      }
    }
    minx = 114514;
    do {
      sum = z[0][c[0] + 1];
      for (int i = 0; i < cnt - 2; i++) {
        sum += z[c[i] + 1][c[i + 1] + 1];
      }
      minx = min(minx, sum);
    } while (next_permutation(c, c + cnt - 1));
    if (minx == 114514) {
      minx = -1;
    }
    cout << minx << endl;
  }
}