Problem p00122 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
#include <queue>
#include <utility>

#define P pair<int, int>
using namespace std;

int main(void) {

  std::ios_base::sync_with_stdio();

  P st;
  int dx[] = {2, 2, 2, 1, 1, 0, 0, -1, -1, -2, -2, -2};
  int dy[] = {0, 1, -1, 2, -2, 2, -2, 2, -2, 0, 1, -1};
  int kx[] = {1, 1, 1, 0, 0, 0, -1, -1, -1};
  int ky[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
  int nx, ny;
  int be, af;
  int n;
  int cnt;
  int map[10][10][10];
  bool flag;
  int sql[2][10]; /*0...x 1...y*/
  queue<P> now;

  while (1) {
    cin >> st.first >> st.second;
    if (st.first == 0 && st.second == 0)
      break;
    cin >> n;
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        for (int k = 0; k < 10; k++) {
          map[i][j][k] = -1;
        }
      }
    }
    for (int i = 0; i < n; i++)
      cin >> sql[0][i] >> sql[1][i];
    now.push(P(st.first, st.second));
    flag = false;
    cnt = 0;
    be = 1;
    af = 0;
    while (!now.empty() && !flag) {
      nx = now.front().first;
      ny = now.front().second;
      map[cnt][nx][ny] = 1;
      for (int i = 0; i < 12; i++) {
        if (nx + dx[i] >= 0 && nx + dx[i] < 10 && ny + dy[i] >= 0 &&
            ny + dy[i] < 10) {
          for (int j = 0; j < 9; j++) {
            if (sql[0][cnt] + kx[j] >= 0 && sql[0][cnt] + kx[j] < 10 &&
                sql[1][cnt] + ky[j] >= 0 && sql[1][cnt] + ky[j] < 10) {
              if (nx + dx[i] == sql[0][cnt] + kx[j] &&
                  ny + dy[i] == sql[1][cnt] + ky[j]) {
                if (map[(cnt + 1) % n][nx + dx[i]][ny + dy[i]] == 1)
                  flag = true;
                else if (map[(cnt + 1) % n][nx + dx[i]][ny + dy[i]] == -1) {
                  af++;
                  now.push(P(nx + dx[i], ny + dy[i]));
                }
              }
            }
          }
        }
      }
      be--;
      if (be == 0) {
        be = af;
        af = 0;
        cnt++;
        if (cnt == n)
          cnt = 0;
      }
      now.pop();
    }
    if (flag)
      cout << "OK" << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <utility>

#define P pair<int, int>
using namespace std;

int main(void) {

  std::ios_base::sync_with_stdio();

  P st;
  int dx[] = {2, 2, 2, 1, 1, 0, 0, -1, -1, -2, -2, -2};
  int dy[] = {0, 1, -1, 2, -2, 2, -2, 2, -2, 0, 1, -1};
  int kx[] = {1, 1, 1, 0, 0, 0, -1, -1, -1};
  int ky[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
  int nx, ny;
  int be, af;
  int n;
  int cnt;
  int map[10][10][10];
  bool flag;
  int sql[2][10]; /*0...x 1...y*/
  queue<P> now;

  while (1) {
    cin >> st.first >> st.second;
    if (st.first == 0 && st.second == 0)
      break;
    cin >> n;
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        for (int k = 0; k < 10; k++) {
          map[i][j][k] = -1;
        }
      }
    }
    for (int i = 0; i < n; i++)
      cin >> sql[0][i] >> sql[1][i];
    now.push(P(st.first, st.second));
    flag = false;
    cnt = 0;
    be = 1;
    af = 0;
    while (!now.empty() && !flag) {
      nx = now.front().first;
      ny = now.front().second;
      map[cnt][nx][ny] = 1;
      for (int i = 0; i < 12; i++) {
        if (nx + dx[i] >= 0 && nx + dx[i] < 10 && ny + dy[i] >= 0 &&
            ny + dy[i] < 10) {
          for (int j = 0; j < 9; j++) {
            if (sql[0][cnt] + kx[j] >= 0 && sql[0][cnt] + kx[j] < 10 &&
                sql[1][cnt] + ky[j] >= 0 && sql[1][cnt] + ky[j] < 10) {
              if (nx + dx[i] == sql[0][cnt] + kx[j] &&
                  ny + dy[i] == sql[1][cnt] + ky[j]) {
                if (map[(cnt + 1) % n][nx + dx[i]][ny + dy[i]] == 1)
                  flag = true;
                else if (map[(cnt + 1) % n][nx + dx[i]][ny + dy[i]] == -1) {
                  af++;
                  map[(cnt + 1) % n][nx + dx[i]][ny + dy[i]] = 0;
                  now.push(P(nx + dx[i], ny + dy[i]));
                }
              }
            }
          }
        }
      }
      be--;
      if (be == 0) {
        be = af;
        af = 0;
        cnt++;
        if (cnt == n)
          cnt = 0;
      }
      now.pop();
    }
    if (flag)
      cout << "OK" << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}