与参考点距离很近

我想计算二维网格中参考点的距离。 揭露:

  • 0,0:不可穿透
  • 0,1:不可穿透
  • 1,1:可穿透

在这里输入图像描述

我search一个algorithm谁返回:

  • 1,1:1
  • 2,1:1
  • 3,1:1
  • 1,2:1
  • 2,2:0
  • 3,2:1
  • 1,6:4
  • 3,6:4

2, 2参考点为例, 4为例格中的最大距离。 什么algorithm可以这(具有良好的性能)?

一个可能的algorithm,用python语言表示:

 def get_distances_for_points(object_position, max_distance): points_distances = {} points_to_looks = [object_position] for current_distance in range(max_distance): new_points_to_looks = [] for looked_point in points_to_looks: around_points = <here return around points of looked_point> for around_point in around_points: if around_point not in points_distances and <here return True if around point is penetrable and False if blocked (wall)>: points_distances[around_point] = current_distance+1 new_points_to_looks.append(around_point) points_to_looks = new_points_to_looks return points_distances 

根据Ali.S和amitp的评论: https ://en.wikipedia.org/wiki/Breadth-first_search( demo )