Problem: Example

Input

9
0 0 v
1 0 >
2 0


Output

9

Solution (in CPP):

Solution:
#include <bits/stdc++.h>

using namespace std;

const string vs = "^v<>";

int main()
{

  int N, X[3000], Y[3000], Z[3000];
  cin >> N;
  map< pair< int, int >, int > xx, yy;
  for(int i = 0; i < N; i++) {
    char zz;
    cin >> X[i] >> Y[i] >> zz;
    Z[i] = vs.find(zz);
    xx[{X[i], Y[i]}] = i;
    yy[{Y[i], X[i]}] = i;
  }

  xx[{-1145141919, -1145141919}] = -1;
  xx[{1145141919, 1145141919}] = -1;
  yy[{-1145141919, -1145141919}] = -1;
  yy[{1145141919, 1145141919}] = -1;


  int ret = 0;

  for(int i = 0; i < N; i++) {
    auto aa = xx, bb = yy;
    aa.erase({X[i], Y[i]});
    bb.erase({Y[i], X[i]});

    int now = i, sum = 1;
    bool latte = true;
    for(;; sum++) {
      latte = false;
      if(Z[now] >= 2) {
        auto pp = bb.lower_bound({Y[now], X[now]});
        if(Z[now] % 2 == 0) --pp;
        if(pp->first.first != Y[now]) break;
        now = pp->second;
      } else {
        auto pp = aa.lower_bound({X[now], Y[now]});
        if(Z[now] % 2 == 0) --pp;
        if(pp->first.first != X[now]) break;
        now = pp->second;
      }
      aa.erase({X[now], Y[now]});
      bb.erase({Y[now], X[now]});
    }

    ret = max(ret, sum);
  }

  cout << ret << endl;
}