matrix_aux_for_circuit = [
[None, None, 'd', None, 'c'],
[None, "start", None, 200, 'b'],
[None, 'f', 'e', None, None],
]
starting_point_box = (1, 1) # In this case, it would be the cell at row 1 and column 1
circuit_list = []
The starting point is indicated by the tuple starting_point_box = (1, 1). Place the coordinates of this first point in circuit_list = [], in this case circuit_list = [(1, 1)]. From this point, attempt to begin with a horizontal movement (i.e., on the same row) to the left until encountering a cell that contains an element that is not None.
If no cell is found to the left with a non-None content, then attempt to find a cell to the right with non-None content, and if found, save the coordinates of the cell where it is found. If a cell to the left with non-None content is found, then save the coordinates of this cell in circuit_list = [], in this case circuit_list = [(1, 1), (1, 4)].
If neither to the left nor to the right on that row, any cell with a non-None element is found, then print "a circuit cannot be formed".
In this case, if a cell with non-None content is found to the left at the coordinates (1, 4), and now as the previous movement was horizontal, then the next movement will have to be vertical (they alternate), so first try upwards. In this case, if the cell (0, 4) has a non-None element, then the coordinates should be saved in circuit_list = [(1, 1), (1, 4), (0, 4)]. But if no cell with a non-None element is found upwards, then try to search downwards in the same column if there is any cell without a non-None element and save its coordinates. If neither upwards nor downwards on that column, any cell with a non-None element is found, then print "a circuit cannot be formed".
In this case, if a cell with non-None content is found upwards at the coordinates (0, 4), and now as the previous movement was vertical, then the next movement will have to be horizontal (they alternate). So, first try to the left of (0, 4), but in this case, there is no cell to the left that does not have None, so try to the right of (0, 4), and in this way, find that the cell at coordinates (0, 2) which has an element different from None, so save its coordinates in circuit_list = [(1, 1), (1, 4), (0, 4), (0, 2)].
In this case, if a cell with non-None content is found to the right at the coordinates (0, 2), and now as the previous movement was horizontal, then the next movement will have to be vertical (they alternate). So, first try upwards from (0, 2), but in this case, upwards there is no cell that does not have None, so try downwards from (0, 2), and in this way, find that the cell at coordinates (2, 2) which has an element different from None, so save its coordinates in circuit_list = [(1, 1), (1, 4), (0, 4), (0, 2), (2, 2)].
In this case, if a cell with non-None content is found downwards at the coordinates (2, 2), and now as the previous movement was vertical, then the next movement will have to be horizontal (they alternate). So, first try to the left of (2, 2), but in this case, there is no cell to the left that does not have None, so try to the right of (2, 2), and in this way, find that the cell at coordinates (2, 1) which has an element different from None, so save its coordinates in circuit_list = [(1, 1), (1, 4), (0, 4), (0, 2), (2, 2), (2, 1)].
And since the previous movement was horizontal, this movement will be vertical, so try going upwards from (2, 1), and as it managed to reach the starting point, the circuit must end and print the coordinates of each of the cells that make up the vertices of the circuit circuit_list = [(1, 1), (1, 4), (0, 4), (0, 2), (2, 2), (2, 1), (1, 1)]. If no cell with non-None content had been found upwards, it should have tried to find it downwards.
starting_point_box = (1, 1)
circuit_list = [starting_point_box] # Initialize the circuit list with the starting point
def find_next_vertex(matrix, current_point, last_move):
x, y = current_point
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)] # Right, Left, Down, Up
for dx, dy in directions:
next_x, next_y = x + dx, y + dy
To solve this problem, you can follow the following steps:
- Start at the specified starting point.
- Explore horizontally and vertically from each point to find the next unvisited vertex (It cannot go 2 times in a row in horizontal scrolling or 2 times in vertical scrolling, but must alternate).
- Add this vertex to the circuit's vertex list and continue exploring from this new vertex.
- Repeat the process until you have found at least three additional vertices and have returned to the starting point.
In this case, a possible correct output is circuit_list = [(1, 1), (1, 4), (0, 4), (0, 2), (2, 2), (2, 1), (1, 1)] , keep in mind that circuits can never take the same square as vertex twice except for the initial square.