csuoj 1112: 机器人的指令


http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1112


1112: 机器人的指令

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 1153  Solved: 386
[Submit][Status][Web Board]

Description

数轴原点有一个机器人。该机器人将执行一系列指令,你的任务是预测所有指令执行完毕之后它的位置。

·LEFT:往左移动一个单位

·RIGHT: 往右移动一个单位

·SAME AS i: 和第i 条执行相同的动作。输入保证i 是一个正整数,且不超过之前执行指令数

Input

输入第一行为数据组数T (T<=100)。每组数据第一行为整数n (1<=n<=100),即指令条数。以下每行一条指令。指令按照输入顺序编号为1~n。

Output

对于每组数据,输出机器人的最终位置。每处理完一组数据,机器人应复位到数轴原点。

Sample Input

2
3
LEFT
RIGHT
SAME AS 2
5
LEFT
SAME AS 1
SAME AS 2
SAME AS 1
SAME AS 4

Sample Output

1
-5

HINT

 

Source




分析:

直接模拟移动即可。



AC代码:

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<queue>
 5 #include<iostream>
 6 #include<stack>
 7 #include<map>
 8 #include<string>
 9 using namespace std;
10 char order[100][15];
11 char ch[15];
12 int main(){
13     int n, tcase, i;
14     scanf("%d", &tcase);
15     while(tcase--){
16         int ans = 0;
17         int cnt = 0;
18         scanf("%d", &n);
19         while(n--){
20         scanf("%s", ch);
21             if(ch[0] == 'S'){
22                 scanf("%s %d", ch, &i);
23                 ch[0] = order[i][0];
24             }
25             if(ch[0] == 'L'){
26                 ans--;
27             }
28             else if(ch[0] == 'R'){
29                 ans++;
30             }
31             order[++cnt][0] = ch[0];
32         }
33         printf("%d
", ans);
34     }
35     return 0;
36 }
View Code
原文地址:https://www.cnblogs.com/jeff-wgc/p/4471075.html