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

#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, n)
#define mp make_pair
using namespace std;

int sdx[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
int sdy[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
int pdx[] = {-2, -2, -2, 2, 2, 2, -1, 0, 1, -1, 0, 1};
int pdy[] = {-1, 0, 1, -1, 0, 1, -2, -2, -2, 2, 2, 2};
int park[10][10][10];
int px, py, n, sx[10], sy[10];

int main() {
  while (true) {
    cin >> px >> py;
    if (!(px | py))
      break;
    cin >> n;
    rep(i, n) cin >> sx[i] >> sy[i];
    rep(i, 10) rep(j, 10) rep(k, 10) park[i][j][k] = 0;
    rep(k, n) rep(i, 9) {
      int x = sx[k] + sdx[i];
      int y = sy[k] + sdy[i];
      if (x < 0 || x > 9 || y < 0 || y > 9)
        continue;
      park[y][x][k] = 1;
    }

    queue<pair<pair<int, int>, int>> q;
    set<pair<pair<int, int>, int>> s;
    q.push(mp(mp(py, px), -1));

    bool ok = false, loop = false;
    while (!q.empty()) {
      pair<pair<int, int>, int> t = q.front();
      q.pop();
      rep(i, 12) {
        int x = t.first.second + pdx[i];
        int y = t.first.first + pdy[i];
        int z = t.second;
        //				if (z == n-1) { z = -1; loop = true; }
        if (z == n - 1) {
          ok = true;
          break;
        }
        if (x < 0 || x > 9 || y < 0 || y > 9)
          continue;
        if (!park[y][x][z + 1])
          continue;
        if (s.find(mp(mp(y, x), z + 1)) == s.end()) {
          s.insert(mp(mp(y, x), z + 1));
          q.push(mp(mp(y, x), z + 1));
        }
        //				else if (loop) { ok = true; break; }
      }
    }

    cout << (ok ? "OK" : "NA") << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, n)
#define mp make_pair
using namespace std;

int sdx[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
int sdy[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
int pdx[] = {-2, -2, -2, -1, -1, 0, 0, 1, 1, 2, 2, 2};
int pdy[] = {-1, 0, 1, 2, -2, 2, -2, 2, -2, -1, 0, 1};
int park[10][10][10];
int px, py, n, sx[10], sy[10];

int main() {
  while (true) {
    cin >> px >> py;
    if (!(px | py))
      break;
    cin >> n;
    rep(i, n) cin >> sx[i] >> sy[i];
    rep(i, 10) rep(j, 10) rep(k, 10) park[i][j][k] = 0;
    rep(k, n) rep(i, 9) {
      int x = sx[k] + sdx[i];
      int y = sy[k] + sdy[i];
      if (x < 0 || x > 9 || y < 0 || y > 9)
        continue;
      park[y][x][k] = 1;
    }

    queue<pair<pair<int, int>, int>> q;
    set<pair<pair<int, int>, int>> s;
    q.push(mp(mp(py, px), -1));
    s.insert(mp(mp(py, px), -1));

    bool ok = false, loop = false;
    while (!q.empty()) {
      pair<pair<int, int>, int> t = q.front();
      q.pop();
      rep(i, 12) {
        int x = t.first.second + pdx[i];
        int y = t.first.first + pdy[i];
        int z = t.second;
        if (z == n - 1) {
          z = -1;
          loop = true;
        }
        if (x < 0 || x > 9 || y < 0 || x > 9)
          continue;
        if (!park[y][x][z + 1])
          continue;
        if (s.find(mp(mp(y, x), z + 1)) == s.end()) {
          s.insert(mp(mp(y, x), z + 1));
          q.push(mp(mp(y, x), z + 1));
        } else if (loop) {
          ok = true;
          break;
        }
      }
    }
    cout << (ok ? "OK" : "NA") << endl;
  }
  return 0;
}