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 pair<int,int> P;

#define MAX_N 3005
int n;
int px[MAX_N],py[MAX_N];
char ch[MAX_N];

int solve(int si){
  bool used[MAX_N]={};
  set< P > sx[MAX_N],sy[MAX_N];
  for(int i=0;i<n;i++){
    if(i==si)continue;
    sx[ px[i] ].insert( P(py[i],i) );
    sy[ py[i] ].insert( P(px[i],i) );
  }
  int res=1;
  int x=px[si],y=py[si];
  char dir=ch[si];

  set< P > :: iterator it;
  while(1){
    P next;
    if(dir=='v'){
      it= sx[x].lower_bound( P(y,0) );
      if(it==sx[x].end())break;
      next=*it;
    }else if(dir=='^'){
      it= sx[x].lower_bound( P(y,0) );
      if(it==sx[x].begin())break;
      it--;
      next=*it;
    }else if(dir=='>'){
      it= sy[y].lower_bound( P(x,0) );
      if(it==sy[y].end())break;
      next=*it;      
    }else if(dir=='<'){
      it= sy[y].lower_bound( P(x,0) );
      if(it==sy[y].begin())break;
      it--;
      next=*it;
    }
    res++;
    int id=next.second;
    x=px[id];
    y=py[id];
    dir=ch[id];
    sx[ x ].erase(P(y,id));
    sy[ y ].erase(P(x,id));
  }
  return res;
}

int main(){
  vector<int> X,Y;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>px[i]>>py[i]>>ch[i];
    X.push_back(px[i]);
    Y.push_back(py[i]);
  }
  sort(X.begin(),X.end());
  sort(Y.begin(),Y.end());
  X.erase(unique(X.begin(),X.end()),X.end());
  Y.erase(unique(Y.begin(),Y.end()),Y.end());

  for(int i=0;i<n;i++){
    px[i]=lower_bound(X.begin(),X.end(),px[i])-X.begin();
    py[i]=lower_bound(Y.begin(),Y.end(),py[i])-Y.begin();
  }



  int ans=0;
  for(int i=0;i<n;i++){
    ans=max(ans,solve(i));
  }
  cout<<ans<<endl;
  return 0;
}