Problem: Example

Input

9
0 0 v
1 0 >
2 0


Output

9

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef pair<int,int>P;

vector<int>v;
int x[3333],y[3333],n,ans,c;
char s[3333];
map<P,char>A;
map<int,int>M;
map<char,int>Dir;
bool used[3001][3001];
int dx[]={0,1,0,-1};
int dy[]={-1,0,1,0};

int get(int idx){
    memset(used,0,sizeof(used));
    int res=1,d;
    int X=x[idx];
    int Y=y[idx];
    d=Dir[A[P(X,Y)]];
    used[X][Y]=1;
    while(1){
        X+=dx[d];
        Y+=dy[d];
        if(X<0||Y<0||X>3000||Y>3000)break;
        if(used[X][Y])continue;
        if(A.count(P(X,Y))){
            used[X][Y]=1;
            d=Dir[A[P(X,Y)]];
            res++;
        }
    }
    return res;
}

int main(){
    Dir['^']=0;
    Dir['>']=1;
    Dir['v']=2;
    Dir['<']=3;
    cin>>n;
    r(i,n){
        cin>>x[i]>>y[i]>>s[i];
        v.push_back(x[i]);
        v.push_back(y[i]);
    }
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
    r(i,v.size())M[v[i]]=c++;
    r(i,n){
        x[i]=M[x[i]];
        y[i]=M[y[i]];
        A[P(x[i],y[i])]=s[i];
    }
    r(i,n)ans=max(ans,get(i));
    cout<<ans<<endl;
}