等高线算法(C语言粗糙版) visualxyk 发表于 2007-6-8 14:19:00等高线算法用来在地图寻找最短路径与最优路径的算法.它通过像地图中的等高线一样从终点向四周扩散开来,一直扩散到开始点为止.则从开始点开始,沿着"高度"逐渐降低的路径,一直到最"低谷",就是从起点到终点的最短路径.它可以绕开障碍物,并且通过加权运算的话,可以考虑到各种地形的影响.源代码如下:#i nclude <stdio.h>#i nclude <stdlib.h>#define MAP_WIDTH 10#define MAP_HEIGHT 10#define MAP_SIZE (MAP_WIDTH * MAP_HEIGHT)int map_data[MAP_SIZE] = { 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,0,1,1,1,1, 1,1,1,1,1,0,1,1,1,1, 1,1,1,1,1,0,0,0,0,1, 1,1,0,1,1,0,1,1,1,1, 1,1,0,1,0,0,1,0,0,0, 1,1,0,1,0,1,1,1,1,1, 1,1,1,0,1,1,1,1,1,1, 1,1,1,0,1,1,1,1,1,1, 1,1,1,0,1,1,1,1,1,1};int temp_data[MAP_SIZE] = {0};void set_marker(int pos, int step_no, int target_pos);int get_path(int start_pos, int targetd_pos, int *path);int get_step(int pos);void set_marker(int pos, int step_no, int target_pos, bool &Found){ int up = pos - MAP_WIDTH, down = pos + MAP_WIDTH, left = pos - 1, right = pos + 1; if (up >= 0) if (map_data[up] != 0) if (temp_data[up] == 0 || temp_data[up] > step_no) { temp_data[up] = step_no; if (up == target_pos) Found = true; } if (down < MAP_SIZE) if (map_data[down] != 0) if (temp_data[down] == 0 || temp_data[down] > step_no) { temp_data[down] = step_no; if (down == target_pos) Found = true; } if (pos%MAP_WIDTH != 0) if (map_data[left] != 0) if (temp_data[left] == 0 || temp_data[down] > step_no) { temp_data[left] = step_no; if (left == target_pos) Found = true; } if (right%MAP_WIDTH != 0) if (map_data[right] != 0) if (temp_data[right] == 0 || temp_data[right] > step_no) { temp_data[right] = step_no; if (right == target_pos) Found = true; }}int get_step(int pos){ int temp_pos = pos - MAP_WIDTH; if (temp_pos >= 0) if (map_data[temp_pos] != 0) if (temp_data[temp_pos] != 0 && temp_data[temp_pos] < temp_data[pos]) return temp_pos; temp_pos = pos + MAP_WIDTH; if (temp_pos < MAP_SIZE) if (map_data[temp_pos] != 0) if (temp_data[temp_pos] != 0 && temp_data[temp_pos] < temp_data[pos]) return temp_pos; temp_pos = pos - 1; if (pos%MAP_WIDTH != 0) if (map_data[temp_pos] != 0) if (temp_data[temp_pos] != 0 && temp_data[temp_pos] < temp_data[pos]) return temp_pos; temp_pos = pos + 1; if (temp_pos%MAP_WIDTH != 0) if (map_data[temp_pos] != 0) if (temp_data[temp_pos] != 0 && temp_data[temp_pos] < temp_data[pos]) return temp_pos; return -1;}int get_path(int start_pos, int target_pos, int *path){ int index = 0; path[index] = get_step(start_pos); index++; while (true) { path[index] = get_step(path[index-1]); if (path[index] == target_pos) break; index++; } return ++index;} void show_map(){ int i, j; for (i=0; i<MAP_HEIGHT; i++) { for (j=0; j<MAP_WIDTH; j++) printf(" %02d", map_data[i * MAP_WIDTH + j]); printf("\n"); }}void show_temp(){ int i, j; for (i=0; i<MAP_HEIGHT; i++) { for (j=0; j<MAP_WIDTH; j++) printf(" %02d", temp_data[i * MAP_WIDTH + j]); printf("\n"); }}void main(){ printf("map data:\n"); show_map(); printf("\ntemp_data:\n"); show_temp(); int i, j, index; int step = 1; int start_pos = 11, end_pos = 88; temp_data[end_pos] = step; bool Found = false; do { for (i=0; i<MAP_HEIGHT; i++) for (j=0; j<MAP_WIDTH; j++) { index = i * MAP_WIDTH + j; if (temp_data[index] == step) set_marker(index, step+1, start_pos, Found); } step++; }while (!Found); printf("after setting markers...\n"); show_temp(); getchar(); int path[30]; int counter = get_path(start_pos, end_pos, path); for (i = 0; i<counter; i++) map_data[path] = 99; printf("The road is like that...\n"); show_map(); getchar();}这是第一个版本,在其中,设置标志部分用了强制的整张地图搜索,效率很低,可以用两个容器来解决上面问题.这将在下一个版本提到.
[人工智能与算法]等高线算法(C语言粗糙版)
来源:互联网 发布日期:2011-09-16 11:54:01 浏览:8508次
导读: 等高线算法(C语言粗糙版) visualxyk 发表于 2007-6-8 14:19:00等高线算法用来在地图寻找最短路径与最优路径的算法.它通过像地图中的等高线一样从终点向四周扩散开来,一直扩散到开始点为止.则从开始点开始,沿着高度逐渐降低的路径,一直到最低谷,就是从起点到...
下一篇:迷宫寻路与人工智能(一)
相关内容
AiLab云推荐
最新资讯
本月热点
热门排行
-
华为5G-A实现规模组网,下载速度超过4Gbps,比5G快10倍
阅读量:56297
-
最坏时期已过,美国5G市场即将迎来回暖?
阅读量:56079
-
回望2023年5G发展:释放和提升价值成最大课题
阅读量:13077
-
全球吹响5G-A商用号角
阅读量:12143
-
5.5G商用来了!多了半个G,和你我有啥关系?
阅读量:6200
-
从5G迈向5G-A,运营商的增长新机会在哪?| MWC 2024
阅读量:6038
推荐内容
- 2024冻立方中国(厦门)冷冻冷藏食品展
- 2024第二十一届中国畜牧业展览会(畜博会CAHE)
- 2024第十三届中国(西安)糖酒食品交易会(西安糖酒会)
- 2024西部乡村振兴博览会暨农业机械装备展
- 2024年第28届伊朗国际石油、天然气、炼油和石化展览会
- 中东石油展 2024年第28届伊朗国际石油展
- 2024重庆国际餐饮(火锅)食材展览会
- 2024深圳国际渔业博览会
- 2024华晨第十一届中国(山东)秋季国际宠物水族展
- 2024中亚一带一路工博会
- 2024第七届雄安城市建设及绿色建筑博览会 第七届雄安装配式建筑及绿色建材展览会 2024中国绿色建筑与智能建造科技大会 第五届雄安金桥梁项目对接会
- 2024第七届中国(新疆)国际智能交通产业博览会
- 2024越南(河内)国际五金及工具展览会
- 2024山东连锁加盟展览会 开年山东首展
- 2024武汉国际电子元器件、材料及生产设备展览会
- 2024山东(济南)连锁加盟博览会助力行业发展
- CRFE国际连锁加盟展览会 -山东招商加盟专业展
- 2024第五届深圳国际老龄博览会
- 2024年山西国际水展览会
- 2024哈尔滨东北亚国际口腔设备及器材展览会
- 2024第二十一届中国(漯河)食品博览会
- 2024第十届中国(北京)国际警用装备及反恐技术装备展览会暨学术研讨会 (CIPATE)
- 2024孟加拉国际暖通、空调制冷、净化、楼宇自控展