「astar算法java」Astar算法C语言
本篇文章给大家谈谈astar算法java,以及Astar算法C语言对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、会一点java,有面向对象编程基础,选择入门unity有很大难度吗?应如何有效的入门?
- 2、Java 链表节点值问题
- 3、问: 40 人工智能及其应用期末作业 用A*算法解决下面的八数码难题。试定义估价函数,启发函数,
- 4、A*算法的实际运用
- 5、astar安卓版怎么用
会一点java,有面向对象编程基础,选择入门unity有很大难度吗?应如何有效的入门?
你可以对照着我们的课程大纲看一下自己的能力
C#语言
数据类型,常量,变量,运算符和表达式及命名规则
输入输出方法,数据类型转换
分支结构,循环,关系运算符,逻辑运算符
一维数组与foreach循环,冒泡排序与二维数组
枚举与结构体定义、结构体成员及访问
面向对象编程 类和面向对象概念,对象的字段成员
对象中的方法成员,方法类型详解
对象中的属性和方法参数
string字符串对象、装箱和拆箱、方法的重载和递归
构造和析构函数
抽象方法、虚方法,多态实现
静抽象类,静态类和单例设计模式
接口和泛型
集合、委托 接口介绍,接口实现多态
泛型方法、泛型类、泛型约束
ArrayList、List、Queue(队列)
Stack(堆栈)、Hashtable (哈希表)、Dictionary(字典)
委托与事件、C#反射类、实现范例的Observer设计模式
实战项目及阶段考核 2048、随机抽奖系统、图书管理系统、乒乓球大对决
题库中随机抽题,包含笔试题、上机题,学生需在规定时间内作答
Unity引擎
开发基础 Unity面板及基本操作
游戏对象的操作
预制体的创建和使用
3D基础理论
面向组件开发 Unity工程结构
Unity开发框架
面向组件的开发思想
脚本组件及生命周期、回调方法的概念
常用类(Transform、GameObject、Vector3、
Quaternion、Time、Mathf、Resources资源加载)
物理系统 输入控制、Input类,输入配置
碰撞器--Collider组件家族
刚体组件与力--Rigidbody组件
刚体组件与力--Rigidbody组件、碰撞条件及回调方法
物理材质、射线、发射方法及重载、角色控制器
实战项目 打飞机、坦克大战、HelixJump、运转银河系、打砖块、接金币
2D精灵和UI Sprite精灵,图集的切割、打包,计算机2D图形学基础
2D物理组件(刚体、碰撞器)
2D动画创建--初识Animation
2D开发常用类,碰撞、触发回调
TimeLine制作剧情
UGUI初级 画布Canvas初识
UV坐标,UI坐标
基本控件、复合控件
UGUI的布局和适配方案
UGUI高级 Canvas的渲染模式、适配模式介绍
水平布局、垂直布局、网格布局组件
ScrollView效果制作、Toggle分页、QQ聊天窗口
UI多种交互方式、事件回调
UGUI案例 MVC设计模式,小地图制作、方位坐标、背包、关卡选择案例
实战项目 捕鱼达人、梦幻西游、超级玛丽、消消乐
动画系统 模型资源分析
动画类型,Avatar系统
动画节点、动画状态机
原画UV展开;人形动画代码控制,角色控制器综合应用
动画系统高级 动画遮罩;
IK动画;
动画事件;
动画曲线
unity高级
数据持久化 PlayerPrefs、Sqlite
XML、JSON、CSV文档读取、Excel加密存取
WWW类和协程 协程、线程和进程的概念
协程的设计思想及使用
WWW类,封装请求工具类
Http协议简介(Get、Post)
资源加载 AssetBundle资源打包及依赖分析
基于WWW类远程资源获取
使用AssetBundle进行资源加载及内存管理
性能优化 针对CPU、GPU、内存、美术资源的优化方案
对象池技术
FSM 设计模式
FSM案例人物控制
FSM案例-Buffer系统
FSM案例-AI系统
行为树 游戏AI方案对比,最优解问题分析;
BehaviorDesigner插件,代码控制
我这有一个Unity学习交流,里面有大神也有小白,可以在群里甩问题啊,而且不定期分享学习资料 q.u.n.[887.207.898]q.u.n. 备注:小白
A*算法 理解AStar算法原理;
代码实现AStar算法
Shader 图形学初探,基础知识;
固定管线着色器;
顶面着色器和表面着色器;
Shader案例
网络 Unet、HLAPI详解,网络版CS射击;
基于ASP.net的web站点搭建;
SqlServer数据库的接入和访问;
基于Post请求的数据通信;
Socket编程基础、制定协议、Socket通信、数据安全
实战项目
及阶段考核 阴阳师、镇魔曲、荒野行动、泡泡堂
题库中随机抽题,包含笔试题、上机题,学生需在规定时间内作答
VR、AR
VR-HTC Vive SteamVR SDK接入及分析
SteamVR 预制体和案例分析
手柄、头部Transform获取,点击事件获取
3D UI交互
射箭、魔法阵绘制、钓线瞬移
性能优化,降低眩晕策略
AR--高通SDK AAR介绍及AR项目展示、常用SDK介绍
Vuforia账号注册、识别图的上传与制作、数据包的下载及使用
手机触屏、陀螺仪与发布的讲解
项目架构与
项目管理 模块封装原理与规范,通用框架搭建,模块封装,消息中心、模块管理器、通信模块、编辑器扩展工具编写
热更新模块(资源热更、逻辑热更)、LuaUI架构、LuaSocket架构、Lua数据库架构、AssetBundle管理规则、AssetBundle自动打包
团队合作工具--SVN
综合项目 学生以小组为单位,组员分工,合作完成至少一个项目,包含但不限于:
RPG角色扮演游戏、ACT动作游戏、AVG冒险游戏、SLG策略游戏、FPS第一人称射击游戏、PZL益智类游戏、MSC音乐游戏、虚拟仿真、VR展示、AR游戏; 项目答辩:学员对本团队的项目进行讲解,讲师进行考核,模拟企业中技术面试环节对项目进行答辩
项目答辩
及评审 对于完成的项目分组进行答辩,按照功能实现、代码规范、以及完成度等进行打分
Java 链表节点值问题
包com.link;公共类节点 {
/ /前一个节点
私营节点上一页;/ /后一个节点
私营节点未来;
/ /值
私人的T值;公共节点(){
超();
}公共节点(节点预防和控制,节点接下来,T值){
超();
this.prev =上一页;
this.next =未来;
THIS.VALUE =值;
}公共节点的GetNext(){
未来的回报;
}公共无效setNext(节点旁边){
this.next =未来;
}公共节点 getPrev(){
返回上级;
}公共无效setPrev(节点先前){
this.prev =上一页;
}公共牛逼的getValue(){
返回值;
}公共无效的setValue(T值){
THIS.VALUE =值;
/ /如果有一个节点或下一个节点
公共布尔的hasNext(){
如果(!this.next = NULL)返回true;
返回false;
公共布尔hasPrev(){
如果(this.prev = NULL!)返回true;
返回false;
------------------------------------ ----包com.link;公共类链表 {
/ /头节点
私营节点头;
/ /构造函数
公众链表(){
头=新的Node (NULL,NULL,NULL);
公共无效添加(T T){
节点温度=头;
而{
温度= temp.getNext()(温度的hasNext());
temp.setNext(新节点(温度,空,T));
私营节点发现(T T){
如果(T == NULL)返回NULL; (!head.hasNext())
如果返回NULL; 节点温度=头;
而(temp.hasNext()){
温度= temp.getNext();
如果(t.equals(temp.getValue())){
返回温度;
返回NULL;
公共布尔hasValue的(T T){如果(发现(T)=空!)返回true;
返回false;
公共布尔删除(T T){
节点温度=发现(T);
如果(temp! = NULL){
temp.getNext()setPrev(temp.getPrev());。
temp.getPrev()。 setNext(temp.getNext());
温度= NULL;
返回true;
返回false;
/ /取一个节点通过索引
公众吨得到(INT指数){
如果返回空值(head.hasNext()!);
节点温度=头;
INT I = 0;
为(我“=指数 temp.hasNext(); i + +){
温度= temp.getNext();
}
如(i ==指数+1)返回temp.getValue();
返回NULL;
}公共静态无效的主要(字串[] args){
LINKLIST 列表=新LINKLIST ();
为(int i = 0; I 20; i + +){
将对List.Add(“字符串”+ I);
/ /以下只打印显示的结果,所以一般情况下不会打印
节点温度= list.head;
INT I = 0;
而(temp.hasNext()){
温度= temp.getNext();
System.out.println(“值”+ I +“:”+ temp.getValue());
i + +;
如果(list.remove(“String10”)){
System.out.println(“成功”);
}其他{
系统。通过out.println(“否”);
温度= list.head;
I = 0;
而(temp.hasNext()){
温度= temp.getNext();
System.out.println(“值”+ I +“:”+ temp.getValue()); i + +;
}
问: 40 人工智能及其应用期末作业 用A*算法解决下面的八数码难题。试定义估价函数,启发函数,
#pragma warning(disable:4786)
#include algorithm
#include cstdio
#include set
#include utility
#include ctime
#include cassert
#include cstring
#include iostream
using namespace std;
/*item记录搜索空间中一个结点
state 记录用整数形式表示的8数码格局
blank 记录当前空格位置,主要用于程序优化,
扩展时可不必在寻找空格位置
g, h 对应g(n), h(n)
pre 记录当前结点由哪个结点扩展而来 */
struct item
{
int state;
int blank;
int g;
int h;
int pre;
};
const int MAXSTEPS = 100000;
const int MAXCHAR = 100;
char buf[MAXCHAR][MAXCHAR]; //open表
item open[MAXSTEPS];
//vectoritem open;
int steps = 0;
//closed表,已查询状态只要知道该状态以及它由哪个结点扩展而来即可,用于输出路径
//每次只需得到对应f值最小的待扩展结点,用堆实现提高效率
pairint, int closed[MAXSTEPS];
//读入,将8数码矩阵格局转换为整数表示
bool read(pairint,int state)
{
if (!gets(buf[0]))
return false;
if (!gets(buf[1]))
return false;
if (!gets(buf[2]))
return false;
//cout strlen(buf[0]) ' ' strlen(buf[1]) ' ' strlen(buf[2]) endl;
assert(strlen(buf[0]) == 5 strlen(buf[1]) == 5 strlen(buf[2]) == 5);
// astar.in中的每行数据长度必须为5
state.first = 0;
for (int i = 0, p = 1; i 3; ++i)
{
for (int j = 0; j 6; j += 2)
{
if (buf[i][j] == '0')
state.second = i * 3 + j / 2; // state.second为0(空格)在节点中的位置
else
state.first += p * (buf[i][j] - '0');
p *= 10;
}
}
/* 若初试节点为:
1 2 3
8 0 4
7 6 5
则state.first为567408321,state.second为4
*/
return true;
}
//计算当前结点距目标的距离
int calculate(int current, int target) // return h=the sum of distances each block have to move to the right position,这里的each block不包括空格
{
int c[9], t[9];
int i, cnt = 0;
for (i = 0; i 9; ++i)
{
c[current % 10] = t[target % 10] = i;
current /= 10;
target /= 10;
}
for (i = 1; i 9; ++i)
cnt += abs(c[i] / 3 - t[i] / 3) + abs(c[i] % 3 - t[i] % 3);
return cnt;
}
//open表中结点间选择时的规则 f(n) = g(n) + h(n)
class cmp
{
public: inline bool operator()(item a, item b)
{
return a.g + a.h b.g + b.h;
}
};
//将整数形式表示转换为矩阵表示输出
void pr(int state)
{
memset(buf, ' ', sizeof(buf));
for (int i = 0; i 3; ++i)
{
for (int j = 0; j 6; j += 2)
{
if (state % 10)
buf[i][j] = state % 10 + '0';
state /= 10;
}
buf[i][5] = '\0';
puts(buf[i]);
}
}
//用于判断当前空格是否可以向对应方向移动
inline bool suit(int a, int b) //空格移动后的坐标为(a,b)
{
return (a = 0 a 3 b = 0 b 3);
}
//递归输出搜索路径
void path(int index)
{
if (index == 0)
{
pr(closed[index].first);
puts("");
return;
}
path(closed[index].second);
pr(closed[index].first); //将整数形式表示转换为矩阵表示输出
puts("");
++steps;
}
int getNixuNum( int state ) //求节点的逆序对数
{
int sum = 0;
int result[9];
memset( result, 0, sizeof(result) );
//cout result[8] result[7] endl;
char buf[10];
itoa( state, buf, 10 );
//cout buf endl;
int k = 0;
while( buf[k] != '\0' )
{
result[9-k-1] = buf[k] - '0';
k++;
}
for( int i = 0; i 9; i++ )
{
for( int j = i + 1; j 9; j++ )
{
if( result[i] result[j] result[i] result[j] )
{
sum++;
}
}
}
return sum; //返回3*3方格数组的逆序对数
}
int main()
{
//cout getNixuNum(87654321);
//open.resize(MAXSTEPS);
unsigned int t1 = clock();
//cout open.size() endl;
if( freopen("astar.in", "r", stdin) == NULL )
{
cout "file not find\n";
exit(0);
};
freopen("astar2.out", "w", stdout);
setintstates;
char tmp[100];
int i, x, y, a, b, nx, ny, end, next, index, kase = 0;
pairint,int start, target;
item head; //4个方向移动时的偏移量
const int xtran[4] = {-1, 0, 1, 0};
const int ytran[4] = {0, 1, 0, -1};
const int p[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
while (read(start)) // 读取初试状态节点
{
unsigned int t2 = clock();
printf("Case %d:\n\n", ++kase);
gets(tmp);
read(target); // 读取目标状态节点
gets(tmp);
int targetNixuNum = getNixuNum(target.first);
//若两者的逆序对数不是同为奇数或同为偶数,则无解
if( !(getNixuNum(start.first)1 targetNixuNum1 || !(getNixuNum(start.first)1) !(targetNixuNum1)) )
{
cout "无法从初始节点到终态节点\n";
exit(0);
}
//初始化open表,将初始状态加入
open[0].state = start.first;
open[0].h = calculate(start.first, target.first); // 计算当前节点到目标节点的估计距离
open[0].blank = start.second;
open[0].pre = -1; // 初始节点无父节点
open[0].g = 0; // 初始节点的g为0
index = 0;
states.insert(start.first); // 扩展过节点保存在states中,即出现过的状态保存在states中,states为setint类型,其中的states中的元素唯一
//提取open表中f值最小元素放入closed表,并对该结点进行扩展
for (end = 1; end 0; ++index) // end为open表中的元素个数,一直循环到open表为空
{
assert(index MAXSTEPS);
//临时存储
head = open[0]; // 由于使用pop_heap函数和push_heap函数,所以open[0]为g+h最小的元素
//放入closed表记录当前格局和由哪个结点扩展而来(该结点肯定已在closed表中)
closed[index].first = open[0].state; //放入close表中,表示已经扩展完的节点,下面的for循环会扩展其节点
closed[index].second = open[0].pre; // index表示当前close表中当前扩展节点的下标
//从open表中删除该结点
pop_heap(open, open + end, cmp());//为algorithm文件中的函数,第一个参数指定开始位置,第二个指定结束,第三个指定比较函数
--end;
//得到结果,递归输出路径
if (head.state == target.first)
{
path(index);
break;
}
x = head.blank / 3;
y = head.blank % 3; //空格在3*3方格中的x,y坐标
/*
|2 0 3|
A = |1 8 4|
|7 6 5| // 看成3*3的数组
则head.blank=1
x=0,y=1,即空格的在3*3的数组中下标为(0,1)
*/
for (i = 0; i 4; ++i)
{
nx = x + xtran[i];
ny = y + ytran[i];
/*
i=0时:(nx,ny)为当前空格向上移动一格后的坐标
i=1时:(nx,ny)为当前空格向右移动一格后的坐标
i=2时:(nx,ny)为当前空格向下移动一格后的坐标
i=3时:(nx,ny)为当前空格向左移动一格后的坐标
*/
if (suit(nx, ny)) // 判断是否能够移动
{
a = head.blank; // 空格当前位置,以上面矩阵A为例,a=1
b = nx * 3 + ny; // 空格移动后的新位置,开始是能够向右边移动,故b=0*3+2=2
//调换十进制表示整数对应两个数字位
next = head.state + ((head.state % p[a + 1]) / p[a] - (head.state % p[b + 1]) / p[b]) * p[b] + ((head.state % p[b + 1]) / p[b] - (head.state % p[a + 1]) / p[a]) * p[a];
// 如head.state=567481302,空格向右移动一次后,next=567481032,即为移动后的节点
// 判断能否由当前节点到达目标节点
if( ( getNixuNum(next)1 targetNixuNum1 ) || ( !(getNixuNum(next)1) !(targetNixuNum1) ) )
{
//判断是否已经扩展过,即已经出现过
if (states.find(next) == states.end()) //没出现就保存一下,也存入open表
{
states.insert(next);
open
astar算法java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Astar算法C语言、astar算法java的信息别忘了在本站进行查找喔。
.pre = index; //扩展后的子节点,其父节点为当前扩展节点open
astar算法java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Astar算法C语言、astar算法java的信息别忘了在本站进行查找喔。
.blank = b;open
astar算法java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Astar算法C语言、astar算法java的信息别忘了在本站进行查找喔。
.state = next;open
astar算法java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Astar算法C语言、astar算法java的信息别忘了在本站进行查找喔。
.h = calculate(next,target.first);open
astar算法java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Astar算法C语言、astar算法java的信息别忘了在本站进行查找喔。
.g = head.g + 1;++end; //open表中元素加1
push_heap(open, open + end, cmp()); //压入堆中
}
}
}
}
}
if (end = 0)
puts("No solution");
else
{
printf("Num of steps: %d\n", steps);
printf("Num of expanded: %d\n", index);
printf("Num of generated: %d\n", index + end);
printf("Time consumed: %d\n\n", clock() - t2);
}
states.clear();
steps = 0;
}
printf("Total time consumed: %d\n", clock() - t1);
return 0;
}
A*算法的实际运用
估价值与实际值越接近,估价函数取得就越好
例如对于几何路网来说,可以取两节点间曼哈顿距离做为估价值,即f=g(n) + (abs(dx - nx) + abs(dy - ny));这样估价函数f在g值一定的情况下,会或多或少的受估价值h的制约,节点距目标点近,h值小,f值相对就小,能保证最短路的搜索向终点的方向进行。明显优于Dijkstra算法的毫无方向的向四周搜索。
conditions of heuristic
Optimistic (must be less than or equal to the real cost)
As close to the real cost as possible
详细内容:
创建两个表,OPEN表保存所有已生成而未考察的节点,CLOSED表中记录已访问过的节点。
算起点的估价值;
将起点放入OPEN表; while(OPEN!=NULL){ 从OPEN表中取估价值f(n)最小的节点n; if(n节点==目标节点) break; for(当前节点n的每个子节点X) { 算X的估价值; if(XinOPEN) if(X的估价值小于OPEN表的估价值) { 把n设置为X的父亲; 更新OPEN表中的估价值;//取最小路径的估价值 } if(XinCLOSE) continue; if(Xnotinboth) { 把n设置为X的父亲; 求X的估价值; 并将X插入OPEN表中;//还没有排序 } }//endfor 将n节点插入CLOSE表中; 按照估价值将OPEN表中的节点排序;//实际上是比较OPEN表内节点f的大小,从最小路径的节点向下进行。}//endwhile(OPEN!=NULL)保存路径,即从终点开始,每个节点沿着父节点移动直至起点,这就是你的路径;
用C语言实现A*最短路径搜索算法 ,作者 Tittup frog(跳跳蛙)。 #include stdio.h#include math.h #define MaxLength 100 //用于优先队列(Open表)的数组#define Height 15 //地图高度#define Width 20 //地图宽度 #define Reachable 0 //可以到达的结点#define Bar 1 //障碍物#define Pass 2 //需要走的步数#define Source 3 //起点#define Destination 4 //终点 #define Sequential 0 //顺序遍历#define NoSolution 2 //无解决方案#define Infinity 0xfffffff #define East (1 0)#define South_East (1 1)#define South (1 2)#define South_West (1 3)#define West (1 4)#define North_West (1 5)#define North (1 6)#define North_East (1 7) typedef struct{ signed char x, y;} Point; const Point dir[8] ={ {0, 1}, // East {1, 1}, // South_East {1, 0}, // South {1, -1}, // South_West {0, -1}, // West {-1, -1}, // North_West {-1, 0}, // North {-1, 1} // North_East}; unsigned char within(int x, int y){ return (x = 0 y = 0 x Height y Width);} typedef struct{ int x, y; unsigned char reachable, sur, value;} MapNode; typedef struct Close{ MapNode *cur; char vis; struct Close *from; float F, G; int H;} Close; typedef struct //优先队列(Open表){ int length; //当前队列的长度 Close* Array[MaxLength]; //评价结点的指针} Open; static MapNode graph[Height][Width];static int srcX, srcY, dstX, dstY; //起始点、终点static Close close[Height][Width]; // 优先队列基本操作void initOpen(Open *q) //优先队列初始化{ q-length = 0; // 队内元素数初始为0} void push(Open *q, Close cls[Height][Width], int x, int y, float g){ //向优先队列(Open表)中添加元素 Close *t; int i, mintag; cls[x][y].G = g; //所添加节点的坐标 cls[x][y].F = cls[x][y].G + cls[x][y].H; q-Array[q-length++] = (cls[x][y]); mintag = q-length - 1; for (i = 0; i q-length - 1; i++) { if (q-Array[i]-F q-Array[mintag]-F) { mintag = i; } } t = q-Array[q-length - 1]; q-Array[q-length - 1] = q-Array[mintag]; q-Array[mintag] = t; //将评价函数值最小节点置于队头} Close* shift(Open *q){ return q-Array[--q-length];} // 地图初始化操作void initClose(Close cls[Height][Width], int sx, int sy, int dx, int dy){ // 地图Close表初始化配置 int i, j; for (i = 0; i Height; i++) { for (j = 0; j Width; j++) { cls[i][j].cur = graph[i][j]; // Close表所指节点 cls[i][j].vis = !graph[i][j].reachable; // 是否被访问 cls[i][j].from = NULL; // 所来节点 cls[i][j].G = cls[i][j].F = 0; cls[i][j].H = abs(dx - i) + abs(dy - j); // 评价函数值 } } cls[sx][sy].F = cls[sx][sy].H; //起始点评价初始值 // cls[sy][sy].G = 0; //移步花费代价值 cls[dx][dy].G = Infinity;} void initGraph(const int map[Height][Width], int sx, int sy, int dx, int dy){ //地图发生变化时重新构造地 int i, j; srcX = sx; //起点X坐标 srcY = sy; //起点Y坐标 dstX = dx; //终点X坐标 dstY = dy; //终点Y坐标 for (i = 0; i Height; i++) { for (j = 0; j Width; j++) { graph[i][j].x = i; //地图坐标X graph[i][j].y = j; //地图坐标Y graph[i][j].value = map[i][j]; graph[i][j].reachable = (graph[i][j].value == Reachable); // 节点可到达性 graph[i][j].sur = 0; //邻接节点个数 if (!graph[i][j].reachable) { continue; } if (j 0) { if (graph[i][j - 1].reachable) // left节点可以到达 { graph[i][j].sur |= West; graph[i][j - 1].sur |= East; } if (i 0) { if (graph[i - 1][j - 1].reachable graph[i - 1][j].reachable graph[i][j - 1].reachable) // up-left节点可以到达 { graph[i][j].sur |= North_West; graph[i - 1][j - 1].sur |= South_East; } } } if (i 0) { if (graph[i - 1][j].reachable) // up节点可以到达 { graph[i][j].sur |= North; graph[i - 1][j].sur |= South; } if (j Width - 1) { if (graph[i - 1][j + 1].reachable graph[i - 1][j].reachable map[i][j + 1] == Reachable) // up-right节点可以到达 { graph[i][j].sur |= North_East; graph[i - 1][j + 1].sur |= South_West; } } } } }} int bfs(){ int times = 0; int i, curX, curY, surX, surY; unsigned char f = 0, r = 1; Close *p; Close* q[MaxLength] = { close[srcX][srcY] }; initClose(close, srcX, srcY, dstX, dstY); close[srcX][srcY].vis = 1; while (r != f) { p = q[f]; f = (f + 1) % MaxLength; curX = p-cur-x; curY = p-cur-y; for (i = 0; i 8; i++) { if (! (p-cur-sur (1 i))) { continue; } surX = curX + dir[i].x; surY = curY + dir[i].y; if (! close[surX][surY].vis) { close[surX][surY].from = p; close[surX][surY].vis = 1; close[surX][surY].G = p-G + 1; q[r] = close[surX][surY]; r = (r + 1) % MaxLength; } } times++; } return times;} int astar(){ // A*算法遍历 //int times = 0; int i, curX, curY, surX, surY; float surG; Open q; //Open表 Close *p; initOpen(q); initClose(close, srcX, srcY, dstX, dstY); close[srcX][srcY].vis = 1; push(q, close, srcX, srcY, 0); while (q.length) { //times++; p = shift(q); curX = p-cur-x; curY = p-cur-y; if (!p-H) { return Sequential; } for (i = 0; i 8; i++) { if (! (p-cur-sur (1 i))) { continue; } surX = curX + dir[i].x; surY = curY + dir[i].y; if (!close[surX][surY].vis) { close[surX][surY].vis = 1; close[surX][surY].from = p; surG = p-G + sqrt((curX - surX) * (curX - surX) + (curY - surY) * (curY - surY)); push(q, close, surX, surY, surG); } } } //printf(times: %d\n, times); return NoSolution; //无结果} const int map[Height][Width] = { {0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,1,1}, {0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1}, {0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,1}, {0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,1}, {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,1,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0}, {0,0,0,0,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0}, {0,1,0,0,0,0,1,0,0,0,0,0,0,1,0,1,0,0,0,1}, {0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0}}; const char Symbol[5][3] = { □, ▓, ▽, ☆, ◎ }; void printMap(){ int i, j; for (i = 0; i Height; i++) { for (j = 0; j Width; j++) { printf(%s, Symbol[graph[i][j].value]); } puts(); } puts();} Close* getShortest(){ // 获取最短路径 int result = astar(); Close *p, *t, *q = NULL; switch(result) { case Sequential: //顺序最近 p = (close[dstX][dstY]); while (p) //转置路径 { t = p-from; p-from = q; q = p; p = t; } close[srcX][srcY].from = q-from; return (close[srcX][srcY]); case NoSolution: return NULL; } return NULL;} static Close *start;static int shortestep;int printShortest(){ Close *p; int step = 0; p = getShortest(); start = p; if (!p) { return 0; } else { while (p-from) { graph[p-cur-x][p-cur-y].value = Pass; printf((%d,%d)→\n, p-cur-x, p-cur-y); p = p-from; step++; } printf((%d,%d)\n, p-cur-x, p-cur-y); graph[srcX][srcY].value = Source; graph[dstX][dstY].value = Destination; return step; }} void clearMap(){ // Clear Map Marks of Steps Close *p = start; while (p) { graph[p-cur-x][p-cur-y].value = Reachable; p = p-from; } graph[srcX][srcY].value = map[srcX][srcY]; graph[dstX][dstY].value = map[dstX][dstY];} void printDepth(){ int i, j; for (i = 0; i Height; i++) { for (j = 0; j Width; j++) { if (map[i][j]) { printf(%s , Symbol[graph[i][j].value]); } else { printf(%2.0lf , close[i][j].G); } } puts(); } puts();} void printSur(){ int i, j; for (i = 0; i Height; i++) { for (j = 0; j Width; j++) { printf(%02x , graph[i][j].sur); } puts(); } puts();} void printH(){ int i, j; for (i = 0; i Height; i++) { for (j = 0; j Width; j++) { printf(%02d , close[i][j].H); } puts(); } puts();} int main(int argc, const char **argv){ initGraph(map, 0, 0, 0, 0); printMap(); while (scanf(%d %d %d %d, srcX, srcY, dstX, dstY) != EOF) { if (within(srcX, srcY) within(dstX, dstY)) { if (shortestep = printShortest()) { printf(从(%d,%d)到(%d,%d)的最短步数是: %d\n, srcX, srcY, dstX, dstY, shortestep); printMap(); clearMap(); bfs(); //printDepth(); puts((shortestep == close[dstX][dstY].G) ? 正确 : 错误); clearMap(); } else { printf(从(%d,%d)不可到达(%d,%d)\n, srcX, srcY, dstX, dstY); } } else { puts(输入错误!); } } return (0);}
astar安卓版怎么用
最近因为跨国问题中国大陆版禁用中也就是无法使用。
如果你想要使用,你可以从官网上下载的插件解压后打开Chrome的扩展管理(我用的是Chrome,打开上方一个打开已解压的扩展程序”然后点你的解压的astar就ok了。
AStar算法是一种静态路网中求解最短路径最有效的直接搜索方法。在包含各种障碍物的地图中,为游戏角色的移动,寻找一条到目标地点最短路径。
astar算法java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Astar算法C语言、astar算法java的信息别忘了在本站进行查找喔。