Problem p00770 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>

#define MAX 1000001
#define SIZE 2000

typedef long long ll;

using namespace std;

int m, n;
int table[SIZE][SIZE] = {{0, 0}};
int dp[SIZE][SIZE];
bool prime[MAX];
pair<int, int> coord[MAX]; // 座標
pair<int, int> ans;        // <素数の数, 出口の番号>

void dfs(int count, int y, int x) {
  int num = table[y][x];
  if (num <= 0 || m < num)
    return;
  if (count < dp[y][x] ||
      (count == dp[y][x] && prime[num] == false && dp[y][x] != 0))
    return;
  dp[y][x] = count;

  if (prime[num] == true) {
    count++;
    dp[y][x] = count;
    if (ans.first < count || (ans.first == count && ans.second < num)) {
      ans.first = count;
      ans.second = num;
    }
  }

  for (int dx = -1; dx <= 1; dx++) {
    dfs(count, y + 1, x + dx);
  }
}

int main(void) {
  fill(prime, prime + MAX, true);
  prime[0] = prime[1] = false;
  for (int i = 2; i < MAX; i++) {
    if (prime[i]) {
      for (int j = i + i; j < MAX; j += i) {
        prime[j] = false;
      }
    }
  }

  int d = 0, x = 1000, y = 1000;
  int dx[4] = {0, 1, 0, -1};
  int dy[4] = {1, 0, -1, 0};
  for (int i = 1; i < MAX; i++) {
    table[y][x] = i;
    coord[i] = make_pair(y, x);
    if (table[y + dy[(d + 1) % 4]][x + dx[(d + 1) % 4]] == 0)
      d++;
    x += dx[d % 4];
    y += dy[d % 4];
  }

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

    int y = coord[n].first;
    int x = coord[n].second;
    fill(dp[0], dp[2000], 0);
    ans = make_pair(0, 0);
    dfs(0, y, x);
    cout << ans.first << " " << ans.second << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define MAX 1000001

typedef long long ll;

using namespace std;

int m;
int table[2000][2000] = {{0, 0}};
int dp[2000][2000] = {{0, 0}};
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
bool prime[MAX];
pair<int, int> coord[MAX]; // 座標
pair<int, int> ans;        // <素数の数, 出口の番号>

void dfs(int count, int y, int x) {
  int n = table[y][x];
  if (n <= 0 || m < n)
    return;

  if (count < dp[y][x] ||
      (count == dp[y][x] && prime[n] == false && dp[y][x] != 0))
    return;
  if (prime[n] == true) {
    count++;
    dp[y][x] = count;
    if (ans.first < count || (ans.first == count && ans.second < n)) {
      ans.first = count;
      ans.second = n;
    }
  }

  for (int dx = -1; dx <= 1; dx++) {
    dfs(count, y + 1, x + dx);
  }
}

int main(void) {
  fill(prime, prime + MAX, true);
  prime[0] = prime[1] = false;
  for (int i = 2; i < MAX; i++) {
    if (prime[i]) {
      for (int j = i + i; j < MAX; j += i) {
        prime[j] = false;
      }
    }
  }

  int d = 0;
  int x = 1000, y = 1000;
  for (int i = 1; i < MAX; i++) {
    table[y][x] = i;
    coord[i] = make_pair(y, x);
    if (table[y + dy[(d + 1) % 4]][x + dx[(d + 1) % 4]] == 0)
      d++;
    x += dx[d % 4];
    y += dy[d % 4];
  }

  while (1) {
    int n;
    cin >> m >> n;
    if (!m && !n)
      break;

    int y = coord[n].first;
    int x = coord[n].second;
    fill(dp[0], dp[2000], 0);
    ans = make_pair(0, 0);
    dfs(0, y, x);
    cout << ans.first << " " << ans.second << endl;
  }

  return 0;
}