寻路
来源:互联网 发布:定格动画软件app 编辑:程序博客网 时间:2024/05/16 09:22
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ShortPathTest
{
class GenShortPath
{
public int GenTargetPath(int tableWidth, int x1, int y1, int x2, int y2)
{
if (tableWidth <= 0) return -1;
int retValue = -1;
//初始化一个图
Element[,] map = new Element[tableWidth, tableWidth];
int[,] distance = new int[tableWidth * tableWidth, tableWidth * tableWidth];
List<Element> pointsInMap = new List<Element>();
for (int i = 0; i < tableWidth; i++)
{
for (int j = 0; j < tableWidth; j++)
{
Element e = new Element() { posX = i, posY = j };
map[i, j] = e;
pointsInMap.Add(e);
}
}
for (int i = 0; i < tableWidth * tableWidth; i++)
{
for (int j = 0; j < tableWidth * tableWidth; j++)
{
if (i == j)
{
distance[i, j] = 0;
}
else
{
int posX_1 = pointsInMap.posX;
int posY_1 = pointsInMap.posY;
int posX_2 = pointsInMap[j].posX;
int posY_2 = pointsInMap[j].posY;
//判断两个点的相对位置
if (Math.Abs(posX_1 - posX_2) == 1 && Math.Abs(posY_1 - posY_2) == 2)
{
distance[i, j] = 1;
}
else if (Math.Abs(posX_1 - posX_2) == 2 && Math.Abs(posY_1 - posY_2) == 1)
{
distance[i, j] = 1;
}
else
{
distance[i, j] = int.MaxValue;
}
}
}
}
//寻找路点
int pointStartIndex = tableWidth * x1 + y1;
Dictionary<int, int> dist = new Dictionary<int,int>();
Dictionary<int, List<Element>> path = new Dictionary<int, List<Element>>();
for (int i = 0; i < tableWidth * tableWidth; i++)
{
path = new List<Element>();
dist = distance[i,pointStartIndex];
if (dist < int.MaxValue)
{
path.Add(pointsInMap[pointStartIndex]);
path.Add(pointsInMap);
}
}
List<Element> s = new List<Element>();
s.Add(pointsInMap[pointStartIndex]);
for (int t = 0; t <= tableWidth * tableWidth - 1; t++)
{
int min = int.MaxValue;
int k = -1;
for (int i = 0; i < tableWidth * tableWidth; i++)
{
if (!s.Contains(pointsInMap) && dist < min) {
k = i;
min = dist;
}
}
if (min == int.MaxValue) break;
s.Add(pointsInMap[k]);
for (int i = 0; i < tableWidth * tableWidth; i++)
{
if (!s.Contains(pointsInMap) && distance[k, i] != int.MaxValue && (dist[k] + distance[k, i]) < dist)
{
dist = dist[k] + distance[k, i];
path = new List<Element>(path[k]);
path.Add(pointsInMap);
}
}
}
foreach (int k in path.Keys)
{
List<Element> curPath = path[k];
Element ele = curPath[curPath.Count - 1];
if (ele.posX == x2 && ele.posY == y2)
{
string str = "Path to::========>>>" + curPath[curPath.Count - 1].ToString() + "\n";
for (int i = 0; i < curPath.Count; i++)
{
str += curPath.ToString() + "-->";
}
Console.WriteLine(str);
}
}
return retValue;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ShortPathTest
{
class GenShortPath
{
public int GenTargetPath(int tableWidth, int x1, int y1, int x2, int y2)
{
if (tableWidth <= 0) return -1;
int retValue = -1;
//初始化一个图
Element[,] map = new Element[tableWidth, tableWidth];
int[,] distance = new int[tableWidth * tableWidth, tableWidth * tableWidth];
List<Element> pointsInMap = new List<Element>();
for (int i = 0; i < tableWidth; i++)
{
for (int j = 0; j < tableWidth; j++)
{
Element e = new Element() { posX = i, posY = j };
map[i, j] = e;
pointsInMap.Add(e);
}
}
for (int i = 0; i < tableWidth * tableWidth; i++)
{
for (int j = 0; j < tableWidth * tableWidth; j++)
{
if (i == j)
{
distance[i, j] = 0;
}
else
{
int posX_1 = pointsInMap.posX;
int posY_1 = pointsInMap.posY;
int posX_2 = pointsInMap[j].posX;
int posY_2 = pointsInMap[j].posY;
//判断两个点的相对位置
if (Math.Abs(posX_1 - posX_2) == 1 && Math.Abs(posY_1 - posY_2) == 2)
{
distance[i, j] = 1;
}
else if (Math.Abs(posX_1 - posX_2) == 2 && Math.Abs(posY_1 - posY_2) == 1)
{
distance[i, j] = 1;
}
else
{
distance[i, j] = int.MaxValue;
}
}
}
}
//寻找路点
int pointStartIndex = tableWidth * x1 + y1;
Dictionary<int, int> dist = new Dictionary<int,int>();
Dictionary<int, List<Element>> path = new Dictionary<int, List<Element>>();
for (int i = 0; i < tableWidth * tableWidth; i++)
{
path = new List<Element>();
dist = distance[i,pointStartIndex];
if (dist < int.MaxValue)
{
path.Add(pointsInMap[pointStartIndex]);
path.Add(pointsInMap);
}
}
List<Element> s = new List<Element>();
s.Add(pointsInMap[pointStartIndex]);
for (int t = 0; t <= tableWidth * tableWidth - 1; t++)
{
int min = int.MaxValue;
int k = -1;
for (int i = 0; i < tableWidth * tableWidth; i++)
{
if (!s.Contains(pointsInMap) && dist < min) {
k = i;
min = dist;
}
}
if (min == int.MaxValue) break;
s.Add(pointsInMap[k]);
for (int i = 0; i < tableWidth * tableWidth; i++)
{
if (!s.Contains(pointsInMap) && distance[k, i] != int.MaxValue && (dist[k] + distance[k, i]) < dist)
{
dist = dist[k] + distance[k, i];
path = new List<Element>(path[k]);
path.Add(pointsInMap);
}
}
}
foreach (int k in path.Keys)
{
List<Element> curPath = path[k];
Element ele = curPath[curPath.Count - 1];
if (ele.posX == x2 && ele.posY == y2)
{
string str = "Path to::========>>>" + curPath[curPath.Count - 1].ToString() + "\n";
for (int i = 0; i < curPath.Count; i++)
{
str += curPath.ToString() + "-->";
}
Console.WriteLine(str);
}
}
return retValue;
}
}
}
0 0
- 寻路
- 寻路
- 寻路
- a*寻路
- BFS寻路
- A*寻路
- 游戏-寻路
- lua 寻路
- A*寻路
- 寻路NavMeshAgent
- A* 寻路
- A*寻路
- A*寻路
- 文学随笔:《寻路》
- 寻路:最多拐弯一次
- 游戏中的A*寻路
- 深入解析AS3寻路
- 寻路__矩形
- 将二叉搜索树转换成升序的双向链表
- 倒排索引——MapReduce实战
- MySQL性能优化的21条最佳经验
- 时间管理
- HDOJ 1257 最少拦截系统 贪心算法
- 寻路
- java源注解
- C语言调动php代码示例
- Android Studio 掌握这些调试技巧,Debug能力不能再高啦
- centos VM VirtualBox虚拟机设置跟windows主机共享文件夹
- Java语法----Java中equals和==的区别
- 关于vfsmount的理解
- 不安装oracle怎样使用PLSQL连接远程(局域网内)服务器上的数据库
- CoordinatorLayout+WebView头部CollapsingToolbarLayout无法滚动的处理办法