Algorithm 确定二维栅格上的所有相邻空间
我似乎找不到一个好办法来解决我正在研究的这个问题 假设我在2D网格上有一个点,描述角色的位置(棋盘游戏): 现在,在每一回合中,我都可以根据骰子的滚动(1,2,3…)移动角色,我想找到角色移动的所有可能方式 移动字符一次意味着仅更改Algorithm 确定二维栅格上的所有相邻空间,algorithm,python-2.7,adjacency-list,Algorithm,Python 2.7,Adjacency List,我似乎找不到一个好办法来解决我正在研究的这个问题 假设我在2D网格上有一个点,描述角色的位置(棋盘游戏): 现在,在每一回合中,我都可以根据骰子的滚动(1,2,3…)移动角色,我想找到角色移动的所有可能方式 移动字符一次意味着仅更改n或m,其中对角线移动计为两个移动(即[n+1,m]移动一次,[n+1,m+1]移动两次) 例如,如果我掷2,则所有可能的移动都是: [n,m+2] [n
n
或m
,其中对角线移动计为两个移动(即[n+1,m]移动一次,[n+1,m+1]移动两次)
例如,如果我掷2,则所有可能的移动都是:
[n,m+2]
[n-1,m+1][n,m+1][n+1,m+1]
[n-2,m][n-1,m][n,m][n+1,m][n+2,m]
[n-1,m-1][n,m-1][n+1,m-1]
[n,m-2]
我似乎找不到一个明确的算法
到目前为止,我得到的是来自
position=[3,2]#位置
n=2#允许的移动次数
对于范围内的i(位置[0]-n,位置[0]+n):
对于范围内的j(位置[1]-n,位置[1]+n):
如果abs(位置[0]-i)+abs(位置[1]-j)穿过(倾斜的)菱形线并转换为法向坐标。Python代码
n = 2 #number of moves allowed
for i in range(0, n+1):
for j in range(0,n+1):
print i + j - n, i - j,'\n'
替代方法:您可以在n
范围内沿第一个坐标双向移动。如果某些移动向左移动(movesleft
),则可以使用movesleft
步骤沿第二个坐标向任何方向移动。第二阶段必须保留奇偶性(奇偶性?),因此使用第2步
伪码
n_moves = 2
for dy = -n_moves to n_moves do
movesleft = n_moves - Abs(dy)
for dx = - movesleft to movesleft] step 2 do
make_turn(pos_x + dx, pos_y + dy)
n=2和n=3的Delphi代码给出了结果
var
n, ml, dx, dy: Integer;
begin
n := 3;
for dy := - n to n do begin
ml := n - Abs(dy);
dx := - ml;
repeat
Memo1.Lines.Add(Format('[%d, %d]', [dy, dx]));
dx := dx + 2;
until dx > ml;
end;
[-2, 0]
[-1, -1]
[-1, 1]
[0, -2]
[0, 0]
[0, 2]
[1, -1]
[1, 1]
[2, 0]
[-3, 0]
[-2, -1]
[-2, 1]
[-1, -2]
[-1, 0]
[-1, 2]
[0, -3]
[0, -1]
[0, 1]
[0, 3]
[1, -2]
[1, 0]
[1, 2]
[2, -1]
[2, 1]
[3, 0]
第1种方法(另一顺序中的相同结果):
假设骰子显示p。
现在你想知道从你现在的位置有多少个点有曼哈顿距离p。
现在你能做的就是这么做
伪代码:
for i=0..p
if( x+i or x-i is valid and y+p-i or y-p+i is valid )
then move in (x+i,y+p-i) or/and (x-i,y-p+i) or/and (x-i,y+p-i) or/and (x-i,y+-i)
使用Python 2.7:
import math
# Get coordinates of possible dislocations
def get_possible_dislocations(initial_x, initial_y, max_steps):
possible_dislocations = []
for x in range(-max_steps,max_steps+1):
for y in range(-max_steps,max_steps+1):
if math.pow(x, 2) + math.pow(y, 2) <= math.pow(max_steps, 2):
possible_dislocations += [[initial_x + x, initial_y + y]]
return possible_dislocations
print get_possible_dislocations(0,0,2)
# Returns [[-2, 0], [-1, -1], [-1, 0], [-1, 1], [0, -2], [0, -1], [0, 0], [0, 1], [0, 2], [1, -1], [1, 0], [1, 1], [2, 0]]
导入数学
#获取可能的位错的坐标
def获得可能的位错(初始x、初始y、最大步数):
可能的位错=[]
对于范围内的x(-max_步数,max_步数+1):
对于范围内的y(-max_步数,max_步数+1):
如果math.pow(x,2)+math.pow(y,2)仍然缺失elements@MichaelG代码给出了(n+1)^2个位置(9,16…),这是正确的数量。n=2缺少什么元素?@Michael G我添加了更简单的元素approach@Michael.:你也可以尝试我的方法。它不会给你漏分。你可以尝试一次。只需在两个范围的末尾加上+1。Python中的范围不包含结尾。@samgak非常感谢!
for i := 0 to n do
for j := 0 to n do begin
dx := i + j - n;
dy := i - j;
Memo1.Lines.Add(Format('[%d, %d]', [dy, dx]));
end;
[0, -2]
[-1, -1]
[-2, 0]
[1, -1]
[0, 0]
[-1, 1]
[2, 0]
[1, 1]
[0, 2]
for i=0..p
if( x+i or x-i is valid and y+p-i or y-p+i is valid )
then move in (x+i,y+p-i) or/and (x-i,y-p+i) or/and (x-i,y+p-i) or/and (x-i,y+-i)
import math
# Get coordinates of possible dislocations
def get_possible_dislocations(initial_x, initial_y, max_steps):
possible_dislocations = []
for x in range(-max_steps,max_steps+1):
for y in range(-max_steps,max_steps+1):
if math.pow(x, 2) + math.pow(y, 2) <= math.pow(max_steps, 2):
possible_dislocations += [[initial_x + x, initial_y + y]]
return possible_dislocations
print get_possible_dislocations(0,0,2)
# Returns [[-2, 0], [-1, -1], [-1, 0], [-1, 1], [0, -2], [0, -1], [0, 0], [0, 1], [0, 2], [1, -1], [1, 0], [1, 1], [2, 0]]
import math
import pygame, sys
from pygame.locals import *
# Get coordinates of possible dislocations
def get_possible_dislocations(initial_x, initial_y, max_steps):
possible_dislocations = []
for x in range(-max_steps,max_steps+1):
for y in range(-max_steps,max_steps+1):
if math.pow(x, 2) + math.pow(y, 2) <= math.pow(max_steps, 2):
possible_dislocations += [[initial_x + x, initial_y + y]]
return possible_dislocations
# Initialize Pygame
pygame.init()
crashed = False
display_width = 500
display_height = 500
clock = pygame.time.Clock()
display=pygame.display.set_mode((display_width,display_height),0,32)
white=(255,255,255)
blue=(0,0,255)
display.fill(white)
center_w = display_width/2
center_h = display_height/2
# Calculate possible dislocations
possible_dislocations = get_possible_dislocations(center_w, center_h, 50)
# Draw pixels on every possible dislocation, starting from the center of the display
for loc in possible_dislocations:
pygame.draw.rect(display,blue,(loc[0],loc[1],2,2))
# Run the display
while not crashed:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
clock.tick(60)