Problem: Example

Input

9
0 0 v
1 0 >
2 0


Output

9

Solution (in CPP):

Solution:
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 3000

enum DIR{
	North,
	East,
	South,
	West,
};

struct Info{
	int x,y;
	DIR dir;
};

typedef pair<int,int> Data;
int N;
map<int,int> X_MAP,Y_MAP;
vector<int> Y,X;
Info info[NUM];
set<Data> SET_X[NUM],SET_Y[NUM];

int main(){

	scanf("%d",&N);

	char buf[2];

	for(int i = 0; i < N; i++){
		scanf("%d %d %s",&info[i].x,&info[i].y,buf);
		X.push_back(info[i].x);
		Y.push_back(info[i].y);
		switch(buf[0]){
		case '^':
			info[i].dir = North;
			break;
		case '>':
			info[i].dir = East;
			break;
		case 'v':
			info[i].dir = South;
			break;
		case '<':
			info[i].dir = West;
			break;
		}
	}

	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 < X.size(); i++){
		X_MAP[X[i]] = i;
	}
	for(int i = 0; i < Y.size(); i++){
		Y_MAP[Y[i]] = i;
	}

	int size_X = (int)X.size(),size_Y = (int)Y.size();

	for(int i = 0; i < N; i++){
		info[i].y = Y_MAP[info[i].y];
		info[i].x = X_MAP[info[i].x];
	}

	int ans = 0;
	int x,y,next_index;
	DIR dir;
	Data tmp;
	bool FLG;

	for(int start = 0; start < N; start++){

		for(int i = 0; i < NUM; i++){
			SET_X[i].clear();
			SET_Y[i].clear();
		}

		int tmp_count = 1;

		for(int i = 0; i < N; i++){
			if(i == start)continue;
			SET_X[info[i].x].insert(Data(info[i].y,i));
			SET_Y[info[i].y].insert(Data(info[i].x,i));
		}

		dir = info[start].dir;
		x = info[start].x;
		y = info[start].y;

		FLG = true;

		set<Data>::iterator it;

		while(true){

			switch(dir){
			case North:
				it = SET_X[x].lower_bound(Data(y,0));
				if(it == SET_X[x].begin()){
					FLG = false;
				}else{
					it--;
					tmp = *it;
					next_index = tmp.second;
				}
				break;
			case East:
				it = SET_Y[y].lower_bound(Data(x,0));
				if(it == SET_Y[y].end()){
					FLG = false;
				}else{
					tmp = *it;
					next_index = tmp.second;
				}
				break;
			case South:
				it = SET_X[x].lower_bound(Data(y,0));
				if(it == SET_X[x].end()){
					FLG = false;
				}else{
					tmp = *it;
					next_index = tmp.second;
				}
				break;
			case West:
				it = SET_Y[y].lower_bound(Data(x,0));
				if(it == SET_Y[y].begin()){
					FLG = false;
				}else{
					it--;
					tmp = *it;
					next_index = tmp.second;
				}
				break;
			}

			if(!FLG)break;

			tmp_count++;
			x = info[next_index].x;
			y = info[next_index].y;
			dir = info[next_index].dir;
			SET_X[x].erase(Data(y,next_index));
			SET_Y[y].erase(Data(x,next_index));
		}
		ans = max(ans,tmp_count);
	}

	printf("%d\n",ans);

	return 0;
}