Problem: Example

Input

9
0 0 v
1 0 >
2 0


Output

9

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<P ,ll> P3;
typedef pair<P ,P> PP;
const ll MOD = ll(1e9)+7;
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(1e5 + 5);
const double EPS = 1e-6;
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(), (v).end()

int n, ans = 1;
map<int, set<P> > vx, vy;
P3 pos[3005];

int main() {
    cin >> n;
    REP(i,n){
        int x, y, dir;
        char d;
        cin >> x >> y >> d;
        if(d == '>') dir = 0;
        if(d == 'v') dir = 1;
        if(d == '<') dir = 2;
        if(d == '^') dir = 3;
        vx[x].insert({y,dir});
        vy[y].insert({x,dir});
        pos[i] = {{x,y},dir};
    }
    REP(i,n){
        auto xs = vx, ys = vy;
        int px = pos[i].first.first, py = pos[i].first.second, dir = pos[i].second, cnt = 1;
        while(1){
            xs[px].erase({py,dir});
            ys[py].erase({px,dir});
            if(dir == 0){
                auto p = ys[py].upper_bound({px,-1});
                if(p==ys[py].end())break;
                px = (*p).first;
                dir = (*p).second;
                cnt++;
            }
            else if(dir == 1){
                auto p = xs[px].upper_bound({py,-1});
                if(p==xs[px].end())break;
                py = (*p).first;
                dir = (*p).second;
                cnt++;
            }
            else if(dir == 2){
                if(ys[py].size()==0) break;
                auto p = ys[py].upper_bound({px,-1});
                if(p==ys[py].begin())break;
                p--;
                px = (*p).first;
                dir = (*p).second;
                cnt++;
            }
            else if(dir == 3){
                if(xs[px].size()==0) break;
                auto p = xs[px].upper_bound({py,-1});
                if(p==xs[px].begin())break;
                p--;
                py = (*p).first;
                dir = (*p).second;
                cnt++;
            }
        }
        ans = max(ans, cnt);
    }
    cout << ans << endl;
    return 0;
}