Blue Bridge Cup National Competition [Robot Walking] Python

 Topic Description Featured Project Courses_IT Popular Courses_Bluebridge Cloud Courses-Bluebridge Cloud Courses

( You can find this question in Lanqiao Yunke, or you can go to the practice system on the official website of Lanqiao Cup )

A children's palace has introduced a batch of robot cars. Can accept pre-entered commands and act on them. The basic movements of the trolley are very simple, there are only 3 types: turn left (denoted as L), turn right (denoted as R), and move forward a few centimeters (record the number directly).

For example, we can enter the following command for the car:

15L10R5LRR10R20

Then, the car will go straight for 15 cm first, turn left, then go 10 cm, then turn right,

It is not difficult to see that for this command string, the car has returned to the starting point.

Your task is: write a program, the user inputs instructions, and the program outputs the straight-line distance between the position of the trolley after each instruction is executed and the position of the trolley before the execution of the instruction.

enter description

The user first enters an integer n (n<100) n (n<100), indicating that there will be n instructions next.

Next enter n commands. Each instruction consists only of L, R and a number (the number is an integer between 0 and 100)

The length of each instruction does not exceed 256 characters.

output description

The program outputs nn lines of results, each of which represents the straight-line distance between the position before and after the trolley executes the corresponding command. Rounding to 2 decimal places is required.

Input and output example

Problem analysis: First of all, as a final question, you need to calm down and analyze it well.

The meaning of the title is, given n instructions, output and execute adjacent instructions, the distance between two points

For example, the first instruction is p, the second instruction is q, the third...

Then what we want to output in the first line is the distance between the obtained point and the origin after the first command

Then what we want to output in the second line is the distance between the point obtained and the point obtained in the previous round after executing the first instruction and then executing the second instruction...

Therefore, as long as we know the coordinates after i instructions and the coordinates after i-1 instructions, we can get the straight-line distance between the position of the car after each command is executed and the position of the car before the command is executed as mentioned in the title.

So how to find the coordinates of the point after i commands? Obviously, if we combine the 1st instruction, the 2nd instruction .. until the i-th instruction, that is, let the origin execute so many times (i times) of instructions at one time, we can get the coordinates.

So how to merge? Since the title gives the content of the i-th instruction, we need to use the prefix sum .

z[i] represents all operations from the 1st to the ith

The next step is to obtain the coordinates of the point after i operations according to z[i]

If you get the coordinates of each point, store them in the array c[i]

The distance between adjacent points is what is required, which can be output in sequence.

The following problem is reduced to, how to get the coordinates of the point after i operations according to z[i]

The following l, r, u, and d represent respectively (left left, right right, up up, down down)

Used for different orientations, the orientation after l, r, u, d is uncertain

We might as well replace l, r, u, d with numbers, and use 0, 1, 2, 3 respectively

Array direction[i][j]=[#four numbers]: Represents the new direction of the turn with i as the direction and passing through j

The dir marks the current direction, which is related to the direction. When 'L' or 'R' is encountered, update dir

When encountering a number, store it in the distance array and append it in

distance[i]: represents the process of operation in the i direction

In detail: initialize distance=[[],[],[],[]], for example, my current orientation is 1 (right), and if I encounter a number, then append the number in the array of distance[1]

At the end, we get the total distance of this point changing in four directions (summing distance[i], the summation result is the distance increasing in the i direction)

Finally, dx=(sum(distance[#right]-distance[#left])) is obtained according to the distance formula

dy is the same

Store in c[i], and then operate c[i] as described above.

n=int(input())
s=[]
z=[0]*n
def trans(x):#分割字符串L100R50R10变成['L','100','R'....]'
    t=[]
    s=''
    for i in x:
        if i.isalpha():
            if s:t.append(s)
            t.append(i)
            s=''
        else:
            s+=i
    if s:t.append(s)
    return t

for i in range(n):
    s.append(trans(input()))

z[0]=s[0]#前缀和(合并)
for i in range(1,n):
    z[i]=z[i-1]+s[i]

cns=[[0,0]]
#cns存每次命令过后的坐标,cns[i]代表经过i次命令后的坐标

def cal(j):
    global cns#开始方向
    distance=[[],[],[],[]]#左右,上下
    #l,r,u,d对应数字为0,1,2,3
    toward=[[3,2,0,0],[2,3,1,1],[0,1,2,2],[1,0,3,3]]
    dir=2
    for i in j:
        if i.isdigit():
            distance[dir].append(int(i))
        else:
            if i=='L':
                dir=toward[dir][0]
            else:
                dir=toward[dir][1]
    dx=sum(distance[1])-sum(distance[0])#右为正,左为负
    dy=sum(distance[2])-sum(distance[3])#上为正,下为负
    cns.append([dx,dy])
            
for j in z:
    cal(j)#调用函数 存坐标
for i in range(1,len(cns)):
    px=(cns[i][0]-cns[i-1][0])**2
    py=(cns[i][1]-cns[i-1][1])**2
    print('%.2f'%(px+py)**0.5)#输出两点间距离
    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324199072&siteId=291194637