在Python中创建一个螺旋数组? [英] creating a spiral array in python?
本文介绍了在Python中创建一个螺旋数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我和我的同伴试图用python创建一个有趣的游戏,其中以螺旋方式访问数组中输入的元素。我尝试了以下几种方法(
解决方案
您可以通过在矩阵中心附近开始并始终向右转(除非已经访问过该元素)来构建螺旋:
#!/ usr / bin / env python
NORTH,S,W,E =(0,-1),(0,1),( -1,0),(1,0)#方向
turn_right = {NORTH:E,E:S,S:W,W:NORTH}#old->新方向
def螺旋(宽度,高度):
如果width< 1或高度< 1:
引发ValueError
x,y =宽度// 2,高度// 2#在中心附近开始
dx,dy = NORTH#初始方向
矩阵= [[无] * _的宽度(范围(高度))
计数= 0
而True:
计数+ = 1
矩阵[y] [x] =计数#访问
#尝试右移
new_dx,new_dy = turn_right [dx,dy]
new_x,new_y = x + new_dx,y + new_dy
if(0< = new_x< width并且0< = new_y<高度和
matrix [new_y] [new_x]为None):#可以右移
x,y = new_x,new_y
dx,dy = new_dx,new_dy
else:#尝试直线移动
x,y = x + dx,y + dy
如果不是(0< = x<宽度和0< = y<高度) :
返回矩阵#无处可去
def print_matrix(matrix):
width = len(str(max(el for el in array in the row in el in row in if el not无)))
fmt = {:0%dd}矩阵中行的%宽度
:
print( .join( _ *宽度,如果el为None,则为el in fmt.format(el)) )
示例:
>>> print_matrix(spiral(5,5))
21 22 23 24 25
20 07 08 09 10
19 06 01 02 11
18 05 04 03 12
17 16 15 14 13
Me and my mate were trying to create a fun game in python where the elements entered in the array are accessed in a spiral manner. I have tried few methods like one given below (source).
def spiral(X, Y):
x = y = 0
dx = 0
dy = -1
for i in range(max(X, Y)**2):
if (-X/2 < x <= X/2) and (-Y/2 < y <= Y/2):
print (x, y)
# DO STUFF...
if x == y or (x < 0 and x == -y) or (x > 0 and x == 1-y):
dx, dy = -dy, dx
x, y = x+dx, y+dy
The above statement accesses the elements in spiral loop and prints them for a defined array AE. I would like to know how can I transform a given array AE to a spiral one
解决方案
You can build a spiral by starting near the center of the matrix and always turning right unless the element has been visited already:
#!/usr/bin/env python
NORTH, S, W, E = (0, -1), (0, 1), (-1, 0), (1, 0) # directions
turn_right = {NORTH: E, E: S, S: W, W: NORTH} # old -> new direction
def spiral(width, height):
if width < 1 or height < 1:
raise ValueError
x, y = width // 2, height // 2 # start near the center
dx, dy = NORTH # initial direction
matrix = [[None] * width for _ in range(height)]
count = 0
while True:
count += 1
matrix[y][x] = count # visit
# try to turn right
new_dx, new_dy = turn_right[dx,dy]
new_x, new_y = x + new_dx, y + new_dy
if (0 <= new_x < width and 0 <= new_y < height and
matrix[new_y][new_x] is None): # can turn right
x, y = new_x, new_y
dx, dy = new_dx, new_dy
else: # try to move straight
x, y = x + dx, y + dy
if not (0 <= x < width and 0 <= y < height):
return matrix # nowhere to go
def print_matrix(matrix):
width = len(str(max(el for row in matrix for el in row if el is not None)))
fmt = "{:0%dd}" % width
for row in matrix:
print(" ".join("_"*width if el is None else fmt.format(el) for el in row))
Example:
>>> print_matrix(spiral(5, 5))
21 22 23 24 25
20 07 08 09 10
19 06 01 02 11
18 05 04 03 12
17 16 15 14 13
这篇关于在Python中创建一个螺旋数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文