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

// ?????¬???????????¬??????

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

// calculate vector x which satisfy Ax = b
// (A is N*N Matrix, b, x is N-dim vector)

template <typename T> using Matrix = vector<vector<T>>;
constexpr double EPS = 1e-8;

template <typename T>
vector<double> gauss_jordan(const Matrix<T> &A, const vector<T> &b) {
  int N = A.size();
  // B = [A b]
  Matrix<double> B(N, vector<double>(N + 1));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= N; j++) {
      if (j < N)
        B[i][j] = A[i][j];
      else
        B[i][j] = b[i];
    }
  }

  for (int i = 0; i < N; i++) {
    int pivot = i;
    for (int j = i + 1; j < N; j++) {
      if (abs(B[j][i]) > abs(B[pivot][i]))
        pivot = j;
    }
    swap(B[i], B[pivot]);

    // pivot is zero -> No solution or Not unique
    // assert(abs(B[i][i]) >= EPS);

    for (int j = i + 1; j <= N; j++)
      B[i][j] /= B[i][i];
    for (int j = 0; j < N; j++) {
      if (i != j) {
        for (int k = i + 1; k <= N; k++)
          B[j][k] -= B[j][i] * B[i][k];
      }
    }
  }
  vector<double> x(N);
  for (int i = 0; i < N; i++)
    x[i] = B[i][N];
  return x;
}

double pos[5][3];
signed main() {
  rep(i, 0, 5) rep(j, 0, 3) cin >> pos[i][j];

  Matrix<double> mat(3, vector<double>(3));
  vector<double> vec(3);

  rep(i, 0, 3) {
    mat[i][0] = pos[3][i] - pos[2][i];
    mat[i][1] = pos[4][i] - pos[2][i];
    mat[i][2] = pos[0][i] - pos[1][i];
  }
  rep(i, 0, 3) vec[i] = pos[0][i] - pos[2][i];

  vector<double> ans = gauss_jordan(mat, vec);
  if (ans[0] < -EPS || ans[1] < -EPS || ans[2] < -EPS)
    cout << "HIT" << endl;
  else if (ans[0] + ans[1] > 1.0 + EPS || ans[2] > 1.0 + EPS)
    cout << "HIT" << endl;
  else
    cout << "MISS" << endl;
  return 0;
}

Error: 0

Fixed submission:

// ?????¬???????????¬??????

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

// calculate vector x which satisfy Ax = b
// (A is N*N Matrix, b, x is N-dim vector)

template <typename T> using Matrix = vector<vector<T>>;
constexpr double EPS = 1e-8;

template <typename T>
vector<double> gauss_jordan(const Matrix<T> &A, const vector<T> &b) {
  int N = A.size();
  // B = [A b]
  Matrix<double> B(N, vector<double>(N + 1));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= N; j++) {
      if (j < N)
        B[i][j] = A[i][j];
      else
        B[i][j] = b[i];
    }
  }

  for (int i = 0; i < N; i++) {
    int pivot = i;
    for (int j = i + 1; j < N; j++) {
      if (abs(B[j][i]) > abs(B[pivot][i]))
        pivot = j;
    }
    swap(B[i], B[pivot]);

    // pivot is zero -> No solution or Not unique
    assert(abs(B[i][i]) >= EPS);

    for (int j = i + 1; j <= N; j++)
      B[i][j] /= B[i][i];
    for (int j = 0; j < N; j++) {
      if (i != j) {
        for (int k = i + 1; k <= N; k++)
          B[j][k] -= B[j][i] * B[i][k];
      }
    }
  }
  vector<double> x(N);
  for (int i = 0; i < N; i++)
    x[i] = B[i][N];
  return x;
}

double pos[5][3];
signed main() {
  rep(i, 0, 5) rep(j, 0, 3) cin >> pos[i][j];

  Matrix<double> mat(3, vector<double>(3));
  vector<double> vec(3);

  rep(i, 0, 3) {
    mat[i][0] = pos[3][i] - pos[2][i];
    mat[i][1] = pos[4][i] - pos[2][i];
    mat[i][2] = pos[0][i] - pos[1][i];
  }
  rep(i, 0, 3) vec[i] = pos[0][i] - pos[2][i];

  vector<double> ans = gauss_jordan(mat, vec);
  if (ans[0] < -EPS || ans[1] < -EPS || ans[0] + ans[1] > 1.0 + EPS ||
      ans[2] < -EPS)
    cout << "HIT" << endl;
  else
    cout << "MISS" << endl;
  return 0;
}