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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int n;
typedef pair<int, int> P;
P pole[3005];
bool field[5005][5005];

int main() {
  while (1) {
    scanf("%d", &n);
    if (n == 0) {
      break;
    }
    int ans = 0;
    fill(pole, pole + 3005, P(0, 0));
    for (int i = 0; i < 5005; i++) {
      for (int j = 0; j < 5005; j++) {
        field[i][j] = false;
      }
    }
    for (int i = 0; i < n; i++) {
      scanf("%d%d", &pole[i].first, &pole[i].second);
      field[pole[i].first][pole[i].second] = true;
    }
    sort(pole, pole + n);
    /*for(int i = 0 ; i < n; i++){
            printf("%d %d  ", pole[i].first, pole[i].second);
    }
    printf("\\n");
    for(int y = 9; y >= 0; y--){
            for(int x = 0; x < 9; x++){
                    if(field[x][y]){
                            printf("P ");
                    }else{
                            printf(". ");
                    }
            }
            printf("\\n");
    }*/
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (i == j) {
          continue;
        }
        P p1 = pole[i];
        P p2 = pole[j];
        P p3 = P(p1.first - (p2.second - p1.second),
                 p1.second + (p2.first - p1.first)); // 3???????????????
        P p4 = P(p2.first - (p2.second - p1.second),
                 p2.second + (p2.first - p1.first)); // 4???????????????
        if (p3.first >= 0 && p3.first < 5000 && p3.second >= 0 &&
            p3.second < 5000 && p4.first >= 0 && p4.first < 5000 &&
            p4.second >= 0 && p4.second < 5000 && field[p3.first][p3.second] &&
            field
                [p4.first]
                [p4.second]) { // 3,
                               // 4????????????????????????????????§??????????????¨???????????????????????§??????
          ans = max(ans, ((p2.second - p1.second) * (p2.second - p1.second) +
                          (p2.first - p1.first) * (p2.first - p1.first)));
        }
        p3 = P(
            p1.first + (p2.second - p1.second),
            p1.second -
                (p2.first -
                 p1.first)); //??????(p1,p2)????????????????????´???????????????????????§???
        p4 = P(p2.first + (p2.second - p1.second),
               p2.second - (p2.first - p1.first));
        if (p3.first >= 0 && p3.first < 5000 && p3.second >= 0 &&
            p3.second < 5000 && p4.first >= 0 && p4.first < 5000 &&
            p4.second >= 0 && p4.second < 5000 && field[p3.first][p3.second] &&
            field[p4.first][p4.second]) {
          ans = max(ans, ((p2.second - p1.second) * (p2.second - p1.second) +
                          (p2.first - p1.first) * (p2.first - p1.first)));
        }
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int n;
typedef pair<int, int> P;
P pole[3005];
bool field[5005][5005];

int main() {
  while (1) {
    scanf("%d", &n);
    if (n == 0) {
      break;
    }
    int ans = 0;
    fill(pole, pole + 3005, P(0, 0));
    for (int i = 0; i < 5005; i++) {
      for (int j = 0; j < 5005; j++) {
        field[i][j] = false;
      }
    }
    for (int i = 0; i < n; i++) {
      scanf("%d%d", &pole[i].first, &pole[i].second);
      field[pole[i].first][pole[i].second] = true;
    }
    sort(pole, pole + n);
    /*for(int i = 0 ; i < n; i++){
            printf("%d %d  ", pole[i].first, pole[i].second);
    }
    printf("\\n");
    for(int y = 9; y >= 0; y--){
            for(int x = 0; x < 9; x++){
                    if(field[x][y]){
                            printf("P ");
                    }else{
                            printf(". ");
                    }
            }
            printf("\\n");
    }*/
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (i == j) {
          continue;
        }
        P p1 = pole[i];
        P p2 = pole[j];
        P p3 = P(p1.first - (p2.second - p1.second),
                 p1.second + (p2.first - p1.first)); // 3???????????????
        P p4 = P(p2.first - (p2.second - p1.second),
                 p2.second + (p2.first - p1.first)); // 4???????????????
        if (p3.first >= 0 && p3.second >= 0 && p4.first >= 0 &&
            p4.second >= 0 && field[p3.first][p3.second] &&
            field
                [p4.first]
                [p4.second]) { // 3,
                               // 4????????????????????????????????§??????????????¨???????????????????????§??????
          ans = max(ans, ((p2.second - p1.second) * (p2.second - p1.second) +
                          (p2.first - p1.first) * (p2.first - p1.first)));
        }
        p3 = P(
            p1.first + (p2.second - p1.second),
            p1.second -
                (p2.first -
                 p1.first)); //??????(p1,p2)????????????????????´???????????????????????§???
        p4 = P(p2.first + (p2.second - p1.second),
               p2.second - (p2.first - p1.first));
        if (p3.first >= 0 && p3.second >= 0 && p4.first >= 0 &&
            p4.second >= 0 && field[p3.first][p3.second] &&
            field[p4.first][p4.second]) {
          ans = max(ans, ((p2.second - p1.second) * (p2.second - p1.second) +
                          (p2.first - p1.first) * (p2.first - p1.first)));
        }
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}