• Robot Instructions


    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3947

    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
    

     


    AC代码:

     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    int now;
    
    char inst[110][7];
    int b[110];
    
    void s(int i)
    {
        switch(inst[i][0])
        {
        case 'L':
            now--;break;
        case 'R':
            now++;break;
        case 'S':
            s(b[i]);break;
        }
    }
    
    int main()
    {
        int t,n,i;
        char x[5];
        scanf("%d",&t);
        while(t--)
        {
            memset(b,0,sizeof(b));
            now = 0;
            scanf("%d",&n);
            for(i = 1; i <= n; i++)
            {
                scanf("%s",&inst[i]);
                if(inst[i][0] == 'S')
                {
                    scanf("%s",&x);
                    scanf("%d",&b[i]);
                }
                s(i);
            }
            printf("%d
    ",now);
        }
    
        return 0;
    }
    



  • 相关阅读:
    教你如何剖析源码
    Java 简介
    java 入门-helloWorld
    linux yum 命令
    Linux vi/vim
    Linux 磁盘管理
    Linux 用户和用户组管理
    Linux 文件与目录管理
    Linux 文件基本属性
    Linux安装Mysql
  • 原文地址:https://www.cnblogs.com/pangblog/p/3246616.html
Copyright © 2020-2023  润新知