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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define fr first
#define sc second
#define mp make_pair

typedef long long int64;
typedef pair<int, int> iP;
typedef pair<iP, int> iiP;

const int INF = 2 << 28;
const double EPS = 1e-10;

#define MAX 1000000
#define NEXT(d) ((d) % 4)

int mas[2000][2000];
iP idx[MAX + 1];
vector<bool> isP(MAX + 1, true);

void makePrime() {
  isP[0] = isP[1] = false;
  for (int i = 2; i * i <= MAX; i++)
    for (int j = i + i; j <= MAX; j += i)
      isP[j] = false;
}

void makeMap() {
  //> ^ < v
  int x = 1000, y = 1000;
  int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
  int dir = 0;
  for (int i = 1; i <= MAX; i++) {
    mas[y][x] = i;
    idx[i] = iP(y, x);
    x += dx[dir], y += dy[dir];
    if (!mas[y + dy[NEXT(dir + 1)]][x + dx[NEXT(dir + 1)]])
      dir = NEXT(dir + 1);
  }
}

int M;
iP dp[MAX + 1];

iP rec(int now) {
  int x = idx[now].sc, y = idx[now].fr;
  if (mas[y][x] > M || !mas[y][x])
    return iP(0, 0);
  if (dp[now].fr)
    return dp[now];
  iP ret = iP(0, 0);
  int dx[] = {-1, 0, 1}, dy[] = {1, 1, 1};
  for (int i = 0; i < 3; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    ret = max(ret, rec(mas[ny][nx]));
  }
  if (!ret.sc && isP[mas[y][x]])
    ret.sc = mas[y][x];
  return dp[now] = iP(ret.fr + isP[mas[y][x]], ret.sc);
}

int main() {

  makePrime();
  makeMap();

  int n;
  while (cin >> M >> n, M) {
    fill_n(dp, MAX + 1, iP(0, 0));
    iP ans = rec(n);
    cout << ans.fr << " " << ans.sc << endl;
    ;
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define fr first
#define sc second
#define mp make_pair

typedef long long int64;
typedef pair<int, int> iP;
typedef pair<iP, int> iiP;

const int INF = 2 << 28;
const double EPS = 1e-10;

#define MAX 1000000
#define NEXT(d) ((d) % 4)

int mas[8000][8000];
iP idx[MAX + 1], used[8000][8000];
vector<bool> isP(MAX + 1, true);

void makePrime() {
  for (int i = 2; i * i <= MAX; i++) {
    for (int j = i + i; j <= MAX; j += i) {
      isP[j] = false;
    }
  }
}

void makeMap() {
  //> ^ < v
  int x = 4000, y = 4000;
  int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
  int dir = 0;
  for (int i = 1; i <= MAX; i++) {
    mas[y][x] = i;
    idx[i] = iP(y, x);
    x += dx[dir], y += dy[dir];
    if (!mas[y + dy[NEXT(dir + 1)]][x + dx[NEXT(dir + 1)]])
      dir = NEXT(dir + 1);
  }
}

iP ans;
void bfs(int limit, int s) {
  fill_n(*used, 8000 * 8000, iP(-INF, -INF));
  // first: { first: y, second: x }
  // second: { first: count prime cave, second: max number of through prime cave
  // }
  ans = iP(0, 0);
  queue<pair<iP, iP>> que;
  if (isP[mas[idx[s].fr][idx[s].sc]])
    que.push(mp(idx[s], iP(1, s)));
  else
    que.push(mp(idx[s], iP(0, 0)));
  int dx[] = {-1, 0, 1}, dy[] = {1, 1, 1};
  while (!que.empty()) {
    pair<iP, iP> p = que.front();
    que.pop();
    for (int i = 0; i < 3; i++) {
      int nx = p.fr.sc + dx[i], ny = p.fr.fr + dy[i];
      if (mas[ny][nx] > limit) {
        ans = max(ans, p.sc);
        continue;
      } else {
        if (isP[mas[ny][nx]]) {
          int maxi = max(p.sc.sc, mas[ny][nx]);
          que.push(mp(iP(ny, nx), iP(p.sc.fr + isP[mas[ny][nx]], maxi)));
          used[ny][nx] = iP(p.sc.fr + isP[mas[ny][nx]], maxi);
        } else if (used[ny][nx] < p.sc) {
          que.push(mp(iP(ny, nx), p.sc));
          used[ny][nx] = p.sc;
        }
      }
    }
  }
}

int main() {

  makePrime();
  makeMap();

  int m, n;
  while (cin >> m >> n, m) {
    bfs(m, n);
    cout << ans.fr << " " << ans.sc << endl;
  }
}