-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUVa 12503 - Robot Instructions
96 lines (88 loc) · 2.65 KB
/
UVa 12503 - Robot Instructions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <iostream>
#include<vector>
using namespace std;
int main()
{
//記得要使用getline, 因為會有 "SAME AS 2" 這種string, 用cin 會出錯。
int t;
cin>>t;
while(t--){
int n;
cin>>n;
vector<int> vec (n);
string instruction;
getline(cin,instruction);
for(int i=0;i<n;i++){
getline(cin,instruction);
if(instruction == "LEFT"){
vec[i]=1;
}
else if(instruction =="RIGHT"){
vec[i]=-1;
}
else{
int number=0;
for(int j=8;j<instruction.length();j++){
number = number*10 + (instruction[j]-'0');
}
vec[i]=vec[number-1];
}
}
int ans=0;
for(int i=0;i<vec.size();i++){
ans+=vec[i];
}
cout<<ans<<endl;
}
return 0;
}
/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <iostream>
#include<vector>
using namespace std;
int main()
{
int t;
cin>>t;
cin.ignore();
while(t--){
int n;
cin>>n;
cin.ignore();
vector<int> vec;
string instruction;
for(int i=0;i<n;i++){
getline(cin,instruction);
if(instruction == "LEFT"){
vec.push_back(1);
}
else if(instruction =="RIGHT"){
vec.push_back(-1);
}
else{
int number=0;
for(int j=8;j<instruction.length();j++){
number = number*10 + (instruction[j]-'0');
}
vec.push_back(vec[number-1]);
}
}
int ans=0;
for(int i=0;i<vec.size();i++){
ans+=vec[i];
}
cout<<ans<<endl;
}
return 0;
}