

二维坐标转换为一维索引的公式
在一个有 m
行、n
列的网格中,可以用公式将坐标 (x, y)
转换为一维索引 cur_pos
:cur_pos = x * n + y
x
是当前点的行号(第几行)。y
是当前点的列号(第几列)。n
是网格的列数。
逆向转换:一维索引到二维坐标
如果需要从一维索引恢复二维坐标 (x, y)
:
x = cur_pos // n
(整除获取行号)y = cur_pos % n
(取余获取列号)
Formula for Converting 2D Coordinates to a 1D Index
In a grid with m
rows and n
columns, the 2D coordinates (x, y)
can be converted to a 1D index cur_pos
using the following formula: cur_pos = x * n + y
x
is the row number.y
is the column number.n
is the number of columns in the grid.
Converting from a 1D Index Back to 2D Coordinates
To recover the 2D coordinates (x, y)
from a 1D index:
- x = cur_pos // n (integer division to get the row number)
