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

#include <algorithm>
#include <iostream>
#include <utility>

using namespace std;
typedef pair<int, int> P;

int n, m;
int cave[1001][1001];
P dp[1000005];
bool prime[1000005];

P search(int x, int y, int val) {
  int nx, ny;
  P ret = make_pair(0, -1);

  if (dp[val].first != -1)
    return dp[val];

  for (int v = -1; v <= 1; v++) {
    nx = x + v, ny = y + 1;
    if (nx < 0 || nx >= 1001 || ny < 0 || ny >= 1001)
      continue;
    if (cave[nx][ny] > n)
      continue;
    ret = max(ret, search(nx, ny, cave[nx][ny]));
  }
  if (!prime[val]) {
    ret.first++;
    if (ret.second == -1)
      ret.second = val;
  }

  return dp[val] = ret;
}

int main(void) {
  int px = 500, py = 500, dir = 0, dir2;
  const int vx[4] = {1, 0, -1, 0}, vy[4] = {0, -1, 0, 1};

  int nx, ny;
  for (int i = 1; i <= 1001 * 1001 - 1; i++) {
    cave[px][py] = i;
    px += vx[dir], py += vy[dir];
    dir2 = (dir + 1) % 4;
    if (cave[px + vx[dir2]][py + vy[dir2]] == 0)
      dir = dir2;
  }

  prime[1] = true;
  for (int i = 2; i < 1100; i++) {
    if (!prime[i]) {
      for (int j = 2 * i; j < 1000005; j += i)
        prime[j] = true;
    }
  }

  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;

    for (int i = 1; i <= n; i++)
      dp[i] = make_pair(-1, -1);

    int x, y;
    for (x = 0; x < 1001; x++) {
      for (y = 0; y < 1001; y++) {
        if (cave[x][y] == m)
          goto end;
      }
    }
  end:;

    P ans = search(x, y, m);
    if (ans.first == 0)
      ans.second = 0;
    cout << ans.first << " " << ans.second << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <utility>

using namespace std;
typedef pair<int, int> P;

int n, m;
int cave[1001][1001];
P dp[1000005];
bool prime[1000005];

P search(int x, int y, int val) {
  int nx, ny;
  P ret = make_pair(0, -1);

  if (dp[val].first != -1)
    return dp[val];

  for (int v = -1; v <= 1; v++) {
    nx = x + v, ny = y + 1;
    if (nx < 0 || nx >= 1001 || ny < 0 || ny >= 1001)
      continue;
    if (cave[nx][ny] > n)
      continue;
    ret = max(ret, search(nx, ny, cave[nx][ny]));
  }
  if (!prime[val]) {
    ret.first++;
    if (ret.second == -1)
      ret.second = val;
  }

  return dp[val] = ret;
}

int main(void) {
  while (cin >> n)
    ;
  cout << "-100" << endl;

  int px = 500, py = 500, dir = 0, dir2;
  const int vx[] = {1, 0, -1, 0}, vy[] = {0, -1, 0, 1};

  int nx, ny;
  for (int i = 1; i <= 1001 * 1001; i++) {
    cave[px][py] = i;
    px += vx[dir], py += vy[dir];
    dir2 = (dir + 1) % 4;
    if (cave[px + vx[dir2]][py + vy[dir2]] == 0)
      dir = dir2;
  }

  prime[1] = true;
  for (int i = 2; i < 1100; i++) {
    if (!prime[i]) {
      for (int j = 2 * i; j < 1000005; j += i)
        prime[j] = true;
    }
  }

  /*

  while(1){
          cin >> n >> m;
          if(n == 0 && m == 0) break;

          for(int i = 1; i <= n; i++) dp[i] = make_pair(-1, -1);

          int x, y;
          for(x = 0; x < 1001; x++){
                  for(y = 0; y < 1001; y++){
                          if(cave[x][y] == m) goto end;
                  }
          }
          end:;

          P ans = search(x, y, m);
          if(ans.first == 0) ans.second = 0;
          cout << ans.first << " " << ans.second << endl;
  }
  */
  return 0;
}