Problem: Example

Input

9
0 0 v
1 0 >
2 0


Output

9

Solution (in CPP):

Solution:
#include <set>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int n; char c[3009][3009];
int main() {
	cin >> n;
	vector<int> x(n), y(n);
	vector<string> s(n);
	for (int i = 0; i < n; i++) cin >> x[i] >> y[i] >> s[i];
	vector<int> tx = x, ty = y;
	sort(tx.begin(), tx.end());
	tx.erase(unique(tx.begin(), tx.end()));
	sort(ty.begin(), ty.end());
	ty.erase(unique(ty.begin(), ty.end()));
	for (int i = 0; i < n; i++) {
		x[i] = lower_bound(tx.begin(), tx.end(), x[i]) - tx.begin();
		y[i] = lower_bound(ty.begin(), ty.end(), y[i]) - ty.begin();
		c[x[i]][y[i]] = s[i][0];
	}
	int ret = 0;
	for (int i = 0; i < n; i++) {
		vector<set<int> > px(tx.size());
		vector<set<int> > py(ty.size());
		for (int j = 0; j < n; j++) {
			px[x[j]].insert(y[j]);
			py[y[j]].insert(x[j]);
		}
		px[x[i]].erase(y[i]);
		py[y[i]].erase(x[i]);
		int cx = x[i], cy = y[i], cnt = 1;
		while (true) {
			if (c[cx][cy] == '<' || c[cx][cy] == '>') {
				set<int>::iterator it = py[cy].lower_bound(cx);
				if (c[cx][cy] == '<') {
					if (it == py[cy].begin()) break;
					else it--;
				}
				else if (it == py[cy].end()) break;
				cx = *it;
			}
			else {
				set<int>::iterator it = px[cx].lower_bound(cy);
				if (c[cx][cy] == '^') {
					if (it == px[cx].begin()) break;
					else it--;
				}
				else if (it == px[cx].end()) break;
				cy = *it;
				px[cx].erase(it);
			}
			px[cx].erase(cy);
			py[cy].erase(cx);
			cnt++;
		}
		ret = max(ret, cnt);
	}
	cout << ret << endl;
	return 0;
}