• 大小: 64KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-17
  • 语言: C#
  • 标签: C#迷宫bfs  

资源简介

1.随意设置起点位置 2.随意设置终点位置 3.随意设置障碍物 4.自动bfs寻路,地图上打印寻找的路径

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int flag = 0;
        
        int []map=new int[1010];
        int [] d=new int [42]{{10}{-10}{01}{0-1}};
        List road = new List();
        public Nodes strat = new Nodes();
        public Nodes end = new Nodes();
        


        private void Form1_Load(object sender EventArgs e)
        {
             


        }

        private void button1_Click(object sender EventArgs e)
        {
            bfs();
        }
        void bfs()
        {
            
            
           
           
            Node work;
            Nodes hq = new Nodes() ext = new Nodes();
            Queue Q = new Queue();

            strat.next = null;
            end.next = null;


            Q.Enqueue(strat);


            while (Q.Count > 0)//  队列 不为空  就不结束循环
            {

                hq = Q.Dequeue();

                int i nrow ncol;
                for (i = 0; i < 4; i++)
                {
                    nrow = hq.pos.x/40 + d[i 0];
                    ncol = hq.pos.y /40+ d[i 1];
                   
                    if (nrow >= 0 && nrow <= 9 && ncol >= 0 && ncol <= 9 && map[nrow ncol] !=3 &&map[nrow ncol] !=1  && map[nrow ncol] != -1)
                    {
                        work = new Node();
                        work.x = nrow*40;
                        work.y = ncol*40;
                  ext = new Nodes();

                 ext.pos = work;
                 ext.next = hq;

                        

                        map[nrow ncol] = -1;//标记走过的路径
                        
                        if (nrow == end.pos.x/40 && ncol == end.pos.y/40)
                        {
                            printbfs(ext);
                            return;
                        }


                        Q.Enqueue(ext);
                    }
                }

            }
          

        }
        void printbfs(Nodes p)
        {

            
          
            while (p != null)
            {
                road.Add(p);
                p = p.next;
             
            }

            for (int i = 0; i < road.Count; i++)
            {
                Button b = new Button();
                b.Location = new Point(road[i].pos.x  road[i].pos.y );
                b.Size = new Size(40 40);
                b.BackColor = Color.Yellow;
                panel1.Controls.Add(b);
                 
            }
            
             
        }


   

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        962  2017-06-12 22:15  C# bfs 自动寻路迷宫\WindowsFormsApplication1.sln

     文件        167  2017-06-15 12:23  C# bfs 自动寻路迷宫\WindowsFormsApplication1\App.config

     文件       5384  2017-06-14 12:35  C# bfs 自动寻路迷宫\WindowsFormsApplication1\Form1.cs

     文件       4636  2017-06-13 22:13  C# bfs 自动寻路迷宫\WindowsFormsApplication1\Form1.Designer.cs

     文件       5817  2017-06-13 22:13  C# bfs 自动寻路迷宫\WindowsFormsApplication1\Form1.resx

     文件        483  2017-06-13 22:37  C# bfs 自动寻路迷宫\WindowsFormsApplication1\Nodes.cs

     文件        536  2017-06-12 22:15  C# bfs 自动寻路迷宫\WindowsFormsApplication1\Program.cs

     文件       3932  2017-06-15 12:23  C# bfs 自动寻路迷宫\WindowsFormsApplication1\WindowsFormsApplication1.csproj

     文件      12288  2017-06-15 12:23  C# bfs 自动寻路迷宫\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe

     文件        167  2017-06-15 12:23  C# bfs 自动寻路迷宫\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe.config

     文件      36352  2017-06-15 12:23  C# bfs 自动寻路迷宫\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.pdb

     文件      22984  2017-06-15 12:23  C# bfs 自动寻路迷宫\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.vshost.exe

     文件        167  2017-06-15 12:23  C# bfs 自动寻路迷宫\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.vshost.exe.config

     文件        490  2012-06-06 02:06  C# bfs 自动寻路迷宫\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.vshost.exe.manifest

     文件       6979  2017-06-15 12:23  C# bfs 自动寻路迷宫\WindowsFormsApplication1\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件          0  2017-06-12 22:15  C# bfs 自动寻路迷宫\WindowsFormsApplication1\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs

     文件          0  2017-06-12 22:15  C# bfs 自动寻路迷宫\WindowsFormsApplication1\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs

     文件          0  2017-06-12 22:15  C# bfs 自动寻路迷宫\WindowsFormsApplication1\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

     文件       2325  2017-06-15 12:23  C# bfs 自动寻路迷宫\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.csproj.FileListAbsolute.txt

     文件      12288  2017-06-15 12:23  C# bfs 自动寻路迷宫\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.exe

     文件        180  2017-06-15 12:23  C# bfs 自动寻路迷宫\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.Form1.resources

     文件      36352  2017-06-15 12:23  C# bfs 自动寻路迷宫\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.pdb

     文件        180  2017-06-15 12:23  C# bfs 自动寻路迷宫\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.Properties.Resources.resources

     文件       4608  2017-06-15 12:23  C# bfs 自动寻路迷宫\WindowsFormsApplication1\obj\Debug\TempPE\Properties.Resources.Designer.cs.dll

     文件        975  2017-06-14 12:37  C# bfs 自动寻路迷宫\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.csproj.GenerateResource.Cache

     文件      30878  2017-06-15 12:23  C# bfs 自动寻路迷宫\WindowsFormsApplication1\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       3873  2017-06-15 12:23  C# bfs 自动寻路迷宫\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.csprojResolveAssemblyReference.cache

     文件       1386  2017-06-12 22:15  C# bfs 自动寻路迷宫\WindowsFormsApplication1\Properties\AssemblyInfo.cs

     文件       2888  2017-06-15 12:23  C# bfs 自动寻路迷宫\WindowsFormsApplication1\Properties\Resources.Designer.cs

     文件       5612  2017-06-12 22:15  C# bfs 自动寻路迷宫\WindowsFormsApplication1\Properties\Resources.resx

............此处省略15个文件信息

评论

共有 条评论

相关资源