• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • YouClaw
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

Uva 12503 - Robot Instructions

你为何这么叼!

Robot Instructions 

Time limit: 1.000 seconds 

 

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$ \le$100). Each test case begins with an integer n ( 1$ \le$n$ \le$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 

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

 


Problemsetter: Rujia Liu, Special Thanks: Feng Chen, Md. Mahbubul Hasan

 

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 #define maxn 111
 6 int n,m,k;
 7 int a[maxn];
 8 int main(){
 9     int t;
10     scanf("%d",&t);
11     while(t--){
12         scanf("%d",&n);
13         int x=0,i=1;
14         while(n--){
15             char op[31];
16             scanf("%s",op);
17             if(op[0]=='L'){x--;a[i++]=-1;continue;}
18             if(op[0]=='R'){x++;a[i++]=1;continue;}
19             scanf("%s%d",op,&k);x+=a[k];a[i++]=a[k];
20         }
21         printf("%d\n",x);
22     }
23     return 0;
24 }
View Code 2013-10-11 19:07:07 

 

posted @ 2013-10-11 19:06  HaibaraAi  阅读(124)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3