Problem: Example

Input

9
0 0 v
1 0 >
2 0


Output

9

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define pb emplace_back
typedef long long ll;
typedef pair<int,int> pint;

map<int,set<int> > xx,yy;
map<pint,char> d;
int x[3001],y[3001];
const int INF=1000100010;
int main(){
    int n;
    cin>>n;
    char c;
    rep(i,n){
        cin>>x[i]>>y[i]>>c;
        xx[x[i]].insert(y[i]);
        yy[y[i]].insert(x[i]);
        d[pint(x[i],y[i])]=c;
    }
    for(auto it:xx){
        it.second.insert(INF);
        it.second.insert(-INF);
        yy[it.first].insert(INF);
        yy[it.first].insert(-INF);
    }
    for(auto it:yy){
        it.second.insert(INF);
        it.second.insert(-INF);
        xx[it.first].insert(INF);
        xx[it.first].insert(-INF);
    }
    int mx=0;
    rep(i,n){
        int cnt=0;
        int tx=x[i],ty=y[i];
        map<int,set<int> > sx=xx,sy=yy;
        while(1){
            if(sx[tx].empty()||sy[ty].empty()) break;
            ++cnt;
            sx[tx].erase(ty),sy[ty].erase(tx);
            char tc=d[pint(tx,ty)];
            if(tc=='v'){
                ty=*sx[tx].lower_bound(ty);
            }
            else if(tc=='^'){
                auto p=sx[tx].lower_bound(ty);
                if(p==sx[tx].begin()) break;
                --p;
                ty=*p;
            }
            else if(tc=='<'){
                auto p=sy[ty].lower_bound(tx);
                if(p==sy[ty].begin()) break;
                --p;
                tx=*p;

            }
            else if(tc=='>'){
                tx=*sy[ty].lower_bound(tx);
            }
            else break;
            if(abs(tx)==INF||abs(ty)==INF) break;
        }
        mx=max(mx,cnt);
    }
    cout<<mx<<endl;
    return 0;
}