五彩珠游戏

//a*算法，比广度多一个参数，来评估队列中每个点到目的地的估计消耗，来提高性能。
public Queue path3(point3 start, point3 end, int[,] migonga)
{
int[,] migong = (int[,])migonga.Clone();
Queue myqueue = new Queue();
myqueue = new Queue();
myqueue.Enqueue(start);
start.level = 1;
start.prepoint = null;
start.visited = true;
start.g = 0;
start.f = 99;
migong[start.py, start.px] = 1;
int step = 0;
while (myqueue.Count != 0 && (myqueue.Peek().px == end.px && myqueue.Peek().py == end.py) == false)
{
step += 1;
//myqueue = (Queue)myqueue.OrderByDescending(p => p.f);

//对评估直排序，先搜索评估直最小的。

List tem = new List(myqueue);
myqueue.Clear();
for (int yy = 1; yy < tem.Count; yy++)
{
if (tem[yy].f < tem[0].f)
{
point3 tempp = tem[0];
tem[0] = tem[yy];
tem[yy] = tempp;
}
}
for (int yy = 0; yy < tem.Count; yy++)
{
myqueue.Enqueue(tem[yy]);
}

point3 p = myqueue.Dequeue();
if (migong[p.py + 1, p.px] < 1)
{

point3 pa = new point3(p.px, p.py + 1, p.level + 1, true, p, 1 + p.g, getFValue(p, end));
migong[pa.py, pa.px] = 1;
myqueue.Enqueue(pa);
if (pa.px == end.px && pa.py == end.py)
{
break;
}
}

if (migong[p.py - 1, p.px] < 1)
{
point3 pa = new point3(p.px, p.py - 1, p.level + 1, true, p, 1 + p.g, getFValue(p, end));
migong[pa.py, pa.px] = 1;
myqueue.Enqueue(pa);
if (pa.px == end.px && pa.py == end.py)
{
myqueue.Enqueue(pa);
break;
}
}

if (migong[p.py, p.px + 1] < 1)
{
point3 pa = new point3(p.px + 1, p.py, p.level + 1, true, p, 1 + p.g, getFValue(p, end));
myqueue.Enqueue(pa);
migong[pa.py, pa.px] = 1;
if (pa.px == end.px && pa.py == end.py)
{
myqueue.Enqueue(pa);
break;
}
}

if (migong[p.py, p.px - 1] < 1)
{
point3 pa = new point3(p.px - 1, p.py, p.level + 1, true, p, 1 + p.g, getFValue(p, end));
myqueue.Enqueue(pa);
migong[pa.py, pa.px] = 1;
if (pa.px == end.px && pa.py == end.py)
{
myqueue.Enqueue(pa);
break;
}
}
}
Console.WriteLine(step.ToString());
return myqueue;
}

//得到某点到目的点的消耗评估
public int getFValue(point3 current, point3 end)
{
return Math.Abs(current.px - end.px) + Math.Abs(current.py - end.py);
}

• 博文量
2376
• 访问量
5297180