一种方法是不使用数组,而是使用一组坐标来记录所有访问过的位置,计算步行的跨度,并在步行后将其映射到适当大小的网格:
该方案随机行走一个walker,对行走的范围没有限制,然后构建一个自动适应数据大小的数据结构;它使用有序序列和索引。
1- 在位置 (0, 0) 处启动您的 walker,一个元组,并将其插入集合中。
2-步行者:
2-1- 为步行者创建一个新的坐标元组,其新位置行 +/- 1 和/或列 +/-1 用于步骤的每个方向。
2-2- 通过在每一步将其插入到集合中来记录这个新位置。
3-继续走直到完成。
4- 从步行位置生成网格:
4-1- 找到最小和最大行和列来确定跨度。
4-3- 为行和列创建一个大小为 max-min 的网格。
4-4-通过将步行坐标更改为网格坐标来填充访问的位置。
如果您需要按顺序执行这些步骤,则可以使用有序集或地图。
如果可以多次访问位置,则可以改用序列(数组、列表...),或者如果您需要跟踪访问的顺序,则可以使用到时间步序列的映射。
实现(python)可能如下所示:
"""
a 2D grid that auto adjusts its size to the data.
coordinates are tuples of integers in the range -1e9 <-> 1e9
"""
class SpanGrid:
def __init__(self, coordinates):
self.coordinates = coordinates
self.grid = None
self.rows = None
self.columns = None
self.make_grid()
def make_grid(self):
self._find_num_rows_columns()
self._populate_grid()
def _find_num_rows_columns(self):
self.minrow, self.mincol = 1e9, 1e9
self.maxrow, self.maxcol = -1e9, -1e9
for row, col in self.coordinates:
self.minrow = row if row < self.minrow else self.minrow
self.mincol = col if col < self.mincol else self.mincol
self.maxrow = row if row > self.maxrow else self.maxrow
self.maxcol = col if col > self.maxcol else self.maxcol
self.rows = self.maxrow - self.minrow + 1
self.columns = self.maxcol - self.mincol + 1
def _populate_grid(self):
self.grid = [[None for dummycol in range(self.columns)]
for dummyrow in range(self.rows)]
for r, c in self.coordinates:
self.grid[r - self.minrow][c - self.mincol] = True
def __str__(self):
result = []
for line in self.grid:
res = ''
for pos in line:
res += str(pos) if pos else ' - '
res += ' '
result.append(res)
return '\n'.join(result)
if __name__ == '__main__':
import random
visited = set()
offsets = ((1, 0), (-1, 0), (0, 1), (0, -1))
start = (0, 0)
current = start
visited.add(current)
for _ in range(100):
cur_row, cur_col = current
row_offset, col_offset = random.choice(offsets)
current = (cur_row + row_offset, cur_col + col_offset)
visited.add(current)
grid = SpanGrid(visited)
print(grid)
示例输出:
- True True True True True True -
- True True True True True True True
- True True True True True True -
True True True True True True - -
True True True True True True - -
- True True True True - - -
- True - - - - - -
============================================================================
- - - - - - - - - True - -
- - - - - - - - True True True -
- - - - - True True - True True True True
True True True True True True True - True True True True
- True True True True True True True True True True -
- - - True True True True True - - - -
============================================================================
- - - - - - - True True True True - - - - - - - - -
- - - - - - - True - - True True True - - True True - - -
- - - - - - - True True - True True True - True True True - - -
- - - - - - - True True True True True True True True True - - - -
- True True - - - - - True - True True True - - True True - - -
True True True True - - - - True - - True True - - True True True - -
True True True True - - True True True - - - True - - - True True - -
- - - True True True True - - - - - - - - True True - - -
- - - - - - - - - - - - - - - - True True True True
- - - - - - - - - - - - - - - - - - - True
- - - - - - - - - - - - - - - - - - True True
- - - - - - - - - - - - - - - - - - - True