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

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

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)

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

  1. def traversal_methods(num_particles, grid_size):
  2. particles = [(0, 0) for _ in range(num_particles)]
  3. while(particles[1] != (grid_size - 1 , grid_size - 1 )):
  4. for k in range(0, grid_size):
  5. for l in range(0, grid_size):
  6. particles[1] = (k, l)
  7. particles[0] = (0, 0)
  8. while(particles[0] != (grid_size-1, grid_size-1)):
  9. for i in range(k, grid_size):
  10. for j in range(0, grid_size):
  11. 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:

  1. def traversal_methods(num_particles, grid_size):
  2. particles = [(0, 0) for _ in range(num_particles)]
  3. while(particles[1] != (grid_size - 1 , grid_size - 1 )):
  4. for k in range(0, grid_size):
  5. for l in range(0, grid_size):
  6. particles[1] = (k, l)
  7. particles[0] = (0, 0)
  8. while(particles[0] != (grid_size-1, grid_size-1)):
  9. for i in range(k, grid_size):
  10. for j in range(0, grid_size):
  11. 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

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

  1. def grid_create(size_x, size_y):
  2. return [[(x, y) for x in range(size_x)] for y in range(size_y)]
  3. grid = grid_create(3, 3)

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

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

遍历(从P1到P2):

  1. def grid_traverse(grid):
  2. c1, c2 = 0, 0
  3. for y1 in range(len(grid)):
  4. for x1 in range(len(grid[y1])):
  5. c1 += 1
  6. c2 = 0
  7. print(f'P1 {grid[y1][x1]}')
  8. for y2 in range(len(grid)):
  9. for x2 in range(len(grid[y2])):
  10. c2 += 1
  11. if (c2 < c1): continue
  12. print(f'\tP2 {grid[y2][x2]}')
  13. grid = grid_create(3, 3)
  14. grid_traverse(grid)

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

  1. P1 (0, 0)
  2. P2 (0, 0)
  3. P2 (1, 0)
  4. P2 (2, 0)
  5. P2 (0, 1)
  6. P2 (1, 1)
  7. P2 (2, 1)
  8. P2 (0, 2)
  9. P2 (1, 2)
  10. P2 (2, 2)
  11. P1 (1, 0)
  12. P2 (1, 0)
  13. P2 (2, 0)
  14. P2 (0, 1)
  15. P2 (1, 1)
  16. P2 (2, 1)
  17. P2 (0, 2)
  18. P2 (1, 2)
  19. P2 (2, 2)
  20. P1 (2, 0)
  21. P2 (2, 0)
  22. P2 (0, 1)
  23. P2 (1, 1)
  24. P2 (2, 1)
  25. P2 (0, 2)
  26. P2 (1, 2)
  27. P2 (2, 2)
  28. P1 (0, 1)
  29. P2 (0, 1)
  30. P2 (1, 1)
  31. P2 (2, 1)
  32. P2 (0, 2)
  33. P2 (1, 2)
  34. P2 (2, 2)
  35. P1 (1, 1)
  36. P2 (1, 1)
  37. P2 (2, 1)
  38. P2 (0, 2)
  39. P2 (1, 2)
  40. P2 (2, 2)
  41. P1 (2, 1)
  42. P2 (2, 1)
  43. P2 (0, 2)
  44. P2 (1, 2)
  45. P2 (2, 2)
  46. P1 (0, 2)
  47. P2 (0, 2)
  48. P2 (1, 2)
  49. P2 (2, 2)
  50. P1 (1, 2)
  51. P2 (1, 2)
  52. P2 (2, 2)
  53. P1 (2, 2)
  54. P2 (2, 2)

结果的示意图见下图:

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

没有网格的示例:

  1. def grid_traverse(size_x, size_y):
  2. c1, c2 = 0, 0
  3. for y1 in range(size_y):
  4. for x1 in range(size_x):
  5. c1 += 1
  6. c2 = 0
  7. print(f'P1 ({x1}, {y1})')
  8. for y2 in range(size_y):
  9. for x2 in range(size_x):
  10. c2 += 1
  11. if (c2 < c1): continue
  12. print(f'\tP2 ({x2}, {y2})')
  13. 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:

  1. def grid_create(size_x, size_y):
  2. return [[(x, y) for x in range(size_x)] for y in range(size_y)]
  3. grid = grid_create(3, 3)

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

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

Traversal (P2 from P1):

  1. def grid_traverse(grid):
  2. c1, c2 = 0, 0
  3. for y1 in range(len(grid)):
  4. for x1 in range(len(grid[y1])):
  5. c1 += 1
  6. c2 = 0
  7. print(f&#39;P1 {grid[y1][x1]}&#39;)
  8. for y2 in range(len(grid)):
  9. for x2 in range(len(grid[y2])):
  10. c2 += 1
  11. if (c2 &lt; c1): continue
  12. print(f&#39;\tP2 {grid[y2][x2]}&#39;)
  13. grid = grid_create(3, 3)
  14. grid_traverse(grid)

Result as (x, y):

  1. P1 (0, 0)
  2. P2 (0, 0)
  3. P2 (1, 0)
  4. P2 (2, 0)
  5. P2 (0, 1)
  6. P2 (1, 1)
  7. P2 (2, 1)
  8. P2 (0, 2)
  9. P2 (1, 2)
  10. P2 (2, 2)
  11. P1 (1, 0)
  12. P2 (1, 0)
  13. P2 (2, 0)
  14. P2 (0, 1)
  15. P2 (1, 1)
  16. P2 (2, 1)
  17. P2 (0, 2)
  18. P2 (1, 2)
  19. P2 (2, 2)
  20. P1 (2, 0)
  21. P2 (2, 0)
  22. P2 (0, 1)
  23. P2 (1, 1)
  24. P2 (2, 1)
  25. P2 (0, 2)
  26. P2 (1, 2)
  27. P2 (2, 2)
  28. P1 (0, 1)
  29. P2 (0, 1)
  30. P2 (1, 1)
  31. P2 (2, 1)
  32. P2 (0, 2)
  33. P2 (1, 2)
  34. P2 (2, 2)
  35. P1 (1, 1)
  36. P2 (1, 1)
  37. P2 (2, 1)
  38. P2 (0, 2)
  39. P2 (1, 2)
  40. P2 (2, 2)
  41. P1 (2, 1)
  42. P2 (2, 1)
  43. P2 (0, 2)
  44. P2 (1, 2)
  45. P2 (2, 2)
  46. P1 (0, 2)
  47. P2 (0, 2)
  48. P2 (1, 2)
  49. P2 (2, 2)
  50. P1 (1, 2)
  51. P2 (1, 2)
  52. P2 (2, 2)
  53. P1 (2, 2)
  54. P2 (2, 2)

Result illustrated:

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

Example without grid:

  1. def grid_traverse(size_x, size_y):
  2. c1, c2 = 0, 0
  3. for y1 in range(size_y):
  4. for x1 in range(size_x):
  5. c1 += 1
  6. c2 = 0
  7. print(f&#39;P1 ({x1}, {y1})&#39;)
  8. for y2 in range(size_y):
  9. for x2 in range(size_x):
  10. c2 += 1
  11. if (c2 &lt; c1): continue
  12. print(f&#39;\tP2 ({x2}, {y2})&#39;)
  13. 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:

确定