[Blue Bridge Cup] Langton ant

Posted by briant on Sun, 27 Feb 2022 05:23:03 +0100

Langton ant, proposed by Chris Langton in 1986, belongs to a kind of cellular automata.

The square grid on the plane is filled with black or white. There is an "ant" in one of the squares.
The direction of the ant's head is: up, down, left and right.



The movement rules of ants are very simple:
If the ant is in a black grid, turn 90 degrees to the right, change the grid to a white grid, and move forward one grid;
If the ant is in a white grid, turn left 90 degrees, change the grid to a black grid, and move forward one grid.

Although the rules are simple, the behavior of ants is very complex. The routes left at the beginning will be nearly symmetrical, like repetition, but no matter what the initial state is, ants will open up a regular "highway" after a long chaotic activity.

The route of ants is difficult to predict in advance.

Your task is to use the computer to simulate the position of Langton ant after the nth walk according to the initial state.

data format

The first row of input data is two integers m and n (3 < m, n < 100), representing the number of rows and columns of the square grid.
Next is m rows of data.
Each row of data is n numbers separated by spaces. 0 indicates white space and 1 indicates black space.

Next is a row of data: x y s k, where x y is an integer, indicating the row number and column number of the ant (the row number increases from top to bottom, and the column number increases from left to right, both starting from 0). S is a capital letter, indicating the orientation of the ant's head. We agree that: up, down, left and right are represented by: UDLR. k is the number of steps taken by the ant.

The output data is an integer p q separated by two spaces, which represents the row number and column number of the grid where the ant is located after step k.

For example, enter:
  5 6
  0 0 0 0 0 0
  0 0 0 0 0 0
  0 0 1 0 0 0
  0 0 0 0 0 0
  0 0 0 0 0 0
  2 3 L 5
The program should output:
  1 3

For another example, enter:
  3 3
  0 0 0
  1 1 1
  1 1 1
  1 1 U 6
The program should output:
  0 0

1, Step by step analysis

1. Input code

m,n = map(int,input().split()) #map stores m and n in the form of int
arr = [0] *m  # Prepare the array for each row
for i in range(m):
    arr[i] = [int(i) for i in input().split()] #Fill a row of numbers into the arr to form a rectangle
x,y,s,k = input().split()  #Input, type str
x,y,k = int(x),int(y),int(k)  #Convert to int
step = 0  #Set the initial steps to 0

2. Use a function to convert URDL to 1234, that is, the next code + 1 is a right turn and - 1 is a left turn

def head(s):
    if s == 'U':
        return 1
    if s == 'R':
        return 2
    if s == 'D':
        return 3
    if s == 'L':
        return 4
    return 0

3. Turn to change color and move forward, and output the coordinates after k steps after the end of the cycle

s = head(s)  #Gets the direction of the initial number
while step < k:  
    if arr[x][y] == 1:  #If it's Haig
        s += 1   #Plus 1 right turn
        if s == 5:  #When 4 is a right turn, it should become 1
            s = 1
        arr[x][y] = 0  #The grid turns white after turning
    else:
        if s == 0:  
            s = 4   #The limit cannot be 0. When it is 0, it turns for a week and then returns to 4
        s -= 1  #White grid left turn-1
        arr[x][y] = 1  #The lattice turns black
    if s == 1:  #One step forward after turning to change color. The forward X and Y depend on the direction of s
        x -= 1
    elif s == 2:
        y += 1
    elif s == 3:
        x += 1
    else:
        y -= 1
    step += 1
print(x,y)

2, Correct code

1. Initial version (written by myself at the beginning)

m,n = map(int,input().split())
arr = [0] *m 
for i in range(m):
    arr[i] = [int(i) for i in input().split()]
x,y,s,k = input().split()
x,y,k = int(x),int(y),int(k)
step = 0
def headDirection(s,color): #Judge the direction of head rotation
    if s == 'U'and color == 0 or s == 'D' and color == 1:  
        s = 'L'
    elif s == 'L'and color == 0 or color == 1 and s == 'R':
        s = 'D'
    elif s == 'D'and color == 0 or color == 1 and s == 'U':
        s = 'R'
    else:
        s = 'U'
    return s
def nextStep(s,x,y): #One step forward after judging the direction
    if s == 'U':
        return x - 1,y
    elif s == 'L':
        return x,y - 1
    elif s == 'R':
        return x,y + 1
    else:
        return x + 1,y
        
while step < k: 
    s = headDirection(s,arr[x][y])  #Call the function of turning the rear head direction
    if arr[x][y] == 0: #Change to black if white
        arr[x][y] = 1
    else:
        arr[x][y] = 0
    step += 1  #Steps plus 1
    x,y = nextStep(s,x,y) # Call the function of the next step
print(x,y)

2. Upgraded version (code analyzed above)

m,n = map(int,input().split())
arr = [0] *m 
for i in range(m):
    arr[i] = [int(i) for i in input().split()]
x,y,s,k = input().split()
x,y,k = int(x),int(y),int(k)
step = 0
def head(s):
    if s == 'U':
        return 1
    if s == 'R':
        return 2
    if s == 'D':
        return 3
    if s == 'L':
        return 4
    return 0
s = head(s)
while step < k:
    if arr[x][y] == 1:
        s += 1
        if s == 5:
            s = 1
        arr[x][y] = 0
    else:
        if s == 0:
            s = 4
        s -= 1
        arr[x][y] = 1
    if s == 1:
        x -= 1
    elif s == 2:
        y += 1
    elif s == 3:
        x += 1
    else:
        y -= 1
    step += 1
print(x,y)
    

Topics: Python