I am trying to have 2 points visit all points in a 3 x 3 grid. The method of visiting the points is:
- I have points P1 and P2, starting from
(0, 0). - P1 moves from
(0, 0)to(0, 1),(1, 0)until(3, 3). - When P1 reached the end of grid (
(3, 3)), P2 should update from(0, 0)to(0, 1), then - P1 starts from position of P2 until end of grid.
- This goes on until P2 reaches
(3, 3).
So an exhaustive/brute-force search with a combination of 2 points. I tried:
def traversal_methods(num_particles, grid_size):
particles = [(0, 0) for _ in range(num_particles)]
while(particles[1] != (grid_size - 1 , grid_size - 1 )):
for k in range(0, grid_size):
for l in range(0, grid_size):
particles[1] = (k, l)
particles[0] = (0, 0)
while(particles[0] != (grid_size-1, grid_size-1)):
for i in range(k, grid_size):
for j in range(0, grid_size):
particles[0] = (i, j)
I am not getting intended result. I also want to extend this to n-points. Can anyone suggest some method to do this?
Using list comprehension and nested loops:
Result (tuples as
(x, y)in list of lists as[y][x]):Traversal (P2 from P1):
Result as
(x, y):Result illustrated:
Example without grid: