Algorithm


Problem Name: 2 AD-HOC - beecrowd | 1437

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1437

Turn Left!

 

By Ricardo Anido Brazil

Timelimit: 1

This year the sergeant is having more work than usual to train the recruits. One of them is very clumsy, and occasionally does everything wrong - for example, instead of turning right when commanded, he turns left, causing great confusion in the battalion. The sergeant has a reputation for tough and will not let the recruit in peace while he does not learn how to properly execute the commands. On Saturday afternoon, while all other recruits are off, he forced the recruit to do an extra training. With the recruit marching, stopped in the same place, the sergeant issued a series of commands "Turn left!" and "Turn right!". For each command, the recruit must rotate about the same point and give a quarter turn in the direction corresponding to the command. For example, if the recruit is initially with his face oriented to the north, after a command "Turn left!" he should stay with his face oriented to the west. If the recruit is initially with his face oriented to the east, after a command "Turn right!" He must have his face oriented to the south. However, during training in which the recruit had initially his face turned to the north, the sergeant issued a series of commands so extensive, and so quickly, that even he was confused and does not know which direction the recruit must have its face turned to after the execution of all the commands. Can you help the sergeant?

 

Input

 

The input contains several test cases. The first line of a test case contains an integer N indicating the number of commands issued by the sergeant (1 ≤ N ≤ 1000). The second line contains N characters, describing a series of commands issued by the Sergeant. Each command is represented by an letter: 'E' (for "Turn left!") and 'D' (for "Turn right!"). The end of input is indicated by N = 0.

The input must be read from standard input.

 

Output

 

For each test case in the input your program must produce one line of output, indicating the direction in which the recruit must have its face turned after performing a series of commands, considering that at the beginning the recruit has its face turned to north . The line should contain a letter between 'N', 'L', 'S' and 'O', representing respectively the directions north, east, south and west.

The output should be written to standard output.

 

 

 

Sample Input Sample Output

3
DDE
2
EE
0

L
S

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <stdio.h>
#include <string.h>

int main(void) {
    int n, p, i, f;
    char c[1002];

    while (scanf("%d", &n) && n != 0) {
        getchar();
        scanf("%s", &c);
        p = 90;
        for (i = 0; i  <  strlen(c); ++i) {
            if (c[i] == ' ') continue;
            if (c[i] == 'D') p -= 90;
            else p += 90;
        }
        f = p % 360;
        if (f == 0) printf("L\n");
        else if (f == 90) printf("N\n");
        else if (f == 180) printf("O\n");
        else printf("S\n");
    }
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3
DDE
2
EE
0

Output

x
+
cmd
L
S

#2 Code Example with C++ Programming

Code - C++ Programming


#include <iostream>
#include <cstring>

using namespace std;

int main(void) {
    int n, p, i, f;
    char c[1002];

    while (cin >> n && n != 0) {
        getchar();
        cin >> c;
        p = 90;
        for (i = 0; i  <  strlen(c); ++i) {
            if (c[i] == ' ') continue;
            if (c[i] == 'D') p -= 90;
            else p += 90;
        }
        f = p % 360;
        if (f == 0) cout << "L" << endl;
        else if (f == 90) cout << "N" << endl;
        else if (f == 180) cout << "O" << endl;
        else cout << "S" << endl;
    }
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3
DDE
2
EE
0

Output

x
+
cmd
L
S

#3 Code Example with Java Programming

Code - Java Programming


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Main {
   static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(System.out);
    static char[] DIRECTIONS = {'N', 'L', 'S', 'O'};

    public static void main(String[] args) throws IOException {
        String line;
        int countD, countE, originaLength, rotations;
        while (!(line = read()).equals("0")) {
            line = read();
            originaLength = line.length();
            line = line.replaceAll("E", "");
            countD = line.length();
            countE = originaLength - countD;

            if (countD >= countE) {
                rotations = (countD - countE) % 4;
            } else {
                rotations = (countE - countD) % 4;
                if (rotations % 2 != 0) {
                    rotations = (rotations == 3) ? 1 : 3;
                }
            }
            out.println(DIRECTIONS[rotations]);
        }
        out.close();
    }

    private static String read() throws IOException {
        return in.readLine();
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3
DDE
2
EE
0

Output

x
+
cmd
L
S

#4 Code Example with Javascript Programming

Code - Javascript Programming


const { readFileSync } = require("node:fs")
const input = readFileSync("/dev/stdin", "utf8").split("\n")

const CardinalPointsEnum = Object.freeze({
	NORTH: "N",
	LEST: "L",
	SOUTH: "S",
	WEST: "O",
})


function main() {
	const output = []

	for (let index = 0; index  <  input.length; index += 2) {
		if (input[index] === "0") break

		const finalCardinalPointIndex = input[index + 1]
			.split("", Number.parseInt(input[index], 10))
			.reduce((final, turn) => {
				switch (turn) {
					case "D": return final + 1
					case "E": return final - 1
					default: return final
				}
			}, 0 /* Starts point to North */)

		switch (((finalCardinalPointIndex % 4) + 4) % 4) {
			case 0: output.push(CardinalPointsEnum.NORTH); break
			case 1: output.push(CardinalPointsEnum.LEST); break
			case 2: output.push(CardinalPointsEnum.SOUTH); break
			case 3: output.push(CardinalPointsEnum.WEST); break
		}
	}

	console.log(output.join("\n"))
}

main()
Copy The Code & Try With Live Editor

Input

x
+
cmd
3
DDE
2
EE
0

Output

x
+
cmd
L
S

#5 Code Example with Python Programming

Code - Python Programming


while True:
    n = int(input())
    if n == 0: break
    e = str(input()).replace(' ', '')
    pos = 90
    for c in e:
        if c == 'D': pos -= 90
        else: pos += 90
    fin = pos % 360
    if fin == 0: print('L')
    elif fin == 90: print('N')
    elif fin == 180: print('O')
    else: print('S')
Copy The Code & Try With Live Editor

Input

x
+
cmd
3
DDE
2
EE
0

Output

x
+
cmd
L
S
Advertisements

Demonstration


Previous
#1435 Beecrowd Online Judge Solution 1435 Square Matrix I Solution in C++, Java, Js and Python
Next
#1441 Beecrowd Online Judge Solution 1441 Hailstone Sequences Solution in C, C++, Java, Js and Python