广度优先寻路算法

Posted iml6yu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了广度优先寻路算法相关的知识,希望对你有一定的参考价值。

文章目录

ps:写在前面,当时只是在一个纵横交错的库房内寻找到从A到B的路径,并没有最短最优的要求。

原始数据

这是一段json转存在C#代码中的结构,

数据属性解释:

  • Id:标识
  • PointName:点位名称
  • PointX:x坐标
  • PointY:y坐标
  • NeighborIdArr:数组,能够到达的邻居节点ID
  • IsWorlplacePoint:是否是工位节点(true表示工位节点,不能穿过,只能作为终点到达)

数据

    #region JsonString
            json = @"[
	""Id"": 1,
	""PointName"": ""A0001"",
	""PointX"": 0,
	""PointY"": 0,
	""NeighborIdArr"": [2, 6],
	""IsWorkplacePoint"": false
, 
	""Id"": 2,
	""PointName"": ""A0002"",
	""PointX"": 0,
	""PointY"": 200,
	""NeighborIdArr"": [1, 3, 7],
	""IsWorkplacePoint"": false
, 
	""Id"": 3,
	""PointName"": ""A0003"",
	""PointX"": 0,
	""PointY"": 700,
	""NeighborIdArr"": [2, 4, 8],
	""IsWorkplacePoint"": false
, 
	""Id"": 4,
	""PointName"": ""A0004"",
	""PointX"": 0,
	""PointY"": 900,
	""NeighborIdArr"": [3, 5, 9],
	""IsWorkplacePoint"": false
, 
	""Id"": 5,
	""PointName"": ""A0005"",
	""PointX"": 0,
	""PointY"": 1400,
	""NeighborIdArr"": [4, 10],
	""IsWorkplacePoint"": false
, 
	""Id"": 6,
	""PointName"": ""A0006"",
	""PointX"": 200,
	""PointY"": 0,
	""NeighborIdArr"": [1, 7, 11],
	""IsWorkplacePoint"": false
, 
	""Id"": 7,
	""PointName"": ""A0007"",
	""PointX"": 200,
	""PointY"": 200,
	""NeighborIdArr"": [2, 6, 12, 8],
	""IsWorkplacePoint"": false
, 
	""Id"": 8,
	""PointName"": ""A0008"",
	""PointX"": 200,
	""PointY"": 700,
	""NeighborIdArr"": [3, 7, 13, 9],
	""IsWorkplacePoint"": false
, 
	""Id"": 9,
	""PointName"": ""A0009"",
	""PointX"": 200,
	""PointY"": 900,
	""NeighborIdArr"": [4, 8, 14, 10],
	""IsWorkplacePoint"": false
, 
	""Id"": 10,
	""PointName"": ""A0010"",
	""PointX"": 200,
	""PointY"": 1400,
	""NeighborIdArr"": [5, 9, 15],
	""IsWorkplacePoint"": false
, 
	""Id"": 11,
	""PointName"": ""A0011"",
	""PointX"": 600,
	""PointY"": 0,
	""NeighborIdArr"": [6, 12, 16],
	""IsWorkplacePoint"": false
, 
	""Id"": 12,
	""PointName"": ""A0012"",
	""PointX"": 600,
	""PointY"": 200,
	""NeighborIdArr"": [7, 13, 17, 11],
	""IsWorkplacePoint"": false
, 
	""Id"": 13,
	""PointName"": ""A0013"",
	""PointX"": 600,
	""PointY"": 700,
	""NeighborIdArr"": [8, 14, 18, 12],
	""IsWorkplacePoint"": false
, 
	""Id"": 14,
	""PointName"": ""A0014"",
	""PointX"": 600,
	""PointY"": 900,
	""NeighborIdArr"": [9, 15, 19, 13],
	""IsWorkplacePoint"": false
, 
	""Id"": 15,
	""PointName"": ""A0015"",
	""PointX"": 600,
	""PointY"": 1400,
	""NeighborIdArr"": [10, 20, 14],
	""IsWorkplacePoint"": false
, 
	""Id"": 16,
	""PointName"": ""A0016"",
	""PointX"": 800,
	""PointY"": 0,
	""NeighborIdArr"": [11, 21, 17],
	""IsWorkplacePoint"": false
, 
	""Id"": 17,
	""PointName"": ""A0017"",
	""PointX"": 800,
	""PointY"": 200,
	""NeighborIdArr"": [12, 16, 22, 18],
	""IsWorkplacePoint"": false
, 
	""Id"": 18,
	""PointName"": ""A0018"",
	""PointX"": 800,
	""PointY"": 700,
	""NeighborIdArr"": [13, 17, 23, 19],
	""IsWorkplacePoint"": false
, 
	""Id"": 19,
	""PointName"": ""A0019"",
	""PointX"": 800,
	""PointY"": 900,
	""NeighborIdArr"": [14, 18, 24, 20],
	""IsWorkplacePoint"": false
, 
	""Id"": 20,
	""PointName"": ""A0020"",
	""PointX"": 800,
	""PointY"": 1400,
	""NeighborIdArr"": [15, 19, 25],
	""IsWorkplacePoint"": false
, 
	""Id"": 21,
	""PointName"": ""A0021"",
	""PointX"": 1200,
	""PointY"": 0,
	""NeighborIdArr"": [16, 26, 22],
	""IsWorkplacePoint"": false
, 
	""Id"": 22,
	""PointName"": ""A0022"",
	""PointX"": 1200,
	""PointY"": 200,
	""NeighborIdArr"": [17, 21, 27, 23],
	""IsWorkplacePoint"": false
, 
	""Id"": 23,
	""PointName"": ""A0023"",
	""PointX"": 1200,
	""PointY"": 700,
	""NeighborIdArr"": [18, 22, 28, 24],
	""IsWorkplacePoint"": false
, 
	""Id"": 24,
	""PointName"": ""A0024"",
	""PointX"": 1200,
	""PointY"": 900,
	""NeighborIdArr"": [19, 23, 29, 25],
	""IsWorkplacePoint"": false
, 
	""Id"": 25,
	""PointName"": ""A0025"",
	""PointX"": 1200,
	""PointY"": 1400,
	""NeighborIdArr"": [20, 24, 30],
	""IsWorkplacePoint"": false
, 
	""Id"": 26,
	""PointName"": ""A0026"",
	""PointX"": 1700,
	""PointY"": 0,
	""NeighborIdArr"": [21, 27],
	""IsWorkplacePoint"": false
, 
	""Id"": 27,
	""PointName"": ""A0027"",
	""PointX"": 1700,
	""PointY"": 200,
	""NeighborIdArr"": [22, 26, 28],
	""IsWorkplacePoint"": false
, 
	""Id"": 28,
	""PointName"": ""A0028"",
	""PointX"": 1700,
	""PointY"": 700,
	""NeighborIdArr"": [23, 27, 29],
	""IsWorkplacePoint"": false
, 
	""Id"": 29,
	""PointName"": ""A0029"",
	""PointX"": 1700,
	""PointY"": 900,
	""NeighborIdArr"": [24, 28, 30],
	""IsWorkplacePoint"": false
, 
	""Id"": 30,
	""PointName"": ""A0030"",
	""PointX"": 1700,
	""PointY"": 1400,
	""NeighborIdArr"": [25, 29],
	""IsWorkplacePoint"": false
, 
	""Id"": 31,
	""PointName"": ""P0001"",
	""PointX"": 100,
	""PointY"": 100,
	""NeighborIdArr"": [1, 2, 6, 7],
	""IsWorkplacePoint"": true
, 
	""Id"": 32,
	""PointName"": ""P0002"",
	""PointX"": 100,
	""PointY"": 450,
	""NeighborIdArr"": [2, 3, 7, 8],
	""IsWorkplacePoint"": true
, 
	""Id"": 33,
	""PointName"": ""P0003"",
	""PointX"": 100,
	""PointY"": 800,
	""NeighborIdArr"": [3, 4, 8, 9],
	""IsWorkplacePoint"": true
, 
	""Id"": 34,
	""PointName"": ""P0004"",
	""PointX"": 100,
	""PointY"": 1150,
	""NeighborIdArr"": [4, 5, 9, 10],
	""IsWorkplacePoint"": true
, 
	""Id"": 35,
	""PointName"": ""P0005"",
	""PointX"": 400,
	""PointY"": 100,
	""NeighborIdArr"": [6, 7, 11, 12],
	""IsWorkplacePoint"": true
, 
	""Id"": 36,
	""PointName"": ""P0006"",
	""PointX"": 400,
	""PointY"": 450,
	""NeighborIdArr"": [7, 8, 12, 13],
	""IsWorkplacePoint"": true
, 
	""Id"": 37,
	""PointName"": ""P0007"",
	""PointX"": 400,
	""PointY"": 800,
	""NeighborIdArr"": [8, 9, 13, 14],
	""IsWorkplacePoint"": true
, 
	""Id"": 38,
	""PointName"": ""P0008"",
	""PointX"": 400,
	""PointY"": 1150,
	""NeighborIdArr"": [9, 10, 14, 15],
	""IsWorkplacePoint"": true
, 
	""Id"": 39,
	""PointName"": ""P0009"",
	""PointX"": 700,
	""PointY"": 100,
	""NeighborIdArr"": [11, 12, 16, 17],
	""IsWorkplacePoint"": true
, 
	""Id"": 40,
	""PointName"": ""P0010"",
	""PointX"": 700,
	""PointY"": 450,
	""NeighborIdArr"": [12, 13, 17, 18],
	""IsWorkplacePoint"": true
, 
	""Id"": 41,
	""PointName"": ""P0011"",
	""PointX"": 700,
	""PointY"": 800,
	""NeighborIdArr"": [13, 14, 18, 19],
	""IsWorkplacePoint"": true
, 
	""Id"": 42,
	""PointName"": ""P0012"",
	""PointX"": 700,
	""PointY"": 1150,
	""NeighborIdArr"": [14, 15, 19, 20],
	""IsWorkplacePoint"": true
, 
	""Id"": 43,
	""PointName"": ""P0013"",
	""PointX"": 1000,
	""PointY"": 100,
	""NeighborIdArr"": [16, 17, 21, 22],
	""IsWorkplacePoint"": true
, 
	""Id"": 44,
	""PointName"": ""P0014"",
	""PointX"": 1000,
	""PointY"": 450,
	""NeighborIdArr"": [17, 18, 22, 23],
	""IsWorkplacePoint"": true
, 
	""Id"": 45,
	""PointName"": ""P0015"",
	""PointX"": 1000,
	""PointY"": 800,
	""NeighborIdArr"": [18, 19, 23, 24],
	""IsWorkplacePoint"": true
, 
	""Id"": 46,
	""PointName"": ""P0016"",
	""PointX"": 1000,
	""PointY"": 1150,
	""NeighborIdArr"": [19, 20, 24, 25],
	""IsWorkplacePoint"": true
, 
	""Id"": 47,
	""PointName"": ""P0017"",
	""PointX"": 1450,
	""PointY"": 100,
	""NeighborIdArr"": [21, 22, 26, 27],
	""IsWorkplacePoint"": true
, 
	""Id"": 48,
	""PointName"": ""P0018"",
	""PointX"": 1450,
	""PointY"": 450,
	""NeighborIdArr"": [22, 23, 27, 28],
	""IsWorkplacePoint"": true
, 
	""Id"": 49,
	""PointName"": ""P0019"",
	""PointX"": 1450,
	""PointY"": 800,
	""NeighborIdArr"": [23, 24, 28, 29],
	""IsWorkplacePoint"": true
, 
	""Id"": 50,
	""PointName"": ""P0020"",
	""PointX"": 1450,
	""PointY"": 1150,
	""NeighborIdArr"": [24, 25, 29, 30],
	""IsWorkplacePoint"": true
]";
            #endregion JsonString 

效果图(没有标记点名称)

寻路效果

程序运行效果

源代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics.Eventing.Reader;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 测试RFID读写工具

    public partial class Form2 : Form
    
        string json = "";
        List<RoadPoint> Points;
        int canvasWidth;
        int canvasHeight;
        float maxX;
        float maxY;

        float rx;
        float ry;

		[ThreadStatic]
		public static string A;
        public Form2()
        
            InitializeComponent();
            this.Load += Form2_Load;
        

        private void Form2_Load(object sender, EventArgs e)
        
            #region JsonString
            json = @"[
	""Id"": 1,
	""PointName"": ""A0001"",
	""PointX"": 0,
	""PointY"": 0,
	""NeighborIdArr"": [2, 6],
	""IsWorkplacePoint"": false
, 
	""Id"": 2,
	""PointName"": ""A0002"",
	""PointX"": 0,
	""PointY"": 200,
	""NeighborIdArr"": [1, 3, 7],
	""IsWorkplacePoint"": false
, 
	""Id"": 3,
	""PointName"": ""A0003"",
	""PointX"": 0,
	""PointY"": 700,
	""NeighborIdArr"": [2, 4, 8],
	""IsWorkplacePoint"": false
, 
	""Id"": 4,
	""PointName"": ""A0004"",
	""PointX"": 0,
	""PointY"": 900,
	""NeighborIdArr"": [3, 5, 9],
	""IsWorkplacePoint"": false
, 
	""Id"": 5,
	""PointName"": ""A0005"",
	""PointX"": 0,
	""PointY"": 1400,
	""NeighborIdArr"": [4, 10],
	""IsWorkplacePoint"": false
, 
	""Id"": 6,
	""PointName"": ""A0006"",
	""PointX"": 200,
	""PointY"": 0,
	""NeighborIdArr"": [1, 7, 11],
	""IsWorkplacePoint"": false
, 
	""Id"": 7,
	""PointName"": ""A0007"",
	""PointX"": 200,
	""PointY"": 200,
	""NeighborIdArr"": [2, 6, 12, 8],
	""IsWorkplacePoint"": false
, 
	""Id"": 8,
	""PointName"": ""A0008"",
	""PointX"": 200,
	""PointY"": 700,
	""NeighborIdArr"": [3, 7, 13, 9],
	""IsWorkplacePoint"": false
, 
	""Id"": 9,
	""PointName"": ""A0009"",
	""PointX"": 200,
	""PointY"": 900,
	""NeighborIdArr"": [4, 8, 14, 10],
	""IsWorkplacePoint"": false
, 
	""Id"": 10,
	""PointName"": ""A0010"",
	""PointX"": 200,
	""PointY"": 1400,
	""NeighborIdArr"": [5, 9, 15],
	""IsWorkplacePoint"": false
, 
	""Id"": 11,
	""PointName"": ""A0011"",
	""PointX"": 600,
	""PointY"": 0,
	""NeighborIdArr"": [6, 12, 16],
	""IsWorkplacePoint"": false
, 
	""Id"": 12,
	""PointName"": ""A0012"",
	""PointX"": 600,
	""PointY"": 200,
	""NeighborIdArr"": [7, 13, 17, 11],
	""IsWorkplacePoint"": false
, 
	""Id"": 13,
	""PointName"": ""A0013"",
	""PointX"": 600,
	""PointY"": 700,
	""NeighborIdArr"": [8, 14, 18, 12],
	""IsWorkplacePoint"": false
, 
	""Id"": 14,
	""PointName"": ""A0014"",
	""PointX"": 600,
	""PointY"": 900,
	""NeighborIdArr"": [9, 15, 19, 13],
	""IsWorkplacePoint"": false
, 
	""Id"": 15,
	""PointName"": ""A0015"",
	""PointX"": 600,
	""PointY"": 1400,
	""NeighborIdArr"": [10, 20, 14],
	""IsWorkplacePoint"": false
, 
	""Id"": 16,
	""PointName"": ""A0016"",
	""PointX"": 800,
	""PointY"": 0,
	""NeighborIdArr"": [11, 21, 17],
	""IsWorkplacePoint"": false
, 
	""Id"": 17,
	""PointName"": ""A0017"",
	""PointX"": 800,
	""PointY"": 200,
	""NeighborIdArr"": [12, 16, 22, 18],
	""IsWorkplacePoint"": false
, 
	""Id"": 18,
	""PointName"": ""A0018"",
	""PointX"": 800,
	""PointY"": 700,
	""NeighborIdArr"": [13, 17, 23, 19],
	""IsWorkplacePoint"": false
, 
	""Id"": 19,
	""PointName"": ""A0019"",
	""PointX"": 800,
	""PointY"": 900,
	""NeighborIdArr"": [14, 18, 24, 20],
	""IsWorkplacePoint"": false
, 
	""Id"": 20,
	""PointName"": ""A0020"",
	""PointX"": 800,
	""PointY"": 1400,
	""NeighborIdArr"": [15, 19, 25],
	""IsWorkplacePoint"": false
, 
	""Id"": 21,
	""PointName"": ""A0021"",
	""PointX"": 1200,
	""PointY"": 0,
	""NeighborIdArr"": [16, 26, 22],
	""IsWorkplacePoint"": false
, 
	""Id"": 22,
	""PointName"": ""A0022"",
	""PointX"": 1200,
	""PointY"": 200,
	""NeighborIdArr"": [17, 21, 27, 23],
	""IsWorkplacePoint"": false
, 
	""Id"": 23,
	""PointName"": ""A0023"",
	""PointX"": 1200,
	""PointY"": 700,
	""NeighborIdArr"": [18, 22, 28, 24],
	""IsWorkplacePoint"": false
, 
	""Id"": 24,
	""PointName"": ""A0024"",
	""PointX"": 1200,
	""PointY"": 900,
	""NeighborIdArr"": [19, 23, 29, 25],
	""IsWorkplacePoint"": false
, 
	""Id"": 25,
	""PointName"": ""A0025"",
	""PointX"": 1200,
	""PointY"": 1400,
	""NeighborIdArr"": [20, 24, 30],
	""IsWorkplacePoint"": false
, 
	""Id"": 26,
	""PointName"": ""A0026"",
	""PointX"": 1700,
	""PointY"": 0,
	""NeighborIdArr"": [21, 27],
	""IsWorkplacePoint"": false
, 
	""Id"": 27,
	""PointName"": ""A0027"",
	""PointX"": 1700,
	""PointY"": 200,
	""NeighborIdArr"": [22, 26, 28],
	""IsWorkplacePoint"": false
, 
	""Id"": 28,
	""PointName"": ""A0028"",
	""PointX"": 1700,
	""PointY"": 700,
	""NeighborIdArr"": [23, 27, 29],
	""IsWorkplacePoint"": false
, 
	""Id"": 29,
	""PointName"": ""A0029"",
	""PointX"": 1700,
	""PointY"": 900,
	""NeighborIdArr"": [24, 28, 30],
	""IsWorkplacePoint"": false
, 
	""Id"": 30,
	""PointName"": ""A0030"",
	""PointX"": 1700,
	""PointY"": 1400,
	""NeighborIdArr"": [25, 29],
	""IsWorkplacePoint"": false
, 
	""Id"": 31,
	""PointName"": ""P0001"",
	""PointX"": 100,
	""PointY"": 100,
	""NeighborIdArr"": [1, 2, 6, 7],
	""IsWorkplacePoint"": true
, 
	""Id"": 32,
	""PointName"": ""P0002"",
	""PointX"": 100,
	""PointY"": 450,
	""NeighborIdArr"": [2, 3, 7, 8],
	""IsWorkplacePoint"": true
, 
	""Id"": 33,
	""PointName"": ""P0003"",
	""PointX"": 100,
	""PointY"": 800,
	""NeighborIdArr"": [3, 4, 8, 9],
	""IsWorkplacePoint"": true
, 
	""Id"": 34,
	""PointName"": ""P0004"",
	""PointX"": 100,
	""PointY"": 1150,
	""NeighborIdArr"": [4, 5, 9, 10],
	""IsWorkplacePoint"": true
, 
	""Id"": 35,
	""PointName"": ""P0005"",
	""PointX"": 400,
	""PointY"": 100,
	""NeighborIdArr"": [6, 7, 11, 12],
	""IsWorkplacePoint"": true
, 
	""Id"": 36,
	""PointName"": ""P0006"",
	""PointX"": 400,
	""PointY"": 450,
	""NeighborIdArr"": [7, 8, 12, 13],
	""IsWorkplacePoint"": true
, 
	""Id"": 37,
	""PointName"": ""P0007"",
	""PointX"": 400,
	""PointY"": 800,
	""NeighborIdArr"": [8, 9, 13, 14],
	""IsWorkplacePoint"": true
, 
	""Id"": 38,
	""PointName"": ""P0008"",
	""PointX"": 400,
	""PointY"": 1150,
	""NeighborIdArr"": [9, 10, 14, 15],
	""IsWorkplacePoint"": true
, 
	""Id"": 39,
	""PointName"": ""P0009"",
	""PointX"": 700,
	""PointY"": 100,
	""NeighborIdArr"": [11, 12, 16, 17],
	""IsWorkplacePoint"": true
, 
	""Id"": 40,
	""PointName"": ""P0010"",
	""PointX"": 700,
	""PointY"": 450,
	""NeighborIdArr"": [12, 13, 17, 18],
	""IsWorkplacePoint"": true
, 
	""Id"": 41,
	""PointName"": ""P0011"",
	""PointX"": 700,
	""PointY"": 800,
	""NeighborIdArr"": [13, 14, 18, 19],
	""IsWorkplacePoint"": true
, 
	""Id"": 42,
	""PointName"": ""P0012"",
	""PointX"": 700,
	""PointY"": 1150,
	""NeighborIdArr"": [14, 15, 19, 20],
	""IsWorkplacePoint"": true
, 
	""Id"": 43,
	""PointName"": ""P0013"",
	""PointX"": 1000,
	""PointY"": 100,
	""NeighborIdArr"": [16, 17, 21, 22],
	""IsWorkplacePoint"": true
, 
	""Id"": 44,
	""PointName"": ""P0014"",
	""PointX"": 1000,
	""PointY"": 450,
	""NeighborIdArr"": [17, 18, 22, 23],
	""IsWorkplacePoint"": true
, 
	""Id"": 45,
	""PointName"": ""P0015"",
	""PointX"": 1000,
	""PointY"": 800,
	""NeighborIdArr"": [18, 19, 23, 24],
	""IsWorkplacePoint"": true
, 
	""Id"": 46,
	""PointName"": ""P0016"",
	""PointX"": 1000,
	""PointY"": 1150,
	""NeighborIdArr"": [19, 20, 24, 25],
	""IsWorkplacePoint"": true
, 
	""Id"": 47,
	""PointName"": ""P0017"",
	""PointX"": 1450,
	""PointY"": 100,
	""NeighborIdArr"": [21, 22, 26, 27],
	""IsWorkplacePoint"": true
, 
	""Id"": 48,
	""PointName"": ""P0018"",
	""PointX"": 1450,
	""PointY"": 450,
	""NeighborIdArr"": [22, 23, 27, 28],
	""IsWorkplacePoint"": true
, 
	""Id"": 49,
	""PointName"": ""P0019"",
	""PointX"": 1450,
	""PointY"": 800,
	""NeighborIdArr"": [23, 24, 28, 29],
	""IsWorkplacePoint"": true
, 
	""Id"": 50,
	""PointName"": ""P0020"",
	""PointX"": 1450,
	""PointY"": 1150,
	""NeighborIdArr"": [24, 25, 29, 30],
	""IsWorkplacePoint"": true
]";
            #endregion JsonString 

            Points = Newtonsoft.Json.JsonConvert.DeserializeObject<List<RoadPoint>>(json);


            canvasWidth = pictureBox1.Width;
            canvasHeight = pictureBox1.Height;
            maxX = Points.Max(t => t.PointX) * 1f;
            maxY = Points.Max(t => t.PointY) * 1f;

            rx = canvasWidth / maxX;
            ry = canvasHeight / maxY;


            listStart.DisplayMember = "PointName";
            listEnd.DisplayMember = "PointName";
            Points.ForEach(p =>
            
                listStart.Items.Add(p);
                listEnd.Items.Add(p);
            );

            Task.Run<Bitmap>(() =>  return DrawPoints(Points); ).ContinueWith(t =>
            

                if (t.IsCompleted && t.Result != null)
                
                    this.Invoke(new Action(() =>
                    
                        pictureBox1.Image = t.Result;
                    ));
                

            );
        

        //全局变量存储最大最小值,作为绘制区域
        // Graphics g = this.Map.CreateGraphics();
        static Color color = Color.Red;
        static Pen pen = new Pen(color, 4)  DashCap = DashCap.Round, DashStyle = DashStyle.Dot ;

        private Bitmap DrawPoints(List<RoadPoint> points)
        
            Bitmap temp = new Bitmap(canvasWidth, canvasHeight);
            Graphics g = Graphics.FromImage(temp);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;

            foreach (var point in points)
            
                var x = (int)Math.Floor(point.PointX * rx);
                var y = (int)Math.Floor(point.PointY * ry);
                if (x > canvasWidth - 15) x = canvasWidth - 15;
                if (y > canvasHeight - 15) y = canvasHeight - 15;

                Console.WriteLine($"point.Id,x,y");
                if (point.IsWorkplacePoint)
                    g.FillEllipse(Brushes.Green, x, y, 10, 10);

                else
                    g.FillEllipse(Brushes.Red, x, y, 10, 10);

            

            g.ResetTransform();
            g.Dispose();

            temp.Save("point_image.png");

            return temp;
        

        private void button1_Click(object sender, EventArgs e)
        
            if (listStart.SelectedIndex == -1)
            
                labPath.Text = "请选择起点!";
                return;
            
            if (listEnd.SelectedIndex == -1)
            
                labPath.Text = "请选择终点!";
                return;
            
            if (listStart.SelectedIndex == listEnd.SelectedIndex)
            
                labPath.Text = "起点和终点不能相同!";
                return;
            

            var start = listStart.SelectedItem as RoadPoint;
            var end = listEnd.SelectedItem as RoadPoint;

            CalcPath(start, end, Points);
        

        private void CalcPath(RoadPoint start, RoadPoint end, List<RoadPoint> points)
        
            var paths = new List<List<RoadPoint>>();
            foreach (var to in start.NeighborIdArr)
            
                var path = new List<RoadPoint>()  start, points.FirstOrDefault(t => t.Id == to) ;
                paths.Add(path);
            

            var targetPath = CalcPath(end, points, paths);
            if (targetPath == null || targetPath.Count == 0)
            
                labPath.Text = "没有检测到有效路径!";
                return;
            

            labPath.Text = string.Join(" --> ", targetPath.Select(t => t.Id));

            Graphics g = Graphics.FromImage(pictureBox1.Image);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;

            for (var i = 0; i < targetPath.Count - 1; i++)
            
                var point1 = targetPath[i];
                var point2 = targetPath[i + 1];

                g.DrawLine(pen,
                    new Point((int)Math.Floor(point1.PointX * rx), (int)Math.Floor(point1.PointY * ry)),
                    new Point((int)Math.Floor(point2.PointX * rx), (int)Math.Floor(point2.PointY * ry)));
            

            g.ResetTransform();
            g.Dispose();

            pictureBox1.Refresh();

        

        private List<RoadPoint> CalcPath(RoadPoint end, List<RoadPoint> points, List<List<RoadPoint>> paths)
        
            if (paths == null) return null;

            List<List<RoadPoint>> newPaths = new List<List<RoadPoint>>();
            foreach (var path in paths)
            
                var currStart = path.Last();
                if (currStart == end)
                    return path;
                foreach (var to in currStart

Unity吃豆人敌人BFS广度(宽度)优先算法实现怪物追踪玩家寻路

本人正在努力建设自己的公众号,大家可以关注公众号,此文章最近也会上线我的公众号,公众号将免费提供大量教学Unity相关内容,除了从Unity入门到数据结构设计模式外,我还会免费分享我再游戏开发中使用的素材,书籍,插件,希望大家可以关注我,提前谢谢啦!

文章目录

以上是关于广度优先寻路算法的主要内容,如果未能解决你的问题,请参考以下文章

寻路算法--- 广度优先搜索

广度优先寻路算法

寻路算法广度优先搜索与迪杰斯特拉算法

深度优先算法 和 宽度优先算法 的优缺点

C语言之广度优先算法

广度优先搜索算法(go)