Robot Instructions UVA - 12503

Robot Instructions UVA - 12503
题目传送门
:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3947
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.
题目大意:做操作,左减右加,重复的操作用same as 来代替;

水;

AC Code:以下代码看不懂set函数的,可以去参考:
http://blog.csdn.net/qq_36525906/article/details/72356136

#include <iostream>
#include <bits/stdc++.h>

using namespace std;
int sum = 0;
char s[100];
int i = 1;
set<int>m1,m2;
int same(int l)
{
    if(m1.find(l) != m1.end())
    {
        sum--;
        m1.insert(i);
    }
    else if(m2.find(l) != m2.end())
    {
        sum++;
        m2.insert(i);
    }
    return 0;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        memset(s,0,sizeof(s));
        int n;
        cin>>n;
        sum = 0;
        i=1;
        while(n--)
        {
            scanf("%s",s);
            if(strcmp(s,"LEFT") == 0)
            {
                sum--;
                m1.insert(i);
            }
            else if(strcmp(s,"RIGHT") == 0)
            {
                sum++;
                m2.insert(i);
            }
            else
            {
                int k;
                char ss[100];
                scanf("%s %d",ss,&k);
                same(k);
            }
            i++;
        }
        cout<<sum<<endl;
        m1.clear();
        m2.clear();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值