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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>

#define MAX_N 3000
#define MAX_X 5000
#define MAX_Y 5000

using namespace std;

typedef pair<int, int> P;

int N;
bool field[MAX_X + 1][MAX_Y + 1];
vector<P> paul;

P edge;

bool search(P o, P p1, P v, int mode) {
  bool result = false;
  P _p1, _v;
  _p1 = P(p1.first + v.first, p1.second + v.second);
  if (_p1.first > MAX_X || _p1.first < 0)
    return false;
  if (_p1.second > MAX_Y || _p1.second < 0)
    return false;
  if (field[_p1.first][_p1.second] == false)
    return false;
  if (_p1.first == o.first && _p1.second == o.second)
    return true;
  if (mode != 2) {
    _v = P(v.second, v.first * -1);
    if (search(o, _p1, _v, 1))
      result = true;
  }
  if (mode != 1) {
    _v = P(v.second * -1, v.first);
    if (search(o, _p1, _v, 2))
      result = true;
  }

  return result;
}

void solve() {
  for (int i = 0; i <= MAX_X; i++) {
    for (int j = 0; j <= MAX_Y; j++) {
      field[i][j] = false;
    }
  }
  for (int i = 0; i < paul.size(); i++) {
    P p = paul[i];
    field[p.first][p.second] = true;
  }

  edge = P(0, 0);
  for (int i = 0; i < paul.size(); i++) {
    P p1 = paul[i];
    for (int j = i + 1; j < paul.size(); j++) {
      P p2 = paul[j];
      P v = P(p2.first - p1.first, p2.second - p1.second);
      if (search(p1, p1, v, 0)) {
        if (pow(edge.first, 2) + pow(edge.second, 2) <
            pow(v.first, 2) + pow(v.second, 2)) {
          edge = P(v.first, v.second);
        }
      }
    }
  }
  return;
}

int main() {

  while (true) {
    scanf("%d", &N);
    if (N == 0)
      break;
    paul.clear();
    int _x, _y;
    for (int i = 0; i < N; i++) {
      scanf("%d%d", &_x, &_y);
      paul.push_back(P(_x, _y));
    }

    solve();

    int ans = pow(edge.first, 2) + pow(edge.second, 2);
    printf("%d\\n", ans);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>

#define MAX_N 3000
#define MAX_X 5000
#define MAX_Y 5000

using namespace std;

typedef pair<int, int> P;

int N;
bool field[MAX_X][MAX_Y];
vector<P> paul;

P edge;

bool search(P o, P p1, P v, int mode) {
  bool result = false;
  P _p1, _v;
  _p1 = P(p1.first + v.first, p1.second + v.second);
  if (_p1.first >= MAX_X || _p1.first < 0)
    return false;
  if (_p1.second >= MAX_Y || _p1.second < 0)
    return false;
  if (field[_p1.first][_p1.second] == false)
    return false;
  if (_p1.first == o.first && _p1.second == o.second)
    return true;
  if (mode != 2) {
    _v = P(v.second, v.first * -1);
    if (search(o, _p1, _v, 1))
      result = true;
  }
  if (mode != 1) {
    _v = P(v.second * -1, v.first);
    if (search(o, _p1, _v, 1))
      result = true;
  }

  return result;
}

void solve() {
  for (int i = 0; i < MAX_X; i++) {
    for (int j = 0; j < MAX_Y; j++) {
      field[i][j] = false;
    }
  }
  for (int i = 0; i < paul.size(); i++) {
    P p = paul[i];
    field[p.first][p.second] = true;
  }

  edge = P(0, 0);
  for (int i = 0; i < paul.size(); i++) {
    P p1 = paul[i];
    for (int j = i + 1; j < paul.size(); j++) {
      P p2 = paul[j];
      P v = P(p2.first - p1.first, p2.second - p1.second);
      if (search(p1, p1, v, 0)) {
        if (pow(edge.first, 2) + pow(edge.second, 2) <
            pow(v.first, 2) + pow(v.second, 2)) {
          edge = P(v.first, v.second);
        }
      }
    }
  }
  return;
}

int main() {

  while (true) {
    scanf("%d", &N);
    if (N == 0)
      break;
    paul.clear();
    int _x, _y;
    for (int i = 0; i < N; i++) {
      scanf("%d%d", &_x, &_y);
      paul.push_back(P(_x, _y));
    }

    solve();

    int ans = pow(edge.first, 2) + pow(edge.second, 2);
    printf("%d\\n", ans);
  }

  return 0;
}