Giving Directions in Harbin

A. Giving Directions in Harbin

In some regions, people are more accustomed to giving directions using cardinal directions, such as: go south to the second intersection, then head east to the first intersection. However, due to the complex road network planning in Harbin, many streets do not align perfectly with cardinal directions. Thus, if you provide directions using absolute directions to someone who has lived in Harbin for a long time, they may struggle to understand your intended route.

In Harbin, people are more accustomed to using relative directions to give guidance. For the same location, a Harbin resident might first instruct you to face south, and then say: walk straight along the road to the second intersection, then turn left, and then straight to the first intersection.

To address this difference, you decide to write a program that converts the direction-giving style using cardinal directions into the style preferred by Harbin residents. Of course, using a real map of Harbin would be too complicated, so in this problem, you can assume the map is an infinitely large grid.

Input

The first line contains an integer T T T ( 1 ≤ T ≤ 1 0 4 1 \le T \le 10^4 1T104), indicating the number of test cases.

For each test case, the first line contains an integer n n n ( 1 ≤ n ≤ 10 1 \le n \le 10 1n10), indicating the number of direction instructions.

The next n n n lines each describe an instruction in absolute position, consisting of a character d d d ( d ∈ { N , S , W , E } d\in\{\texttt{N}, \texttt{S}, \texttt{W}, \texttt{E}\} d{N,S,W,E}) and an integer x x x ( 1 ≤ x ≤ 10 1 \le x \le 10 1x10), indicating “go to the x x x-th intersection in the d d d direction.” Here, N \texttt{N} N represents north, S \texttt{S} S represents south, W \texttt{W} W represents west, and E \texttt{E} E represents east.

It is guaranteed that two consecutive instructions will not have the same direction or opposite directions (north and south are opposite, as are west and east).

Output

For each test case, the first line outputs an integer m m m ( 1 ≤ m ≤ 20 1 \le m \le 20 1m20) and a character f f f ( f ∈ { N , S , W , E } f \in \{\texttt{N}, \texttt{S}, \texttt{W}, \texttt{E}\} f{N,S,W,E}), representing the number of instructions in Harbin style and the initial facing direction, with the same meanings for directions as in the input.

Next, output m m m lines. Each line starts with a character g ∈ { Z , L , R } g \in \{\texttt{Z}, \texttt{L}, \texttt{R}\} g{Z,L,R}, where Z \texttt{Z} Z means to go straight, L \texttt{L} L means to turn left, and R \texttt{R} R means to turn right. If the character is Z \texttt{Z} Z, the line must also include an integer y y y ( 1 ≤ y ≤ 100 1 \le y \le 100 1y100), representing going straight to the y y y-th intersection. The first output instruction must start with Z \texttt{Z} Z. Consecutive instructions cannot have the same character g g g, and L \texttt{L} L and R \texttt{R} R instructions cannot be adjacent.

In this problem, you do not need to minimize m m m. If there are multiple ways to reach the same destination, any valid solution is acceptable.

Example

Input

1
2
S 2
E 1

Output

3 S
Z 2
L
Z 1

code

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
 
using namespace std;


const int N = 2e5+10,INF=0x3f3f3f3f,mod=1e9+7;
 
typedef pair<int,int> PII;

int T=1;

void solve(){
	int n;
	cin>>n;
	int x=0,y=0;
	for(int i=0;i<n;i++){
		char s;
		int len;
		cin>>s>>len;
		if(s=='N') y+=len;
		if(s=='S') y-=len;
		if(s=='W') x-=len;
		if(s=='E') x+=len;
	}
//	cout<<"x和y:"<<x<<" "<<y<<endl;
	if(x==0 && y==0){
		cout<<7<<" "<<'S'<<endl;
		cout<<'Z'<<" "<<2<<endl;
		cout<<'L'<<endl;
		cout<<'Z'<<" "<<2<<endl;
		cout<<'L'<<endl;
		cout<<'Z'<<" "<<2<<endl;
		cout<<'L'<<endl;
		cout<<'Z'<<" "<<2<<endl;
		return;
	}
	char t;
	if(x==0){
		if(y>0) t='N';
		else t='S';
		cout<<1<<" "<<t<<endl;
		cout<<'Z'<<" "<<abs(y)<<endl;
		return;
	}
	if(y==0){
		if(x>0) t='E';
		else t='W';
		cout<<1<<" "<<t<<endl;
		cout<<'Z'<<" "<<abs(x)<<endl;
		return;
	}
	
	if(y>0) t='N';
	else t='S';
	
	if(x!=0 && y!=0){
		cout<<3<<" "<<t<<endl;
		cout<<'Z'<<" "<<abs(y)<<endl;
		if(x>0 && y>0){
			cout<<'R'<<endl;
			cout<<'Z'<<" "<<abs(x)<<endl;
		}else if(x<0 && y>0){
			cout<<'L'<<endl;
			cout<<'Z'<<" "<<abs(x)<<endl;
		}else if(x>0 && y<0){
			cout<<'L'<<endl;
			cout<<'Z'<<" "<<abs(x)<<endl;
		}else if(x<0 && y<0){
			cout<<'R'<<endl;
			cout<<'Z'<<" "<<abs(x)<<endl;
		}
		return;
	}
}

signed main(){
	cin>>T; 
    while(T--){
        solve();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值