24h購物| | PChome| 登入
2013-05-28 23:06:47| 人氣3,417| 回應0 | 上一篇 | 下一篇

[UVA] 12503 - Robot Instructions

推薦 0 收藏 0 轉貼0 訂閱站台


  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 (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

#include <stdio.h>

int main() {
    scanf("%*d");
    char cmd[30];
    int n, I[105], i, j;
    while(scanf("%d ", &n) == 1) {
        int x = 0;
        for(i = 0; i < n; i++) {
            gets(cmd);
            if(cmd[0] == 'L')
                I[i] = -1, x--;
            else if(cmd[0] == 'R')
                I[i] = 1, x++;
            else {
                sscanf(cmd+8, "%d", &j);
                I[i] = I[j-1], x += I[i];
            }
        }
        printf("%d\n", x);
    }
    return 0;
}

台長: Morris
人氣(3,417) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVa] 10019 - Funny Encryption Method
此分類上一篇:[UVA][dp] 10688 - The Poor Giant

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文