문제
Kile, a board games enthusiast, recently discovered the game Robots. The game consists of a board with
At the beginning, the robot is positioned on some field
The board has three types of fields:
Empty field - the robot continues moving in the same direction
Left turn field - when the robot steps on this field, it will turn left by
90 ° and continue movingRight turn field - when the robot steps on this field, it will turn right by
90 ° and continue moving
Most fields on the board are empty, only
The game consists of
After playing this game several times, Kile realized that it is more challenging than it initially seemed. That is why he needs your help now. Help him determine the minimum number of turns required for each round of the game!
Note: If the robot starts or finishes its path on a left or right turn field, that turn is not counted.
입력
The first line contains integers
The i-th of the following
The next line contains integer
The
출력
In the
부분문제
번호 | 점수 | 조건 |
---|---|---|
#1 | 10점 | |
#2 | 13점 | |
#3 | 49점 | |
#4 | 38점 | No additional constraints. |
예제1
22 2
1 1 L
2 2 R
5
1 1 2 2
2 1 1 2
1 1 1 2
2 1 1 1
2 2 2 1
-1
1
0
0
0
예제2
33 4
1 1 L
1 3 L
2 1 L
3 3 L
7
1 1 3 3
3 3 2 1
3 1 2 2
2 3 1 2
2 3 3 1
1 2 3 2
3 3 2 2
1
2
1
1
1
0
3
First round: We start at
Second round: We start at
예제3
44 8
1 1 R
1 3 L
2 2 R
2 4 L
3 1 L
3 3 L
4 2 L
4 4 L
7
1 2 1 4
2 2 3 4
4 4 3 2
4 1 4 4
4 2 3 1
1 2 2 3
2 4 2 3
2
1
1
0
-1
5
0