码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 12503 Robot Instructions (B)

时间:2014-08-17 02:27:21      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   strong   for   

 Robot Instructions 

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 (Tbubuko.com,布布扣100). Each test case begins with an integer n ( 1bubuko.com,布布扣nbubuko.com,布布扣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

 水题

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <string>
 5 using namespace std;
 6 int T, n, ans;
 7 string s[105];
 8 int main(){
 9     scanf("%d", &T);
10     while(T--){
11         ans = 0;
12         string t;int temp;
13         scanf("%d", &n);
14         for(int i = 1; i <= n; i++){
15             cin>>s[i];
16             if(s[i][0] == S){
17                 cin>>t;cin>>temp;
18                 s[i] = s[temp];
19             }
20 
21         }
22         for(int i = 1; i <= n; i++){
23             if(s[i][0] == R) ans++;
24             else ans--; 
25         }
26         printf("%d\n", ans); 
27     } 
28     
29     return 0;
30 }

 

UVA 12503 Robot Instructions (B),布布扣,bubuko.com

UVA 12503 Robot Instructions (B)

标签:style   blog   http   color   os   io   strong   for   

原文地址:http://www.cnblogs.com/titicia/p/3917235.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!