1.问题描述
笔者在用C++语言写A*算法代码的上机实验课时,发现明明引用了正确的头文件
#include<graphics.h>,为什么其中该有的函数在调用时依然报未声明/定义的错误。
通过搜索引擎发现,该图形库需要额外下载才能正常使用。
2.问题解决
2.1EasyX下载
链接:https://codebus.cn/f/a/0/0/488/easyx4mingw_20240601.zip
2.2文件迁移
现假设你下载的dev c++的路径为E:\Dev-Cpp
1.将下载好的 easyx4mingw_20240601.zip解压缩。
2.将 include 文件夹下的 easyx.h 和 graphics.h 拷贝到 E:\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include 文件夹里。
3.将 lib64\libeasyx.a 拷贝到 E:\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib 文件夹里。
4.将 lib32\libeasyx.a 拷贝到 E:\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib32 文件夹里。
至此迁移完成。
2.3参数配置
在dev中新建空白项目、写代码、保存、编译......(这里省略)
在导航栏中寻找:项目->项目属性
随后:参数->链接
复制 -leasyx输入进去,点击确定
3.代码测试
这是我调整后的可视化A*算法代码样例展示
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<graphics.h>
#include<cstdlib>
#include<ctime>
#include<unordered_set>
using namespace std;
const int WIDTH_BLOCK_NUM = 20; //格子宽
const int HEIGHT_BLOCK_NUM = 20; //格子高
const int WINDOWS_WIDTH = 800; //屏幕宽
const int WINDOWS_HEIGHT = 800; //屏幕高
const int MAX_MARK = 20; //图格子最大值
const int MIN_MARK = 0; //图格子最小值
int dir_x[]={1,1,1,0,0,-1,-1,-1};
int dir_y[]={1,-1,0,1,-1,1,-1,0};
struct Block{
public:
Block(){}
Block(int x, int y, int f, int g){
this->x=x;
this->y=y;
this->f=f;
this->g=g;
}
friend bool operator<(Block a, Block b) {
return a.f+a.g>b.f+b.g;
}
int x,y,f,g;
};
priority_queue<Block>pq;
vector<Block>vec;
vector<Block>local_vec;
unordered_set<int>us; //记录哪个点
Block start_block,end_block;
int randInt(int left, int right){
return rand()%(right-left+1)+left;
}
void init(){
start_block.x=randInt(MIN_MARK,MAX_MARK); //随机生成出起始和截止点的坐标
start_block.y=randInt(MIN_MARK,MAX_MARK);
end_block.x=randInt(MIN_MARK, MAX_MARK);
end_block.y=randInt(MIN_MARK, MAX_MARK);
while(start_block.x==end_block.x||start_block.y==end_block.y){
end_block.x=randInt(MIN_MARK,MAX_MARK);
end_block.y=randInt(MIN_MARK,MAX_MARK);
}
}
int position2mark(int x, int y){
return x+y*WIDTH_BLOCK_NUM;
}
bool is_valid(int x,int y){
if (x<0||y<0||x>=WIDTH_BLOCK_NUM||y>=HEIGHT_BLOCK_NUM){
return false;
}else return true;
}
void position2rectangle(struct Block&b,int rect[][2]){
int width=WINDOWS_WIDTH/WIDTH_BLOCK_NUM;
int height=WINDOWS_HEIGHT/HEIGHT_BLOCK_NUM;
int temp_x=b.x*width;
int temp_y=b.y*height;
rect[0][0]=temp_x;
rect[0][1]=temp_y;
rect[1][0]=temp_x + width;
rect[1][1]=temp_y + height;
}
void draw(){
int width=WINDOWS_WIDTH / WIDTH_BLOCK_NUM;
int height=WINDOWS_HEIGHT / HEIGHT_BLOCK_NUM;
for(int i=0;i<WINDOWS_WIDTH;i+=width){
line(i,0,i,WINDOWS_HEIGHT);
}
for(int i=0;i<WINDOWS_HEIGHT;i+=height){
line(0,i,WINDOWS_WIDTH,i);
}
int arr[2][2];
COLORREF prev_color=getfillcolor();
setfillcolor(BLUE);
for(Block b:local_vec){
position2rectangle(b,arr);
fillrectangle(arr[0][0],arr[0][1],arr[1][0],arr[1][1]);
}
setfillcolor(RED);
for(Block b:vec){
position2rectangle(b,arr);
fillrectangle(arr[0][0],arr[0][1],arr[1][0],arr[1][1]);
}
setfillcolor(GREEN);
position2rectangle(start_block,arr);
fillrectangle(arr[0][0],arr[0][1],arr[1][0],arr[1][1]);
position2rectangle(end_block,arr);
fillrectangle(arr[0][0],arr[0][1],arr[1][0],arr[1][1]);
setfillcolor(prev_color);
Sleep(1000);
}
int main(){
srand(unsigned(time(NULL)));
init();
initgraph(WINDOWS_WIDTH,WINDOWS_HEIGHT);
Block temp=start_block;
pq.push(temp); //起点入队
us.insert(position2mark(temp.x, temp.y));
while(!(temp.x==end_block.x&&temp.y==end_block.y)){
temp=pq.top();
pq.pop();
vec.push_back(temp);
for(int i=0;i<8; ++i){ //枚举8个方向
int temp_x=temp.x+dir_x[i];
int temp_y=temp.y+dir_y[i];
if(is_valid(temp_x,temp_y)&&!us.count(position2mark(temp_x,temp_y))){ //没越界且为第一次到达
Block b;
b.x=temp_x;
b.y=temp_y;
b.f=temp.f+(abs(dir_x[i])+abs(dir_y[i]))>1?14:10;
b.g=(abs(temp_x-end_block.x)+abs(temp_y-end_block.y))*10;
pq.push(b);
us.insert(position2mark(temp_x,temp_y));
local_vec.push_back(b);
}
}
draw();
}
closegraph();
return 0;
}
样例运行结果展示:
4.参考:在 CLion、Dev-C++ 或 Code::Blocks 下面配置 EasyX(2024-6-1 更新) - CodeBus