⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 在VC6.0下通过测试的A*算法源码
💻 CPP
字号:
#include "Asuanfa.h"
#include <conio.h> 
#include <stdlib.h> 
#include <ctype.h> 
#include <stdio.h> 

char map[100][100]; 
int maph,mapw; 

static short incx[8]={ 0, 1, 1, 1, 0, -1, -1, -1 }; 
static short incy[8]={ -1, -1, 0, 1, 1, 1, 0, -1 }; 

AstarPathFind PathFind; 
int startx,starty,endx,endy; 
short Judge(short x,short y,short endx,short endy) 
{ 
short dx=abs(x-endx); 
short dy=abs(y-endy); 
return (dx+dy)<<3; 
} 
char Possible(short x,short y) 
{ 
if (x<0||y<0||x>=mapw||y>=maph) return 0; 
return (map[y][x]==' ')?1:0; 
} 
int readmap() 
{ 
int i,j; 
FILE *f; 
if ((f=fopen("map.dat","r"))==NULL) return -1; 
fscanf(f,"%d,%d\n",&mapw,&maph); 
for (j=0;j<maph;j++) fgets(map[j],mapw+2,f); 
fclose(f); 
for (j=0;j<maph;j++) for (i=0;i<mapw;i++) 
{ 
if (map[j][i]=='s'||map[j][i]=='S') startx=i,starty=j/*,map[j][i]=' '*/; 
if (map[j][i]=='e'||map[j][i]=='E') endx=i,endy=j/*,map[j][i]=' '*/; 
} 
return 0; 
} 

int main(void) 
{ 
int i,j; 
int x,y; 
if (readmap()) { printf("no map file\n"); return -1; } 
short PosXY[500]; 
char PosR[500]; 
for (j=0;j<maph;j++) { 
for (i=0;i<mapw;i++) putchar(map[j][i]); 
putchar('\n'); 
} 
i=PathFind.Create(mapw,maph,Possible,Judge); 
if (i) { 
printf("创建失败\n"); 
return -1; 
} 
PathFind.SetCloseLimited(79*24L); 
PathFind.SetDirMask(0xff); 
PathFind.FindPath(startx,starty,endx,endy); 
PathFind.GetPosPath(PosXY,250); 
PathFind.GetDirPath(PosR,500); 
getch(); 
for (i=1;PosXY[i*2]>=0;i++) 
{ 
x=PosXY[i*2]; 
y=PosXY[i*2+1]; 
map[y][x]='.'; 
} 
for (j=0;j<maph;j++) { 
for (i=0;i<mapw;i++) 
{ 
putchar(map[j][i]); 
if (map[j][i]=='.') map[j][i]=' '; 
} 
putchar('\n'); 
} 
PathFind.Release(); 
return 0; 
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -