#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
#include<math.h>
#include<string.h>
int startX = 0;//默认起始坐标
int startY = 0;
int start = 0;
char Location[4] = { 'N','E','S','W' };//定义一个固定坐标,北东南西
void changelocation(char s)//改变方向
{
if (s == 'L')
{
if (start == 0)
start = 3;
else
start -= 1;
}
if (s == 'R')
{
if (start == 3)
start = 0;
else
start += 1;
}
}
void explain(char* s)//拆分指令
{
int l = 0;
int i = 0;
int len = strlen(s);
while (i < len)
{
if (isalpha(s[i]))//为数字直接改变步数位置
{
changelocation(s[i]);
i++;
}
else
{
l = i;
while (i < len && isdigit(s[i]))
i++;
int k = i - l;
int sum = 0;
for (int j = k-1; j >= 0; j--)
{
int m = s[l++] - '0';
sum += m * (int)pow(10, j);
}
//判段为那个方向,默认上和右为正,下和左为负
if (Location[start] == 'N')
startY += sum;
else if (Location[start] == 'S')
startY -= sum;
else if (Location[start] == 'E')
startX += sum;
else if (Location[start] == 'W')
startX -= sum;
}
}
}
int main(int argc, char* argv[])
{
// 请在此输入您的代码
int n;
char str[257];
scanf("%d", &n);
while (n--)
{
getchar();
scanf("%s", str);
explain(str);
double nums = sqrt(startX * startX + startY * startY);
printf("%.2lf\n", nums);
startX = 0;//接受新指令
startY = 0;
start = 0;
}
return 0;
}
机器人的行走
最新推荐文章于 2024-09-06 10:38:06 发布