Huawei OD machine test-Snake

 Question description

[Snake] Snake is a classic game. The snake's body is made up of several connected squares, and the body moves with the snake's head. When the snake's head touches the food, the snake's length will increase by one. When the snake's head collides with any square of the body or the boundary of the game board, the game ends.

Let us complete the simulation of the Snake game:
given an N M array ar, representing a layout composed of N M squares, Snake moves one square at a time. If ar[i][j]=='H', it means that this square can be the starting position of the greedy snake; if ar[i][j]=='F', it means that this square is food. If ar [i][j]=='E' means that the square is a space.
The initial length of the greedy snake is 1, and the initial movement direction is to the left. The input is a given series of snake movement operations, and the length of the snake after the operation is returned. If the game ends before the operation is completed, the length of the snake at the end of the game is returned.
The details of snake movement, food eating and collision handling are shown in the diagram below:
 

alt


Figure 1
 

alt


Figure II
 

alt


Figure 3
 

alt


Figure 4
 

alt


Figure 5
 

alt


Figure 6

Figure 1: An intermediate state of the snake's movement is intercepted. H represents the snake's head, F represents food, and the numbers are the numbers of each section of the snake's body. The snake is moving to the left. At this time, the snake's head and food are adjacent.

Figure 2: The snake head moves one space to the left, and the snake head overlaps with the food. Note that the food space becomes the new snake head at this time. The first segment of the body moves to the position of the snake head, and the second segment of the body moves to the position of the first segment of the body. By analogy, the fourth section is finally added to rise to the original position of the body of section 3.

Figure 3: The snake head continues to move one space to the left, and each section of the body moves according to the above rules. At this time, the snake head is adjacent to the border, but has not yet collided.

Figure 4: The snake head continues to move one square to the left. At this time, the snake head has exceeded the boundary, collides, and the game is over.

Figures 5 and 6 give an example of a collision between a snake's head and its body, with the snake moving upward. In Figure 5, the snake head is adjacent to the body of Section 7, but has not yet collided; in Figure 6, the snake head moves up one space. At this time, both the snake head and the body of Section 8 have moved to the original position of the body of Section 7. A collision occurs and the game ends.

Enter description

Enter the space-separated letters in the first line, which represent the movement operation of the snake. The letter values ​​are U, D, L, R, and G. Among them, U, D, L, and R represent that the greedy snake turns up, down, left, and right respectively. The snake does not move when turning. G represents the greedy snake. Move one space in the current direction. The use case guarantees the correct operation of the input.
The second line contains two numbers separated by spaces, designated as N and M, which are the row and column numbers of the array. Each of the remaining N lines is M letters separated by spaces. The letter values ​​are H, F and E. H represents the starting position of the snake, F represents food, and E represents that the square is empty. The use case is guaranteed to have one and only one H, while there will be multiple F and E.

Output description

Outputs a number that is the length of the snake.

Example 1

enter

DGG
33
FFF
FFH
EFE

output

1

Code

# coding:utf-8
# 贪吃蛇
# https://blog.nowcoder.net/n/42420d1a2d324c32838f7f23e4da45f3
import sys

try:
    while True:
        a1 = sys.stdin.readline().strip()
        a2 = []
        n, m = [int(x) for x in input('input N M:').split(' ')]
        for i in range(n):
            a2.append(input(f'input {i + 1} line:').split(' '))
        start = (0, 0)
        for i in range(n):
            for j in range(m):
                if a2[i][j] == 'H':
                    a2[i][j] == 'E'
                    start = (i, j)
        body = [start]
        direction = 'L'
        for c in a1:
            if c in 'UDLR':
                direction = c
            elif c == 'G':
                if direction == 'U':
                    next = (body[0][0] - 1, body[0][1])
                elif direction == 'D':
                    next = (body[0][0] + 1, body[0][1])
                elif direction == 'L':
                    next = (body[0][0], body[0][1] - 1)
                elif direction == 'R':
                    next = (body[0][0], body[0][1] + 1)
                if next[0] < 0 or next[1] < 0 or next[0] > n - 1 or next[1] > m - 1 or next in body[:-1]:
                    # print(len(body))
                    break
                if a2[next[0]][next[1]] == 'E':
                    body = [next] + body[:-1]
                elif a2[next[0]][next[1]] == 'F':
                    body = [next] + body[:]
        print(len(body))

except:
    pass

Guess you like

Origin blog.csdn.net/SD_JZZ/article/details/132666558