写的有点简单主要是不想浪费时间
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace lianxi
{
public partial class Form1 : Form
{
//状态 0未探索 1开启 2关闭 3障碍物
public static Pane[,] panes = new Pane[29, 29];
//存储开启列表
public static List<Pane> list = new List<Pane>();
//存储路径
public List<Pane> lx = new List<Pane>();
public Form1()
{
InitializeComponent();
this.DoubleBuffered = true;
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 29; i++)
{
for (int j = 0; j < 29; j++)
{
panes[i, j] = new Pane(i, j);
}
}
//指定起点和终点以及障碍物
panes[10, 10].zt = 1;
panes[13, 8].zt = 3;
panes[13, 9].zt = 3;
panes[13, 10].zt = 3;
panes[13, 11].zt = 3;
panes[13, 12].zt = 3;
panes[13, 7].zt = 3;
panes[13, 13].zt = 3;
panes[13, 14].zt = 3;
panes[13, 6].zt = 3;
//终点 20,10 起点 10,10
list.Add(panes[10, 10]);
chazhao();
if (panes[20, 10].pan != null)
{
luxian(panes[20, 10]);
}
else
{
MessageBox.Show("死路");
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
Pen p1 = new Pen(Color.Gray);
for (int i = 0; i < 30; i++)
{
g.DrawLine(p1, 0, 10 * i, 300, i * 10);
}
for (int i = 0; i < 30; i++)
{
g.DrawLine(p1, 10 * i, 0, 10 * i, 300);
}
for (int i = 0; i < lx.Count; i++)
{
Pane pane = lx[i];
g.FillRectangle(Brushes.Green, pane.X * 10, pane.Y * 10, 10, 10);
}
g.FillRectangle(Brushes.Red, 100, 100, 10, 10);
g.FillRectangle(Brushes.Red, 200, 100, 10, 10);
g.FillRectangle(Brushes.Blue, 130, 70, 10, 10);
g.FillRectangle(Brushes.Blue, 130, 80, 10, 10);
g.FillRectangle(Brushes.Blue, 130, 90, 10, 10);
g.FillRectangle(Brushes.Blue, 130, 100, 10, 10);
g.FillRectangle(Brushes.Blue, 130, 110, 10, 10);
g.FillRectangle(Brushes.Blue, 130, 120, 10, 10);
g.FillRectangle(Brushes.Blue, 130, 130, 10, 10);
for (int i = 0; i < list.Count; i++)
{
Pane pane = list[i];
g.FillRectangle(Brushes.Yellow, pane.X * 10, pane.Y * 10, 10, 10);
}
}
public void luxian(Pane pane)
{
if (pane != null)
{
lx.Add(pane);
luxian(pane.pan);
}
}
public void chazhao()
{
while (true)
{
Pane pane = null;
if (list.Count > 0)
{
pane = list[0];
int index = 0;
for (int i = 1; i < list.Count; i++)
{
if (list[i].G + list[i].H < pane.G + pane.H)
{
pane = list[i];
index = i;
}
}
list.RemoveAt(index);
panes[pane.X, pane.Y].zt = 2;
if (panes[20, 10].zt == 2)
{
break;
}
}
else
{
break;
}
lookup(pane);
}
}
public void lookup(Pane pane)
{
int x = pane.X;
int y = pane.Y;
for (int i = 0; i < 8; i++)
{
switch (i)
{
case 0://上
if (y <= 0)
{
break;
}
open(pane, x, y - 1,10);
break;
case 1://下
if (y >= 28)
{
break;
}
open(pane, x, y + 1, 10);
break;
case 2://左
if (x <= 0)
{
break;
}
open(pane, x - 1, y, 10);
break;
case 3://右
if (x >= 28)
{
break;
}
open(pane, x + 1, y, 10);
break;
case 4://左上
if (x <= 0 || y <= 0)
{
break;
}
open(pane, x - 1, y - 1, 10);
break;
case 5://右上
if (x >= 28 || y <= 0)
{
break;
}
open(pane, x + 1, y - 1, 10);
break;
case 6://左下
if (y >= 28 || x <= 0)
{
break;
}
open(pane, x - 1, y + 1, 10);
break;
case 7://右下
if (y >= 28 || x >= 28)
{
break;
}
open(pane, x + 1, y + 1, 10);
break;
}
}
}
public void open(Pane pane,int x,int y,int number)
{
Pane p = panes[x, y];
if (p.zt == 2 || p.zt == 3)
{
return;
}
else if (p.zt == 1)//状态为已开启
{
if (pane.G + pane.H + number < p.G + p.H)
{
panes[x, y].pan = pane;
}
}
else//状态为未探索
{
panes[x, y].zt = 1;
panes[x, y].G = pane.G + number;
panes[x, y].H = (20 - x) * (20 - x) + (10 - y) * (10 - y);
panes[x, y].pan = pane;
list.Add(panes[x, y]);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace lianxi
{
public class Pane
{
//状态 0未探索 1开启 2关闭 3障碍物 4起点 5终点
public int zt = 0;
//父节点
public Pane pan = null;
//G
public int G = 0;
//H
public int H = 0;
//X
public int X;
//Y
public int Y;
public Pane(int x, int y)
{
this.X = x;
this.Y = y;
}
}
}
请高手跟我指点哈哦
新手如果有什么不懂得可以问我哈