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

#include <algorithm>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;

// vector cout
template <typename _Ty> ostream &operator<<(ostream &os, const vector<_Ty> &v) {
  os << "{";
  for (size_t i = 0; i < v.size(); i++) {
    if (i != 0)
      os << ", ";
    os << v[i];
  }
  os << "}";

  return os;
}

// point
class Point {
public:
  double x;
  double y;

  Point() {
    x = 0;
    y = 0;
  }
  Point(int x, int y) {
    this->x = x;
    this->y = y;
  }

  bool operator==(const Point &a) {
    if (x != a.x)
      return false;
    if (y != a.y)
      return false;
    return true;
  }

  bool operator!=(const Point &a) {
    if (x != a.x)
      return true;
    if (y != a.y)
      return true;
    return false;
  }

  bool operator<(const Point &a) {
    if (x == a.x)
      return (y < a.y);
    return x < a.x;
  }
};

// point cout
ostream &operator<<(ostream &os, const Point &p) {
  return os << "(" << p.x << ", " << p.y << ")";
}

// AB ?? AC
double crossProduct(Point A, Point B, Point C) {
  return (B.x - A.x) * (C.y - A.y) - (B.y - A.y) * (C.x - A.x);
}

double distanceSquare(Point A, Point B) {
  return (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y);
}

// S:????????????????????????
int convexhull(vector<Point> &S) {
  // L:??????????§?????????????????????????
  vector<Point> L;

  // sort
  sort(S.begin(), S.end());

  Point A, B, C;

  A = S[0];
  do {
    L.push_back(A);
    B = S[0];
    for (int i = 1; i < S.size(); i++) {
      C = S[i];
      if (B == A)
        B = C;
      else {
        double v = crossProduct(A, B, C);
        if (v > 0 ||
            ((fabs(v) < 1e-8) && (distanceSquare(A, C) > distanceSquare(A, B))))
          B = C;
      }
    }
    A = B;
  } while (A != L[0]);

  return S.size() - L.size();
}

int main() {

  while (1) {

    int n;
    vector<Point> Points;
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      Point p;
      string input;
      cin >> input;

      stringstream ss(input);

      ss >> p.x;
      ss.ignore();
      ss >> p.y;

      Points.push_back(p);
    }
    cout << convexhull(Points) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;

// vector cout
template <typename _Ty> ostream &operator<<(ostream &os, const vector<_Ty> &v) {
  os << "{";
  for (size_t i = 0; i < v.size(); i++) {
    if (i != 0)
      os << ", ";
    os << v[i];
  }
  os << "}";

  return os;
}

// point
class Point {
public:
  double x;
  double y;

  Point() {
    x = 0;
    y = 0;
  }
  Point(int x, int y) {
    this->x = x;
    this->y = y;
  }

  bool operator<(const Point &a) const {
    if (x == a.x)
      return y < a.y;
    else
      return x < a.x;
  }
};

// point cout
ostream &operator<<(ostream &os, const Point &p) {
  return os << "(" << p.x << ", " << p.y << ")";
}

void rec(list<Point *> &Hull, list<Point *> &Points, Point *p1, Point *p2,
         int sgn) {

  double MaxDistance = 0;
  Point *pMaxDistance = NULL;
  list<Point *>::iterator it; // iterator

  // sgn?????????????????¨?????????
  list<Point *> sgnPoints;

  // sgn ????????????????????§??´??? p1-p2
  // ??????????????¢?????§??????????±???????
  for (it = Points.begin(); it != Points.end(); it++) // list???????°??????§
  {
    Point *p = *it;
    double d =
        (p2->x - p1->x) * (p->y - p1->y) - (p2->y - p1->y) * (p->x - p1->x);

    // 0 ????????´???????????£?????? sgn ??????????????¨
    double reg = sgn * d;

    //??´????????§????????°??????
    if (fabs(reg) < 1e-8) {
      Points.erase(it);
      continue;
    }

    //??£?????????????????????????????¢?????????
    if (reg > 0) {
      sgnPoints.push_back(*it);

      if (fabs(d) > MaxDistance) {
        MaxDistance = fabs(d);
        pMaxDistance = p;
      }
    }
  }

  //?????¨??????????????°??????
  if (pMaxDistance == NULL)
    return;

  // p0 ??????????????????
  Point *p0 = pMaxDistance;
  Hull.push_back(p0);
  Points.remove(p0);
  sgnPoints.remove(p0);

  //????§???¢ p0, p1, p2 ?????¨????????¨??????????????????
  for (it = sgnPoints.begin(); it != sgnPoints.end(); it++) {
    Point *pl = *it;

    Point *p[3] = {p0, p1, p2};
    // cross product
    double cp[3];
    for (int i = 0; i < 3; i++) {
      cp[i] = (p[(i + 1) % 3]->x - p[i]->x) * (pl->y - p[i]->y) -
              (p[(i + 1) % 3]->y - p[i]->y) * (pl->x - p[i]->x);
    }
    //?????¨???????????????
    if ((cp[0] < 0 && cp[1] < 0 && cp[2] < 0) ||
        (cp[0] > 0 && cp[1] > 0 && cp[2] > 0)) {
      //??????
      Points.remove(*it);
    }
  }

  //??´??? (p0, p1) ???????????? p2 ????¬????
  double sp2 =
      (p1->x - p0->x) * (p2->y - p0->y) - (p1->y - p0->y) * (p2->x - p0->x);
  if (sp2 < 0)
    rec(Hull, Points, p0, p1, 1);
  else
    rec(Hull, Points, p0, p1, -1);

  //??´??? (p0, p2) ???????????? p1 ????¬????
  double sp1 =
      (p2->x - p0->x) * (p1->y - p0->y) - (p2->y - p0->y) * (p1->x - p0->x);
  if (sp1 < 0)
    rec(Hull, Points, p0, p2, 1);
  else
    rec(Hull, Points, p0, p2, -1);

  return;
}

int convexhull(vector<Point> &vPoints) {
  // sort
  sort(vPoints.begin(), vPoints.end());

  // list ??? vPoints ??????????´???????????????????????´?
  list<Point *> lPoints;
  for (int i = 0; i < vPoints.size(); i++) {
    lPoints.push_back(&vPoints[i]);
  }

  //??????????§?????????????????????????
  list<Point *> Hull;

  // x??§?¨?????°????????????§?????????????????????
  Hull.push_back(lPoints.front());
  Hull.push_back(lPoints.back());

  // p1, p2
  Point *p1 = lPoints.front();
  Point *p2 = lPoints.back();

  //??????
  lPoints.pop_front();
  lPoints.pop_back();

  //?¬??????£???????????¢?´¢
  rec(Hull, lPoints, p1, p2, 1);
  //?¬??????????????????¢?´¢
  rec(Hull, lPoints, p1, p2, -1);

  return vPoints.size() - Hull.size();
}

int main() {

  while (1) {

    int n;
    vector<Point> Points;
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      Point p;
      string input;
      cin >> input;

      stringstream ss(input);

      ss >> p.x;
      ss.ignore();
      ss >> p.y;

      Points.push_back(p);
    }
    cout << convexhull(Points) << endl;
  }

  return 0;
}