Search Here

Tuesday, June 9, 2015

UVa - 12503 ( Robot Instructions Solutions )

Try Yourself First. For help Scroll Down .

Tip : Its a very easy Ad Hoc problem . Just think about the work that you have to. Take an array and then store every command as ' +1 ' for Left &'  -1 ' for Right and for Same as Step you just add the array value and you will get your answer . Just be careful about taking the inputs . If you don't get it yet then go down .


Code :

#include<bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int num;
        scanf("%d",&num);int j=1, pos=0;
        int com[105]={0};
        char s[10], b[5]; int n;
        for(int i=1; i<num+1; i++)
        {
            scanf("%s",s);
            if(s[0]=='L')
            {
                com[j++] = -1;
                pos = pos - 1;
            }
            else if(s[0]=='R')
            {
                com[j++] = 1;
                pos = pos + 1;
            }
            else
            {
                scanf("%s %d", b, &n);
                com[j++] = com[n];
                pos = pos + com[n];
            }
        }
        printf("%d\n",pos);
    }
    return 0;
}

=> Need Help . Leave a Comment .

1 comment: