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

#include <iostream>
#include <queue>
#include <set>
#include <utility>
#include <vector>
using namespace std;

const int needleX[4] = {-1, 0, 1, 0};
const int needleY[4] = {0, -1, 0, 1};

pair<int, int> newPoint(pair<int, int> &nowPoint, int i,
                        vector<vector<bool>> &depart, bool takayuki) {
  pair<int, int> result;
  result.first = nowPoint.first + (needleX[i] * (takayuki ? 1 : -1));
  result.second = nowPoint.second + (needleY[i] * (takayuki ? 1 : -1));
  if (!depart[result.first][result.second]) {
    result = nowPoint;
  }
  return result;
}

int main(void) {
  while (true) {
    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0) {
      break;
    }
    pair<int, int> takayuki, kazuyuki;
    cin >> takayuki.second >> takayuki.first;
    cin >> kazuyuki.second >> kazuyuki.first;

    int input;
    vector<vector<bool>> depart(h + 2, vector<bool>(w + 2, false));
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        cin >> input;
        if (input == 0) {
          depart[i][j] = true;
        }
      }
    }

    int ans = -1;
    set<pair<pair<int, int>, pair<int, int>>> used;
    queue<pair<int, pair<pair<int, int>, pair<int, int>>>> wait;
    used.insert(make_pair(takayuki, kazuyuki));
    wait.push(make_pair(0, make_pair(takayuki, kazuyuki)));
    while (!wait.empty()) {
      int cost = wait.front().first;
      pair<int, int> t = wait.front().second.first;
      pair<int, int> k = wait.front().second.second;
      wait.pop();

      if (t == k) {
        ans = cost;
        break;
      }

      for (int i = 0; i < 4; i++) {
        pair<int, int> newt = newPoint(t, i, depart, true);
        pair<int, int> newk = newPoint(k, i, depart, false);
        if (used.count(make_pair(newt, newk)) == 0) {
          used.insert(make_pair(newt, newk));
          wait.push(make_pair(cost + 1, make_pair(newt, newk)));
        }
      }
    }

    if (ans == -1) {
      cout << "NA" << endl;
    } else {
      cout << ans << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#include <set>
#include <utility>
#include <vector>
using namespace std;

const int needleX[4] = {-1, 0, 1, 0};
const int needleY[4] = {0, -1, 0, 1};

pair<int, int> newPoint(pair<int, int> &nowPoint, int i,
                        vector<vector<bool>> &depart, bool takayuki) {
  pair<int, int> result;
  result.first = nowPoint.first + (needleX[i] * (takayuki ? 1 : -1));
  result.second = nowPoint.second + (needleY[i] * (takayuki ? 1 : -1));
  if (!depart[result.first][result.second]) {
    result = nowPoint;
  }
  return result;
}

int main(void) {
  while (true) {
    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0) {
      break;
    }
    pair<int, int> takayuki, kazuyuki;
    cin >> takayuki.first >> takayuki.second;
    cin >> kazuyuki.first >> kazuyuki.second;

    int input;
    vector<vector<bool>> depart(h + 2, vector<bool>(w + 2, false));
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        cin >> input;
        if (input == 0) {
          depart[i][j] = true;
        }
      }
    }

    int ans = -1;
    set<pair<pair<int, int>, pair<int, int>>> used;
    queue<pair<int, pair<pair<int, int>, pair<int, int>>>> wait;
    used.insert(make_pair(takayuki, kazuyuki));
    wait.push(make_pair(0, make_pair(takayuki, kazuyuki)));
    while (!wait.empty()) {
      int cost = wait.front().first;
      pair<int, int> t = wait.front().second.first;
      pair<int, int> k = wait.front().second.second;
      wait.pop();

      if (t == k) {
        ans = cost;
        break;
      }

      for (int i = 0; i < 4; i++) {
        pair<int, int> newt = newPoint(t, i, depart, true);
        pair<int, int> newk = newPoint(k, i, depart, false);
        if (used.count(make_pair(newt, newk)) == 0) {
          used.insert(make_pair(newt, newk));
          wait.push(make_pair(cost + 1, make_pair(newt, newk)));
        }
      }
    }

    if (ans == -1) {
      cout << "NA" << endl;
    } else {
      cout << ans << endl;
    }
  }

  return 0;
}