Description:

ZOJ4115 Wandering Robot (模拟思维)_i++

Input

ZOJ4115 Wandering Robot (模拟思维)_字符串_02

output:

ZOJ4115 Wandering Robot (模拟思维)_#include_03

Sample Input

2 3 3 RUL 1 1000000000 D

Sample Output

4 1000000000

Hint

For the first sample test case, the final instruction sequence is "RULRULRUL" and the route of the robot is (0, 0) - (1, 0) - (1, 1) - (0, 1) - (1, 1) - (1, 2) - (0, 2) - (1, 2) - (1, 3) - (0, 3). It's obvious that the farthest point on the route is (1, 3) and the answer is 4.

题意:

一个机器人有四种走的方式,给定一个字符串,求k次循环后机器人距离原点的最大距离,因为是循环所以只要求出第一次的最远距离,和最后一次的最远距离就行了。

ac代码:

#include<iostream>
#include<math.h>
#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long ll;
char s[100010];
int main()
{
    int i,j,l;
    int t;
    ll n,k;
    ll x,y,ans;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld %lld",&n,&k);
        scanf("%s",s);
        int len=strlen(s);
        x=0;
        y=0;
        ans=0;
        for(i=0;i<len;i++)
        {
            if(s[i]=='R')
                x++;
            else if(s[i]=='L')
                x--;
            else if(s[i]=='U')
                y++;
            else if(s[i]=='D')
                y--;
                if(ans<abs(x)+abs(y))
                {
                    ans=abs(x)+abs(y);
                }
        }
        x=x*(k-1);
        y=y*(k-1);
        if(ans<abs(x)+abs(y))
           {
             ans=abs(x)+abs(y);
           }
       for(i=0;i<len;i++)
        {
            if(s[i]=='R')
                x++;
            else if(s[i]=='L')
                x--;
            else if(s[i]=='U')
                y++;
            else if(s[i]=='D')
                y--;
                if(ans<abs(x)+abs(y))
                {
                    ans=abs(x)+abs(y);
                }
        }
        printf("%lld\n",ans);
    }
    return 0;
}