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;
}