人工智能算法实现:[1]a*算法c语言

王多多 2024-05-20 16:58:40
最佳回答
1估价值与实际值越接近,估价函数取得就越好例如对于几何路网来说,可以取两节点间欧几理德距离(直线距离)做为估价值,即f=g(n)+sqrt((dx-nx)*(dx-nx)+(dy-ny)*(dy-ny));这样估价函数f在g值一定的情况下,会或多或少的受估价值h的制约,节点距目标点近,h值小,f值相对就小,能保证最短路的搜索向终点的方向进行。明显优于dijkstra算法的毫无方向的向四周搜索。conditions of heur**ticoptim**tic (must be less than or equal to the real cost)as close to the real cost as possible详细内容:创建两个表,open表保存所有已生成而未考察的节点,closed表中记录已访问过的节点。算起点的估价值;将起点放入open表;2a star算法在静态路网中的应用,以在地图中找从开始点 s 到e点的最短行走路径为例:首先定义数据结构#define mapmaxsize 100 //地图面积最大为 100x100 #define maxint 8192 //定义一个最大整数, 地图上任意两点距离不会超过它 #define stacksize 65536 //保存搜索节点的堆栈大小 #define tile_num(x,y) ((y)*map_w+(x)) //将 x,y 坐标转换为地图上块的编号 #define tile_x(n) ((n)%map_w) //由块编号得出 x,y 坐标 #define tile_y(n) ((n)/map_w) // 树结构, 比较特殊, 是从叶节点向根节点反向链接 typedef struct node *tree; struct node {int h;int tile;tree father;};typedef struct node2 *link;struct node2 {tree node;int f;link next;};link queue; // 保存没有处理的行走方法的节点 tree stack[stacksize]; // 保存已经处理过的节点 (搜索完后释放) int stacktop;char map[][6]={{'x','x','x','x','x','x'}, {'x','e',' ',' ',' ','x'}, {'x','x',' ','x',' ','x'}, {'x','x',' ',' ',' ','x'}, {'x','x','x','x','s','x'}, {'x','x','x','x','x','x'} };//地图数据 int d**_map[mapmaxsize][mapmaxsize];//保存搜索路径时,中间目标地最优解 int map_w,map_h;//地图宽和高 int start_x,start_y,end_x,end_y; //地点,终点坐标 // 路径寻找主函数 void findpath(int *path){ //printf("%d,%d,%d,%d",start_x,start_y,end_x,end_y); tree root;int i,j;stacktop=0;for (i=0;i<map_h;i++)for (j=0;j<map_w;j++)d**_map[i][j]=maxint;init_queue();root=(tree)malloc(sizeof(*root));root->tile=tile_num(start_x,start_y);root->h=0;root->father=**;enter_queue(root,judge(start_x,start_y));for (;;) {int x,y,child;tree p;root=get_from_queue();if (root==**) {*path=-1;return;}x=tile_x(root->tile);y=tile_y(root->tile);if (x==end_x && y==end_y) break;// 达到目的地成功返回 child=trytile(x,y-1,root);//尝试向上移动 child&=trytile(x,y+1,root);//尝试向下移动 child&=trytile(x-1,y,root);//尝试向左移动 child&=trytile(x+1,y,root);//尝试向右移动 if (child!=0)pop_stack();// 如果四个方向均不能移动,释放这个死节点 }// 回溯树,将求出的最佳路径保存在 path[] 中 for (i=0;root;i++) {path[i]=root->tile;root=root->father;//printf("path** %d",path[i]);}path[i]=-1;freetree();}// 估价函数,估价 x,y 到目的地的距离,估计值必须保证比实际值小 int judge(int x,int y){ int d**tance;d**tance=abs(end_x-x)+abs(end_y-y);return d**tance;}// 尝试下一步移动到 x,y 可行否 int trytile(int x,int y,tree father){tree p=father;int h;if (map[y][x]=='x') return 1; // 如果 (x,y) 处是障碍,失败 while (p) {if (x==tile_x(p->tile) && y==tile_y(p->tile)) return 1; //如果 (x,y) 曾经经过,失败 p=p->father;}h=father->h+1;if (h>=d**_map[y][x]) return 1;// 如果曾经有更好的方案移动到 (x,y) 失败 d**_map[y][x]=h;// 记录这次到 (x,y) 的距离为历史最佳距离 // 将这步方案记入待处理队列 p=(tree)malloc(sizeof(*p));p->father=father;p->h=father->h+1;p->tile=tile_num(x,y);enter_queue(p,p->h+judge(x,y));return 0;}3打开c语言编译器,输入我们的运行代码,编译,运行如下,打印出地图如下图:4点任意键进行运行找静态路网5说明:找到路后会存到一个数组中去,我们为了显示这个过程可以运用打印函数打印出来代码如下void printpath(int *path){int i;//printf("-44444444444444");for (i=0;path[i]>=0;i++) {gotoxy(tile_x(path[i])+1,tile_y(path[i])+1);printf("-");sleep(2000); }printf("\n");printf("\n");printf("走迷宫完成");}6整个程序的代码如下:#include<windows.h>#include"stdio.h"#include<conio.h>#include"assert.h" #include"stdlib.h"#define mapmaxsize 100 //地图面积最大为 100x100 #define maxint 8192 //定义一个最大整数, 地图上任意两点距离不会超过它 #define stacksize 65536 //保存搜索节点的堆栈大小 #define tile_num(x,y) ((y)*map_w+(x)) //将 x,y 坐标转换为地图上块的编号 #define tile_x(n) ((n)%map_w) //由块编号得出 x,y 坐标 #define tile_y(n) ((n)/map_w) // 树结构, 比较特殊, 是从叶节点向根节点反向链接 typedef struct node *tree; struct/*designde by 1wangxiaobo@163.com*/ node {int h;int tile;tree father;};typedef struct /*designde by 1wangxiaobo@163.com*/node2 *link;struct node2 {tree node;int f;/*designde by 1wangxiaobo@163.com*/link next;};link queue; // 保存没有处理的行走方法的节点 tree stack[stacksize]; // 保存已经处理过的节点 (搜索完后释放) int stacktop;char map[][6]={{'x','x','x','x','x','x'}, {'x','e',' ',' ',' ','x'}, {'x','x',' ','x',' ','x'}, {'x','x',' ',' ',' ','x'}, {'x','x','x','x','s','x'}, {'x','x','x','x','x','x'} };//地图数据 int d**_map/*designde by 1wangxiaobo@163.com*/[mapmaxsize][mapmaxsize];//保存搜索路径时,中间目标地最优解 int map_w,map_h;//地图宽和高 int start_x,start_y,end_x,end_y; //地点,终点坐标 void gotoxy(int x ,int y){handle a;coord zb;zb.x =x-1;zb.y =y-1;a= getstdhandle(std_output_handle/*designde by 1wangxiaobo@163.com*/);setconsolecursorposition(a,zb);}// 初始化队列 void init_queue(){ queue=(link)malloc(sizeof(*queue));queue->node=**;queue->f=-1;queue->next=(link)/*designde by 1wangxiaobo@163.com*/malloc(sizeof(*queue));queue->next->f=maxint;queue->next->node=**;queue->next->next=**;} // 待处理节点入队列, 依靠对目的地估价距离插入排序 void enter_queue(tree node,int f){ link p=queue,father,q;while(f>p->f) {father=p;p=p->next/*designde by 1wangxiaobo@163.com*/;assert(p);} q=(link)malloc(sizeof(*q));assert(queue);q->f=f,q->node=node,q->next=p;father->next=q;} // 将离目的地估计最近的方案出队列 tree get_from_queue(){ tree bestchoice=queue->next->node;link next=queue->next->next;/*designde by 1wangxiaobo@163.com*/free(queue->next);queue->next=next;stack[stacktop++]=bestchoice;assert(stacktop<stacksize);return /*designde by 1wangxiaobo@163.com*/bestchoice;} // 释放栈顶节点 void pop_stack(){ free(stack[--stacktop]);}// 释放申请过的所有节点 void freetree(){ int i;link p;for (i=0;i<stacktop;i++)free(stack);while /*designde by 1wangxiaobo@163.com*/(queue) {p=queue;free(p->node);queue=queue->next;free(p);} } // 估价函数,估价 x,y 到目的地的距离,估计值必须保证比实际值小 int judge(int x,int y){ int d**tance;d**tance=abs(end_x-x)+abs(end_y-y);return d**tance;}// 尝试下一步移动到 x,y 可行否 int trytile(int/*designde by 1wangxiaobo@163.com*/ x,int y,tree father){tree p=father;int h;if (map[y][x]=='x') return 1; // 如果 (x,y) 处是障碍,失败 while (p) {/*designde by 1wangxiaobo@163.com*/if (x==tile_x(p->tile) && y==tile_y(p->tile)) return 1; //如果 (x,y) 曾经经过,失败 p=p->father;}h=father->h+1;if (h>=d**_map[y][x]) return 1;// 如果曾经有更好的方案移动到 (x,y) 失败 d**_map[y][x]=h;// 记录这次到 (x,y) 的距离为历史最佳距离 // 将这步方案记入待处理队列 p=(tree)malloc(sizeof(*p));p->father=father;p->h=father->h+1;p->tile=tile_num(x,y);enter_queue(p,p->h+judge(x,y));return 0;}// 路径寻找主函数 void findpath(int *path){ //printf("%d,%d,%d,%d",start_x,start_y,end_x,end_y); tree root;int i,j;stacktop=0;for (i=0;i<map_h;i++)for (j=0;j<map_w;j++)d**_map[i][j/*designde by 1wangxiaobo@163.com*/]=maxint;init_queue();root=(tree)malloc(sizeof(*root));root->tile=tile_num(start_x,start_y);root->h=0;root->father=**;enter_queue(root,judge(start_x,start_y));for (;;) {int x,y,child;tree p;root=get_from_queue();if (root==**) {*path=-1;return;}x=tile_x(root->tile);y=tile_y(root->tile);if (x==end_x && y==end_y) break;// 达到目的地成功返回 child=trytile(x,y-1,root);//尝试向上移动 child&=trytile(x,y+1,root);//尝试向下移动 child&=trytile(x-1,y,root);//尝试向左移动 child&=trytile(x+1,y,root);//尝试向右移动 if (child!=0)pop_stack();// 如果四个方向均不能移动,释放这个死节点 }// 回溯树,将求出的最佳路径保存在 path[] 中 for (i=0;root;i++) {path[i]=root->tile;root=root->father;//printf("path** %d",path[i]/*designde by 1wangxiaobo@163.com*/);}path[i]=-1;freetree();}void printpath(int *path){int i;//printf("-44444444444444");for (i=0;path[i]>=0;i++) {gotoxy(tile_x(path[i])+1,tile_y(path[i])+1);printf("-");sleep(2000); }printf("\n");printf("\n");printf("走迷宫完成");}void readmap(){ printf("走迷宫,s是起始点 e是终点 按任意键开始");getchar();//file *f;int i,j;//f=fopen("2.c","r");//assert(f);//scanf("%d%d",&map_w,&map_h);map_w=map_h=6;for (i=0;i<map_h;i++)//fgets(&map[i][0],map_w+1,f);//fclose(f);start_x=-1,end_x=-1;for (i=0;i<map_h;i++)for (j=0;j/*designde by 1wangxiaobo@163.com*/<map_w;j++) {if (map[i][j]=='s') map[i][j]=' ',start_x=j,start_y=i;if (map[i][j]=='e') map[i][j]=' ',end_x=j,end_y=i;}assert(start_x>=0 && end_x>=0);//printf("%d,%d,%d,%d",start_x,start_y,end_x,end_y);}void showmap(){int i,j;system("cls");for (i=0;i<map_h;i++) {gotoxy(1,i+1);for (j=0;j<map_w;j++)if (map[i][j]!=' ') printf("x");else printf(" ");}gotoxy(end_x+1,end_y+1);printf("e");gotoxy(start_x+1,start_y+1);printf("s");}int main(){ system("title 晓博 a*算法试验程序");//设置cmd窗口标题 printf("…………欢迎使用晓博 a*算法试验程序,designed by 1wangxiaobo@163.com 河南财经政法大学…………");int path[maxint];readmap();showmap();sleep(2000);findpath(path);printpath(path);getchar();system("pause");return 0;}end 20210311
汇率兑换计算器

类似问答
  • 编程实现银行家算法?
    • 2024-05-20 11:56:39
    • 提问者: 未知
    void share()//利用银行家算法对申请资源对进行判定 { char ch; int i=//函数实现 void init(){ int n,a,i,j; //可利用的资源数目及其种类
  • c语言a
    • 2024-05-20 03:39:06
    • 提问者: 未知
    while(a这里的a<b<c求的是一个逻辑值,正确为1,错误为0,a=1,b=2,c=2,首先a成立,逻辑值为1,然后1<c,也成立,所以满足循环条件(逻辑值为0时不满足,为1满足),开始...
  • 既然有 c 语言,那么 a 语言和 b 语言分别是什么?
    • 2024-05-20 01:24:59
    • 提问者: 未知
    1941 年,konrad zuse 展出德国**赞助的可编程的通用型计算机 z3。1942 年,konrad zuse 开始研发 z4。1943 年,...ibm 的 edgar f.codd 提出 关系模型(relational model)。...
  • 1+2+3+100用c语言怎么编?
    • 2024-05-20 18:35:31
    • 提问者: 未知
    很简单的题~如果没理解错的话就是这个#include<>void main(){int i,sum=0;for(i=1;i<=100;i++){sum+=i;}printf("sum=%d",sum);}输出:sum=5050
  • c语言
    • 2024-05-20 17:40:20
    • 提问者: 未知
    第一个v3 和第二个v3指的都是“v3=v1*16+v2;得到的v3 printf("十六进制是:ox%x,十进制是:%d\n",v3,v3);这里,%x是以十六进制数出v3;而%d是以十进制数出v3
  • 在c语言中如何用标准宏定义实现计算两个参数中的最小值
    • 2024-05-20 07:54:59
    • 提问者: 未知
    可以通目运算符(? :)实现,这样定义,#define min(x,y)((x)<(y)?(x):(y)),测试代码如下,//程序功能实现,计算两个数的最小值#include <stdio.h>#define min(x,y)((x)<(y)?(x):(y))int main(int argc, char *argv[]){int x=10,y=15;printf(&quo...
  • c语言 线性表的实现
    • 2024-05-20 21:51:47
    • 提问者: 未知
    typedef int datatype;define maxsize 1024 typedef struct { elemtype data[maxsize];int length;}sql**t;void initl**t(sql**t&l) { l.length=0;}...
  • c语言 计算fibonacci数列?
    • 2024-05-20 21:14:29
    • 提问者: 未知
    #include<stdio.h>//使用递归求斐波拉切数列的某一项的值int fib(int num){if(num==1||num==2){return 1;}else{return fib(num-1)+fib(num-2);}}void main(){int i; //斐波拉切数列的项数for(i=1;i<=18;i++){if(i%6!=0){ //输出六项,然后再换...
  • 大智慧公式提示语法错误---》错误 line 1:c1001 :语法错误:","a:=drawline(low
    • 2024-05-20 12:53:21
    • 提问者: 未知
    drawline为通达信特有的未来函数,大智慧中无法替代.放弃吧!
  • c语言提供的合法**是
    • 2024-05-20 14:36:28
    • 提问者: 未知
    由ansi标准定义c语言**共32个 : auto double int struct break else long switch case enum reg**ter typedef char extern return union const float short unsigned continue for signed void default goto sizeof vola...
汇率兑换计算器

热门推荐
热门问答
最新问答
推荐问答
新手帮助
常见问题
房贷计算器-九子财经 | 备案号: 桂ICP备19010581号-1 商务联系 企鹅:2790-680461

特别声明:本网为公益网站,人人都可发布,所有内容为会员自行上传发布",本站不承担任何法律责任,如内容有该作者著作权或违规内容,请联系我们清空删除。