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

#include <algorithm>
#include <cstdio>
#include <deque>
#include <iostream>
#include <queue>

using namespace std;

int main() {
  int h, w;
  while (scanf("%d%d\\n", &w, &h), w + h) {
    char board[22][24] = {{0}};
    deque<pair<int, int>> pos;
    int count = 3;
    for (int i = 1; i <= h; i++) {
      fgets(&board[i][1], 23, stdin);
      board[i][w + 1] = 0;
      for (int j = 1; j <= w; j++) {
        if (board[i][j] == 'x')
          board[i][j] = 0;
        else if (board[i][j] == '.')
          board[i][j] = 1;
        else if (board[i][j] == '*') {
          board[i][j] = count++;
          pos.push_back(make_pair(j, i));
        } else if (board[i][j] == 'o') {
          board[i][j] = 2;
          pos.push_front(make_pair(j, i));
        }
      }
    }

    /*
    for(int i=0;i<h+2;i++){
      for(int j=0;j<w+2;j++){
        printf("%d ",board[i][j]);
      }
      puts("");
    }
    */

    // int darty=pos.size()-1;
    int dist[11][11] = {{0}};
    bool possible = true;
    for (int i = 0; i < pos.size() - 1; i++) {
      // dijkstra
      int flag[22][22] = {{0}};
      int d[22][22];
      for (int x = 0; x < 22; x++)
        for (int y = 0; y < 22; y++)
          d[y][x] = 1000;
      count = 0;
      priority_queue<pair<int, pair<int, int>>> q;
      q.push(make_pair(0, make_pair(pos[i].first, pos[i].second)));
      while (!q.empty()) {
        if (count == pos.size())
          break;
        int x = q.top().second.first, y = q.top().second.second,
            hy = q.top().first;
        q.pop();
        // printf("(%d,%d)=%d\\n",x,y,-hy);
        if (flag[y][x] != 0)
          continue;
        flag[y][x] = 1;
        if (board[y][x] >= 2) { // darty or initial
          // printf("test %d\\n",board[y][x]-2);
          dist[i][board[y][x] - 2] = -hy;
          dist[board[y][x] - 2][i] = -hy;
          count++;
        }
        if (board[y][x - 1] != 0 && d[y][x - 1] > -hy + 1) {
          d[y][x - 1] = -hy + 1;
          q.push(make_pair(hy - 1, make_pair(x - 1, y)));
        }
        if (board[y][x + 1] != 0 && d[y][x + 1] > -hy + 1) {
          d[y][x + 1] = -hy + 1;
          q.push(make_pair(hy - 1, make_pair(x + 1, y)));
        }
        if (board[y - 1][x] != 0 && d[y - 1][x] > -hy + 1) {
          d[y - 1][x] = -hy + 1;
          q.push(make_pair(hy - 1, make_pair(x, y - 1)));
        }
        if (board[y + 1][x] != 0 && d[y + 1][x] > -hy + 1) {
          d[y + 1][x] = -hy + 1;
          q.push(make_pair(hy - 1, make_pair(x, y + 1)));
        }
      }
      if (count != pos.size()) {
        possible = false;
        break;
      }
    }
    if (!possible) {
      puts("-1");
      continue;
    }

    /*
    for(int i=0;i<pos.size();i++){
      for(int j=0;j<pos.size();j++){
        printf("%3d",dist[i][j]);
      }
      printf("\\n");
    }
    */
    // traveling salesman problem
    vector<int> v(pos.size() - 1);
    int ans = 100000;
    for (int i = 0; i < pos.size() - 1; i++)
      v[i] = i + 1;
    do {
      int tmp = dist[0][v[0]];
      for (int i = 0; i < pos.size() - 2; i++) {
        tmp += dist[v[i]][v[i + 1]];
        if (tmp > ans) {
          sort(&v[i + 1], &v[pos.size() - 2], greater<int>());
          break;
        }
      }
      if (ans > tmp)
        ans = tmp;
    } while (next_permutation(v.begin(), v.end()));
    printf("%d\\n", ans);
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <deque>
#include <iostream>
#include <queue>

using namespace std;

int main() {
  int h, w;
  while (scanf("%d%d\\n", &w, &h), w + h) {
    char board[22][23] = {{0}};
    deque<pair<int, int>> pos;
    int count = 3;
    for (int i = 1; i <= h; i++) {
      fgets(&board[i][1], sizeof(&board[i][1]), stdin);
      for (int j = 1; j <= w; j++) {
        if (board[i][j] == 'x')
          board[i][j] = 0;
        else if (board[i][j] == '.')
          board[i][j] = 1;
        else if (board[i][j] == '*') {
          board[i][j] = count++;
          pos.push_back(make_pair(j, i));
        } else if (board[i][j] == 'o') {
          board[i][j] = 2;
          pos.push_front(make_pair(j, i));
        }
      }
    }

    /*
    for(int i=0;i<h+2;i++){
      for(int j=0;j<w+2;j++){
        printf("%d ",board[i][j]);
      }
      puts("");
    }
    */

    // int darty=pos.size()-1;
    int dist[11][11] = {{0}};
    bool possible = true;
    for (int i = 0; i < pos.size() - 1; i++) {
      // dijkstra
      int flag[22][22] = {{0}};
      int d[22][22];
      for (int x = 0; x < 22; x++)
        for (int y = 0; y < 22; y++)
          d[y][x] = 1000;
      count = 0;
      priority_queue<pair<int, pair<int, int>>> q;
      q.push(make_pair(0, make_pair(pos[i].first, pos[i].second)));
      while (!q.empty()) {
        if (count == pos.size())
          break;
        int x = q.top().second.first, y = q.top().second.second,
            hy = q.top().first;
        q.pop();
        // printf("(%d,%d)=%d\\n",x,y,-hy);
        if (flag[y][x] != 0)
          continue;
        flag[y][x] = 1;
        if (board[y][x] >= 2) { // darty or initial
          // printf("test %d\\n",board[y][x]-2);
          dist[i][board[y][x] - 2] = -hy;
          dist[board[y][x] - 2][i] = -hy;
          count++;
        }
        if (board[y][x - 1] != 0 && d[y][x - 1] > -hy + 1) {
          d[y][x - 1] = -hy + 1;
          q.push(make_pair(hy - 1, make_pair(x - 1, y)));
        }
        if (board[y][x + 1] != 0 && d[y][x + 1] > -hy + 1) {
          d[y][x + 1] = -hy + 1;
          q.push(make_pair(hy - 1, make_pair(x + 1, y)));
        }
        if (board[y - 1][x] != 0 && d[y - 1][x] > -hy + 1) {
          d[y - 1][x] = -hy + 1;
          q.push(make_pair(hy - 1, make_pair(x, y - 1)));
        }
        if (board[y + 1][x] != 0 && d[y + 1][x] > -hy + 1) {
          d[y + 1][x] = -hy + 1;
          q.push(make_pair(hy - 1, make_pair(x, y + 1)));
        }
      }
      if (count != pos.size()) {
        possible = false;
        break;
      }
    }
    if (!possible) {
      puts("-1");
      continue;
    }

    /*
    for(int i=0;i<pos.size();i++){
      for(int j=0;j<pos.size();j++){
        printf("%3d",dist[i][j]);
      }
      printf("\\n");
    }
    */
    // traveling salesman problem
    vector<int> v(pos.size() - 1);
    int ans = 100000;
    for (int i = 0; i < pos.size() - 1; i++)
      v[i] = i + 1;
    do {
      int tmp = dist[0][v[0]];
      for (int i = 0; i < pos.size() - 2; i++) {
        tmp += dist[v[i]][v[i + 1]];
        if (tmp > ans) {
          sort(&v[i + 1], &v[pos.size() - 2], greater<int>());
          break;
        }
      }
      if (ans > tmp)
        ans = tmp;
    } while (next_permutation(v.begin(), v.end()));
    printf("%d\\n", ans);
  }
  return 0;
}