如何访问二维网格中的所有点?

huangapple go评论95阅读模式
英文:

How to visit all points in a 2D grid?

问题

我正在尝试让两个点访问3x3网格中的所有点。访问点的方法是:

  • 我有点P1和P2,从(0, 0)开始。
  • P1从(0, 0)移动到(0, 1)(1, 0)直到(3, 3)
  • 当P1到达网格的末端((3, 3))时,P2应该从(0, 0)更新到(0, 1),然后
  • P1从P2的位置开始,直到网格的末端。
  • 这个过程一直持续,直到P2达到(3, 3)

因此,这是一个用两个点的组合进行的穷举/暴力搜索。我尝试了以下方法:

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)

我没有得到预期的结果。我还想将这个扩展到n个点。有人可以建议一些方法吗?

英文:

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?

答案1

得分: 1

使用列表理解和嵌套循环可以实现这个目标:

def grid_create(size_x, size_y):
    return [[(x, y) for x in range(size_x)] for y in range(size_y)]

grid = grid_create(3, 3)

结果(元组作为(x, y)的列表,以[y][x]的形式存储):

[(0, 0), (1, 0), (2, 0)],
[(0, 1), (1, 1), (2, 1)],
[(0, 2), (1, 2), (2, 2)]

遍历(从P1到P2):

def grid_traverse(grid):
    c1, c2 = 0, 0

    for y1 in range(len(grid)):
        for x1 in range(len(grid[y1])):
            c1 += 1
            c2 = 0
            print(f'P1 {grid[y1][x1]}')

            for y2 in range(len(grid)):
                for x2 in range(len(grid[y2])):
                    c2 += 1
                    if (c2 < c1): continue
                    print(f'\tP2 {grid[y2][x2]}')

grid = grid_create(3, 3)
grid_traverse(grid)

结果(以(x, y)表示):

P1 (0, 0)
    P2 (0, 0)
    P2 (1, 0)
    P2 (2, 0)
    P2 (0, 1)
    P2 (1, 1)
    P2 (2, 1)
    P2 (0, 2)
    P2 (1, 2)
    P2 (2, 2)
P1 (1, 0)
    P2 (1, 0)
    P2 (2, 0)
    P2 (0, 1)
    P2 (1, 1)
    P2 (2, 1)
    P2 (0, 2)
    P2 (1, 2)
    P2 (2, 2)
P1 (2, 0)
    P2 (2, 0)
    P2 (0, 1)
    P2 (1, 1)
    P2 (2, 1)
    P2 (0, 2)
    P2 (1, 2)
    P2 (2, 2)
P1 (0, 1)
    P2 (0, 1)
    P2 (1, 1)
    P2 (2, 1)
    P2 (0, 2)
    P2 (1, 2)
    P2 (2, 2)
P1 (1, 1)
    P2 (1, 1)
    P2 (2, 1)
    P2 (0, 2)
    P2 (1, 2)
    P2 (2, 2)
P1 (2, 1)
    P2 (2, 1)
    P2 (0, 2)
    P2 (1, 2)
    P2 (2, 2)
P1 (0, 2)
    P2 (0, 2)
    P2 (1, 2)
    P2 (2, 2)
P1 (1, 2)
    P2 (1, 2)
    P2 (2, 2)
P1 (2, 2)
    P2 (2, 2)

结果的示意图见下图:

如何访问二维网格中的所有点?

没有网格的示例:

def grid_traverse(size_x, size_y):
    c1, c2 = 0, 0

    for y1 in range(size_y):
        for x1 in range(size_x):
            c1 += 1
            c2 = 0
            print(f'P1 ({x1}, {y1})')

            for y2 in range(size_y):
                for x2 in range(size_x):
                    c2 += 1
                    if (c2 < c1): continue
                    print(f'\tP2 ({x2}, {y2})')

grid_traverse(3, 3)
英文:

> I also want to extend this to n-points. Can anyone suggest some method to do this?

Using list comprehension and nested loops:

def grid_create(size_x, size_y):
    return [[(x, y) for x in range(size_x)] for y in range(size_y)]


grid = grid_create(3, 3)

Result (tuples as (x, y) in list of lists as [y][x]):

[(0, 0), (1, 0), (2, 0)],
[(0, 1), (1, 1), (2, 1)],
[(0, 2), (1, 2), (2, 2)]

Traversal (P2 from P1):

def grid_traverse(grid):
    c1, c2 = 0, 0

    for y1 in range(len(grid)):
        for x1 in range(len(grid[y1])):
            c1 += 1
            c2  = 0
            print(f&#39;P1 {grid[y1][x1]}&#39;)

            for y2 in range(len(grid)):
                for x2 in range(len(grid[y2])):
                    c2 += 1
                    if (c2 &lt; c1): continue
                    print(f&#39;\tP2 {grid[y2][x2]}&#39;)


grid = grid_create(3, 3)
grid_traverse(grid)

Result as (x, y):

P1 (0, 0)
	P2 (0, 0)
	P2 (1, 0)
	P2 (2, 0)
	P2 (0, 1)
	P2 (1, 1)
	P2 (2, 1)
	P2 (0, 2)
	P2 (1, 2)
	P2 (2, 2)
P1 (1, 0)
	P2 (1, 0)
	P2 (2, 0)
	P2 (0, 1)
	P2 (1, 1)
	P2 (2, 1)
	P2 (0, 2)
	P2 (1, 2)
	P2 (2, 2)
P1 (2, 0)
	P2 (2, 0)
	P2 (0, 1)
	P2 (1, 1)
	P2 (2, 1)
	P2 (0, 2)
	P2 (1, 2)
	P2 (2, 2)
P1 (0, 1)
	P2 (0, 1)
	P2 (1, 1)
	P2 (2, 1)
	P2 (0, 2)
	P2 (1, 2)
	P2 (2, 2)
P1 (1, 1)
	P2 (1, 1)
	P2 (2, 1)
	P2 (0, 2)
	P2 (1, 2)
	P2 (2, 2)
P1 (2, 1)
	P2 (2, 1)
	P2 (0, 2)
	P2 (1, 2)
	P2 (2, 2)
P1 (0, 2)
	P2 (0, 2)
	P2 (1, 2)
	P2 (2, 2)
P1 (1, 2)
	P2 (1, 2)
	P2 (2, 2)
P1 (2, 2)
	P2 (2, 2)

Result illustrated:

如何访问二维网格中的所有点?

Example without grid:

def grid_traverse(size_x, size_y):
    c1, c2 = 0, 0

    for y1 in range(size_y):
        for x1 in range(size_x):
            c1 += 1
            c2  = 0
            print(f&#39;P1 ({x1}, {y1})&#39;)

            for y2 in range(size_y):
                for x2 in range(size_x):
                    c2 += 1
                    if (c2 &lt; c1): continue
                    print(f&#39;\tP2 ({x2}, {y2})&#39;)


grid_traverse(3, 3)

huangapple
  • 本文由 发表于 2023年8月10日 17:25:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76874362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定