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代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<iostream>
#include<stack>
#include<map>
#include<string>
using namespace std;
char order[][];
char ch[];
int main(){
int n, tcase, i;
scanf("%d", &tcase);
while(tcase--){
int ans = ;
int cnt = ;
scanf("%d", &n);
while(n--){
scanf("%s", ch);
if(ch[] == 'S'){
scanf("%s %d", ch, &i);
ch[] = order[i][];
}
if(ch[] == 'L'){
ans--;
}
else if(ch[] == 'R'){
ans++;
}
order[++cnt][] = ch[];
}
printf("%d\n", ans);
}
return ;
}