Algorithm
Problem Name: 2 AD-HOC - beecrowd | 1574
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1574
Robot Instructions
By Rujia Liu, Tsinghua University China
Timelimit: 1
You have a robot standing on the origin of x axis. The robot will be given some instructions. Your task is to predict its position after executing all the instructions.
- LEFT: move one unit left (decrease p by 1, where p is the position of the robot before moving)
- RIGHT: move one unit right (increase p by 1)
- SAME AS i: perform the same action as in the i-th instruction. It is guaranteed that i is a positive integer not greater than the number of instructions before this.
Input
The first line contains the number of test cases T (T <= 100). Each test case begins with an integer n ( 1 <= n <= 100), the number of instructions. Each of the following n lines contains an instruction.
Output
For each test case, print the final position of the robot. Note that after processing each test case, the robot should be reset to the origin.
Sample Input | Sample Output |
2 |
1 |
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
using namespace std;
int hatoi(const char *str)
{
int val = 0;
while (*str) {
if ('0' < = *str and *str <= '9')
val = val*10 + (*str - '0');
++str;
}
return val;
}
int sum(int *vet, int t) {
int i, s = 0;
for (i = 0; i < t; ++i) s += vet[i];
return s;
}
int main(void) {
int q, i, j, k, v;
char e[15];
cin >> q;
cin.ignore(80, '\n');
for (k = 0; k < q; ++k) {
cin >> v;
cin.ignore(80, '\n');
int vet[v];
for (j = 0; j < v; ++j) {
cin.getline(e, sizeof(e));
if (e[0] == 'L') vet[j] = -1;
else if (e[0] == 'R') vet[j] = 1;
else vet[j] = vet[hatoi(e)-1];
}
cout << sum(vet, v) << endl;
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
3
LEFT
RIGHT
SAME AS 2
5
LEFT
SAME AS 1
SAME AS 2
SAME AS 1
SAME AS 4
Output
-5
#2 Code Example with Python Programming
Code -
Python Programming
q = int(input())
for p in range(q):
s = int(input())
v = []
for r in range(s):
e = str(input()).split()
if e[0] == 'LEFT': v.append(-1)
elif e[0] == 'RIGHT': v.append(1)
else: v.append(v[int(e[2]) - 1])
print(sum(v))
Copy The Code &
Try With Live Editor
Input
3
LEFT
RIGHT
SAME AS 2
5
LEFT
SAME AS 1
SAME AS 2
SAME AS 1
SAME AS 4
Output
-5
#3 Code Example with Javascript Programming
Code -
Javascript Programming
const { readFileSync } = require("fs")
const [numCases, ...lines] = readFileSync("/dev/stdin", { encoding: "utf8" }).split("\n")
const input = (function* (lines) {
for (const line of lines) yield line
})(lines)
function main() {
const responses = []
for (let index = 0; index < +numCases; index++) {
const numInstrunctions = Number.parseInt(input.next().value, 10)
const instrunctionsList = new Array(numInstrunctions)
let position = 0
for (let j = 0; j < numInstrunctions; j++) {
const command = input.next().value ?? ""
instrunctionsList[j] = /SAME AS (\d+)/.test(command)
? instrunctionsList[+/SAME AS (\d+)/.exec(command)[1] - 1]
: command
switch (instrunctionsList[j]) {
case "LEFT": position--; break
case "RIGHT": position++; break
default: break
}
}
responses.push(position)
}
console.log(responses.join("\n"))
}
main()
Copy The Code &
Try With Live Editor
Input
3
LEFT
RIGHT
SAME AS 2
5
LEFT
SAME AS 1
SAME AS 2
SAME AS 1
SAME AS 4
Output
-5