Quantcast
Channel: Determine neighboring cells in a two-dimensional grid with periodic conditions - Stack Overflow
Viewing all articles
Browse latest Browse all 5

Answer by GolamMazid Sajib for Determine neighboring cells in a two-dimensional grid with periodic conditions

$
0
0

Try with this:

grid = [[20,  21,  22,  23,  24],[15,  16,  17,  18,  19],[10,  11,  12,  13,  14],[5,   6,   7,   8,   9],[0,   1,   2,   3,   4]]def celdas_vecinas(cell,n):    Row = [-1, -1, -1, 0, 0, 0, 1, 1, 1]    Col = [-1, 0, 1, -1, 0, 1, -1, 0, 1]    x = y = 0    for i in range(n):        z = 0;        for j in range(n):            if grid[i][j] == cell:                x = i                y = j                z = 1                break        if z:            break    ans = []    for i in range(9):        xx = (n + x + Row[i]) % n        yy = (n + y + Col[i]) % n        ans.append(grid[xx][yy])    return ans;print(celdas_vecinas(1,5))print(celdas_vecinas(21,5))print(celdas_vecinas(5,5))

Viewing all articles
Browse latest Browse all 5

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>