Problem: Example

Input

9
0 0 v
1 0 >
2 0


Output

9

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
#define N 3001
using namespace std;
int w,h;
int dx[]={0,1,0,-1};//^>v<
int dy[]={-1,0,1,0};
vector<int> X,Y,d;
map<int,int> x,y;

int compless(vector<int> A,map<int,int> &M){
  sort(A.begin(),A.end());
  int c=0;
  for(int i=0;i<A.size();i++)if(!M.count(A[i]))M[A[i]]=c++;
  return c;
}

int calc(int y,int x,vector<vector<int> >&mp){
  vector<vector<bool> > used(h,vector<bool>(w,0));
  int res=0;
  while(1){
    res++;
    int i=mp[y][x];
    used[y][x]=1;
    while(1){
      y+=dy[i],x+=dx[i];
      if(y<0||x<0||x>=w||y>=h)return res;
      if(mp[y][x]!=-1&&!used[y][x])break;
    }
  }
  assert(0);
}

int main(){
  int n;
  cin>>n;
  X.resize(n),Y.resize(n),d.resize(n);
  for(int i=0;i<n;i++){
    char ch;
    cin>>X[i]>>Y[i]>>ch;
    if(ch=='^')d[i]=0;
    if(ch=='>')d[i]=1;
    if(ch=='v')d[i]=2;
    if(ch=='<')d[i]=3;
  }
  h=compless(Y,y);
  w=compless(X,x);
  vector<vector<int> >mp(h,vector<int>(w,-1));
  for(int i=0;i<X.size();i++)mp[y[Y[i]]][x[X[i]]]=d[i];

  int ans=1;
  for(int i=0;i<n;i++)ans=max(ans,calc(y[Y[i]],x[X[i]],mp));
  cout <<ans<<endl;

  return 0;
}