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

#include <fstream>
#include <iomanip>
#include <iostream>

using namespace std;

#define ALLNUM 1005
#define fin cin

int all, start;
int map[ALLNUM][ALLNUM] = {};

bool isPrime(int n) {
  if (n <= 1)
    return false;
  if (n == 2)
    return true;
  if (n % 2 == 0)
    return false;

  for (int i = 3; i * i <= n; i += 2) {
    if (n % i == 0)
      return false;
  }
  return true;
}

class Pair {

public:
  int num;
  int count;

  Pair() {
    this->num = 0;
    this->count = 0;
  };
  Pair(int _num) {
    if (isPrime(_num)) {
      this->num = _num;
      this->count = 1;
    } else {
      this->num = this->count = 0;
    }
  }

  Pair operator+(const Pair &p) {
    this->count += p.count;
    if (p.num != 0)
      this->num = p.num;
    return *this;
  }

  void print() { cout << this->count << " " << this->num; }
};

bool come[ALLNUM][ALLNUM];
Pair pMap[ALLNUM][ALLNUM];

void init() {

  for (int i = 0; i < ALLNUM; i++) {
    for (int j = 0; j < ALLNUM; j++) {
      pMap[i][j] = Pair(0);
      come[i][j] = false;
    }
  }
}

Pair calc(int y, int x) {

  if (map[y][x] > all)
    return Pair(-10);
  if (come[y][x])
    return pMap[y][x];

  Pair p(map[y][x]);
  Pair ans;

  Pair tmp1 = calc(y + 1, x);
  Pair tmp2 = calc(y + 1, x + 1);

  if (tmp1.count == tmp1.count) {
    if (tmp1.num < tmp2.num)
      tmp1.count--;
  }

  if (tmp1.count < tmp2.count)
    ans = tmp2;
  else
    ans = tmp1;

  tmp1 = calc(y + 1, x - 1);

  if (tmp1.count == ans.count)
    if (tmp1.num < ans.num)
      tmp1.count--;

  if (tmp1.count < ans.count)
    ;
  else
    ans = tmp1;

  /*
   if (tmp1.count == tmp2.count) {
   if(tmp1.num == tmp2.num)ans = tmp1;
   else if(tmp1.num < tmp2.num)ans = tmp2;
   else ans = tmp1;
   }
   else if(tmp1.count < tmp2.count)ans = tmp2;
   else ans = tmp1;

   tmp2 = calc(y + 1, x - 1);
   if (ans.count == tmp2.count) {
   if(ans.num == tmp2.num);
   else if(ans.num < tmp2.num)ans = tmp2;
   else ;
   }
   else if(ans.count < tmp2.count)ans = tmp2;
   else ;
   */

  come[y][x] = true;
  return pMap[y][x] = p + ans;
}

int main() {
  //    ifstream fin("in.txt");

  int x, y;
  x = y = ALLNUM / 2;

  map[y][x] = 1;

  int gumi = 1;
  int num = 1;

  while (num < (ALLNUM - 1) * (ALLNUM - 1)) {
    for (int i = 0; i < gumi; i++) {
      map[y][++x] = ++num;
    }
    if (num >= ALLNUM * ALLNUM)
      break;

    for (int i = 0; i < gumi; i++) {
      map[--y][x] = ++num;
    }
    if (num >= ALLNUM * ALLNUM)
      break;

    gumi++;
    for (int i = 0; i < gumi; i++) {
      map[y][--x] = ++num;
    }
    if (num >= ALLNUM * ALLNUM)
      break;

    for (int i = 0; i < gumi; i++) {
      map[++y][x] = ++num;
    }
    if (num >= ALLNUM * ALLNUM)
      break;
    gumi++;
  }

  while (true) {
    fin >> all >> start;
    if ((all | start) == 0)
      break;

    init();
    x = y = 0;

    for (int i = 0; i < ALLNUM; i++) {
      for (int j = 0; j < ALLNUM; j++) {
        if (map[j][i] == start) {
          x = i;
          y = j;
          i = j = ALLNUM; //?????????break;
          break;
        }
      }
    }
    calc(y, x).print();
    cout << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

#define ALLNUM 1002
#define fin cin

int all, start;
int map[ALLNUM][ALLNUM] = {};

bool isPrime(int n) {
  if (n <= 1)
    return false;
  if (n == 2)
    return true;
  if (n % 2 == 0)
    return false;

  for (int i = 3; i * i <= n; i += 2) {
    if (n % i == 0)
      return false;
  }
  return true;
}

class Pair {

public:
  int num;
  int count;

  Pair() {
    this->num = 0;
    this->count = 0;
  };
  Pair(int _num) {
    if (isPrime(_num)) {
      this->num = _num;
      this->count = 1;
    } else {
      this->num = this->count = 0;
    }
  }

  Pair operator+(const Pair &p) {
    this->count += p.count;
    if (p.num != 0)
      this->num = p.num;
    return *this;
  }

  void print() { cout << this->count << " " << this->num; }
};

bool come[ALLNUM][ALLNUM];
Pair pMap[ALLNUM][ALLNUM];

void init() {

  for (int i = 0; i < ALLNUM; i++) {
    for (int j = 0; j < ALLNUM; j++) {
      pMap[i][j] = Pair(0);
      come[i][j] = false;
    }
  }
}

Pair calc(int y, int x) {

  if (map[y][x] > all)
    return Pair(-10);
  if (come[y][x])
    return pMap[y][x];

  Pair p(map[y][x]);
  Pair ans;

  Pair tmp1 = calc(y + 1, x);
  Pair tmp2 = calc(y + 1, x + 1);

  if (tmp1.count == tmp1.count) {
    if (tmp1.num < tmp2.num)
      tmp1.count--;
  }

  if (tmp1.count < tmp2.count)
    ans = tmp2;
  else
    ans = tmp1;

  tmp1 = calc(y + 1, x - 1);

  if (tmp1.count < ans.count)
    ;
  else
    ans = tmp1;

  /*
   if (tmp1.count == tmp2.count) {
   if(tmp1.num == tmp2.num)ans = tmp1;
   else if(tmp1.num < tmp2.num)ans = tmp2;
   else ans = tmp1;
   }
   else if(tmp1.count < tmp2.count)ans = tmp2;
   else ans = tmp1;

   tmp2 = calc(y + 1, x - 1);
   if (ans.count == tmp2.count) {
   if(ans.num == tmp2.num);
   else if(ans.num < tmp2.num)ans = tmp2;
   else ;
   }
   else if(ans.count < tmp2.count)ans = tmp2;
   else ;
   */

  come[y][x] = true;
  return pMap[y][x] = p + ans;
}

int main() {
  // ifstream fin("in.txt");

  int x, y;
  x = y = ALLNUM / 2;

  map[y][x] = 1;

  int gumi = 1;
  int num = 1;

  while (num < (ALLNUM - 1) * (ALLNUM - 1)) {
    for (int i = 0; i < gumi; i++) {
      map[y][++x] = ++num;
    }
    if (num >= ALLNUM * ALLNUM)
      break;

    for (int i = 0; i < gumi; i++) {
      map[--y][x] = ++num;
    }
    if (num >= ALLNUM * ALLNUM)
      break;

    gumi++;
    for (int i = 0; i < gumi; i++) {
      map[y][--x] = ++num;
    }
    if (num >= ALLNUM * ALLNUM)
      break;

    for (int i = 0; i < gumi; i++) {
      map[++y][x] = ++num;
    }
    if (num >= ALLNUM * ALLNUM)
      break;
    gumi++;
  }

  while (true) {
    fin >> all >> start;
    if ((all | start) == 0)
      break;

    init();
    x = y = 0;

    for (int i = 0; i < ALLNUM; i++) {
      for (int j = 0; j < ALLNUM; j++) {
        if (map[j][i] == start) {
          x = i;
          y = j;
          i = j = ALLNUM; //?????????break;
          break;
        }
      }
    }
    calc(y, x).print();
    cout << endl;
  }
  return 0;
}