Problem: Example

Input

9
0 0 v
1 0 >
2 0


Output

9

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;

using P = pair<int,int>;

signed main(){
	int n,x[3010],y[3010],ans = 0;
	char d[3010];
	map<int,set<P>> xy,yx;
	cin >> n;
	for(int i = 0;i < n;i++){
		cin >> x[i] >> y[i] >> d[i];
	}
	for(int i = 0;i < n;i++){
		xy.clear();
		yx.clear();
		for(int j = 0;j < n;j++){
			xy[x[j]].emplace(y[j],j);
			yx[y[j]].emplace(x[j],j);
		}
		int v = i,cnt = 0;
		while(1){
			int nv;
			cnt++;
			if(d[v] == '^'){
				auto it = xy[x[v]].find(P(y[v],v));
				if(it == xy[x[v]].begin()) break;
				nv = (*(--it)).second;
			}else if(d[v] == 'v'){
				auto it = xy[x[v]].find(P(y[v],v));
				if(++it == xy[x[v]].end()) break;
				nv = (*it).second;
			}else if(d[v] == '<'){
				auto it = yx[y[v]].find(P(x[v],v));
				if(it == yx[y[v]].begin()) break;
				nv = (*(--it)).second;
			}else{
				auto it = yx[y[v]].find(P(x[v],v));
				if(++it == yx[y[v]].end()) break;
				nv = (*it).second;
			}
			xy[x[v]].erase(P(y[v],v));
			yx[y[v]].erase(P(x[v],v));
			v = nv;
		}
		ans = max(ans,cnt);
	}
	cout << ans << endl;
}