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

📄 unit1.~cpp

📁 A*算法的优化范例
💻 ~CPP
📖 第 1 页 / 共 2 页
字号:
            ShowMapPath(path, map);
            MapPathShowExist = 1;
        }
        SetCost(path);
    }
    if(idxALG == 2){
        clsMAP8dB map;
        map.InputMapFile(mapFILENAME.c_str());
        clsPathFinder8dB PathFinder;
        pgbMAP->Position = 60;
        //开启寻路
        time_cost = (unsigned long)(timeGetTime());
        path = PathFinder.FindPath(map);
        time_cost = (unsigned long)(timeGetTime()) - time_cost;
        pgbMAP->Position = 80;
        stbMAP->Panels[0][2]->Text = IntToStr(time_cost) + "ms";
        if(path.size()){
            pgbMAP->Position = 100;
            ShowMapPath(path, map);
            MapPathShowExist = 1;
        }
        SetCost(path);
    }
    pgbMAP->Position = 0;
}
//---------------------------------------------------------------------------
// 名称:SetCost
// 功能:设置状态栏上的Cost
//---------------------------------------------------------------------------
void TfrmMain::SetCost(typePATH path)
{
    unsigned int i;
    float cost = 0;
    for(i = 0; i < path.size(); i++){
        if((path[i] == 1) || (path[i] == 3) || (path[i] == 5) || (path[i] == 7))
            cost += VALUE_oblique_direction;
        if((path[i] == 2) || (path[i] == 4) || (path[i] == 6) || (path[i] == 8))
            cost += VALUE_straight_direction;
    }
    stbMAP->Panels[0][3]->Text = "Cost=" + FloatToStr(cost / 10);
}
//---------------------------------------------------------------------------
// 名称:ShowMapPath
// 功能:在地图上显示路径 0通行 1障碍 2开启 3关闭 4起点 5终点 -1路径
//---------------------------------------------------------------------------
void TfrmMain::ShowMapPath(typePATH path, clsMAP &map)
{
    clsNODE *now_p = map.start_point;
    unsigned int i, j;
    vector<vector<clsNODE *> > show_map_data;
    show_map_data = map.mapData;
    //将路径加入临时地图
    show_map_data[now_p->x][now_p->y]->Flag = 4;
    for(i = 0; i < path.size(); i++){
        switch(path[i]){
		case 1 :
			now_p = show_map_data[now_p->x - 1][now_p->y + 1];
			break;
		case 2 :
			now_p = show_map_data[now_p->x][now_p->y + 1];
			break;
		case 3 :
			now_p = show_map_data[now_p->x + 1][now_p->y + 1];
			break;
		case 4 :
			now_p = show_map_data[now_p->x - 1][now_p->y];
			break;
		case 6 :
			now_p = show_map_data[now_p->x + 1][now_p->y];
			break;
		case 7 :
			now_p = show_map_data[now_p->x - 1][now_p->y - 1];
			break;
		case 8 :
			now_p = show_map_data[now_p->x][now_p->y - 1];
			break;
		case 9 :
			now_p = show_map_data[now_p->x + 1][now_p->y - 1];
			break;
        }
        show_map_data[now_p->x][now_p->y]->Flag = -1;
    }
    show_map_data[map.finish_point->x][map.finish_point->y]->Flag = 5;
    pgbMAP->Position = 90;
    int x, y;
    for(x = 0; x < 50; x++)
        for(y = 0; y < 50; y++)
            switch(show_map_data[x][y]->Flag){
            case -1 :
                setBrush(x * 10 + 5,y * 10 + 5, 5);
                imgMAP->Canvas->Rectangle(rectBrush);
                break;
            case 1 :
                setBrush(x * 10 + 5,y * 10 + 5, 1);
                imgMAP->Canvas->Rectangle(rectBrush);
                break;
            case 4 :
                setBrush(x * 10 + 5,y * 10 + 5, 3);
                imgMAP->Canvas->Rectangle(rectBrush);
                break;
            case 5 :
                setBrush(x * 10 + 5,y * 10 + 5, 4);
                imgMAP->Canvas->Rectangle(rectBrush);
                break;
            default :
                setBrush(x * 10 + 5,y * 10 + 5, 2);
                imgMAP->Canvas->Rectangle(rectBrush);
            }
    pgbMAP->Position = 100;
}
//---------------------------------------------------------------------------
// 名称:ShowMapPath
// 功能:在地图上显示路径 0通行 1障碍 2开启 3关闭 4起点 5终点 -1路径
//---------------------------------------------------------------------------
void TfrmMain::ShowMapPath(typePATH path, clsMAP4d &map)
{
    clsNODE *now_p = map.start_point;
    unsigned int i, j;
    vector<vector<clsNODE *> > show_map_data;
    show_map_data = map.mapData;
    //将路径加入临时地图
    show_map_data[now_p->x][now_p->y]->Flag = 4;
    for(i = 0; i < path.size(); i++){
        switch(path[i]){
		case 1 :
			now_p = show_map_data[now_p->x - 1][now_p->y + 1];
			break;
		case 2 :
			now_p = show_map_data[now_p->x][now_p->y + 1];
			break;
		case 3 :
			now_p = show_map_data[now_p->x + 1][now_p->y + 1];
			break;
		case 4 :
			now_p = show_map_data[now_p->x - 1][now_p->y];
			break;
		case 6 :
			now_p = show_map_data[now_p->x + 1][now_p->y];
			break;
		case 7 :
			now_p = show_map_data[now_p->x - 1][now_p->y - 1];
			break;
		case 8 :
			now_p = show_map_data[now_p->x][now_p->y - 1];
			break;
		case 9 :
			now_p = show_map_data[now_p->x + 1][now_p->y - 1];
			break;
        }
        show_map_data[now_p->x][now_p->y]->Flag = -1;
    }
    show_map_data[map.finish_point->x][map.finish_point->y]->Flag = 5;
    pgbMAP->Position = 90;
    int x, y;
    for(x = 0; x < 50; x++)
        for(y = 0; y < 50; y++)
            switch(show_map_data[x][y]->Flag){
            case -1 :
                setBrush(x * 10 + 5,y * 10 + 5, 5);
                imgMAP->Canvas->Rectangle(rectBrush);
                break;
            case 1 :
                setBrush(x * 10 + 5,y * 10 + 5, 1);
                imgMAP->Canvas->Rectangle(rectBrush);
                break;
            case 4 :
                setBrush(x * 10 + 5,y * 10 + 5, 3);
                imgMAP->Canvas->Rectangle(rectBrush);
                break;
            case 5 :
                setBrush(x * 10 + 5,y * 10 + 5, 4);
                imgMAP->Canvas->Rectangle(rectBrush);
                break;
            default :
                setBrush(x * 10 + 5,y * 10 + 5, 2);
                imgMAP->Canvas->Rectangle(rectBrush);
            }
    pgbMAP->Position = 100;
}
//---------------------------------------------------------------------------
// 名称:ShowMapPath
// 功能:在地图上显示路径 0通行 1障碍 2开启 3关闭 4起点 5终点 -1路径
//---------------------------------------------------------------------------
void TfrmMain::ShowMapPath(typePATH path, clsMAP8dB &map)
{
    clsNODE *now_p = map.start_point;
    unsigned int i, j;
    vector<vector<clsNODE *> > show_map_data;
    show_map_data = map.mapData;
    //将路径加入临时地图
    show_map_data[now_p->x][now_p->y]->Flag = 4;
    for(i = 0; i < path.size(); i++){
        switch(path[i]){
		case 1 :
			now_p = show_map_data[now_p->x - 1][now_p->y + 1];
			break;
		case 2 :
			now_p = show_map_data[now_p->x][now_p->y + 1];
			break;
		case 3 :
			now_p = show_map_data[now_p->x + 1][now_p->y + 1];
			break;
		case 4 :
			now_p = show_map_data[now_p->x - 1][now_p->y];
			break;
		case 6 :
			now_p = show_map_data[now_p->x + 1][now_p->y];
			break;
		case 7 :
			now_p = show_map_data[now_p->x - 1][now_p->y - 1];
			break;
		case 8 :
			now_p = show_map_data[now_p->x][now_p->y - 1];
			break;
		case 9 :
			now_p = show_map_data[now_p->x + 1][now_p->y - 1];
			break;
        }
        show_map_data[now_p->x][now_p->y]->Flag = -1;
    }
    show_map_data[map.finish_point->x][map.finish_point->y]->Flag = 5;
    pgbMAP->Position = 90;
    int x, y;
    for(x = 0; x < 50; x++)
        for(y = 0; y < 50; y++)
            switch(show_map_data[x][y]->Flag){
            case -1 :
                setBrush(x * 10 + 5,y * 10 + 5, 5);
                imgMAP->Canvas->Rectangle(rectBrush);
                break;
            case 1 :
                setBrush(x * 10 + 5,y * 10 + 5, 1);
                imgMAP->Canvas->Rectangle(rectBrush);
                break;
            case 4 :
                setBrush(x * 10 + 5,y * 10 + 5, 3);
                imgMAP->Canvas->Rectangle(rectBrush);
                break;
            case 5 :
                setBrush(x * 10 + 5,y * 10 + 5, 4);
                imgMAP->Canvas->Rectangle(rectBrush);
                break;
            default :
                setBrush(x * 10 + 5,y * 10 + 5, 2);
                imgMAP->Canvas->Rectangle(rectBrush);
            }
    pgbMAP->Position = 100;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnOpenMapClick(TObject *Sender)
{
    if(dlgOpenMap->Execute()){
        OpenMAP(dlgOpenMap->FileName);
        pgbMAP->Position =  0;
    }

}
//---------------------------------------------------------------------------
// 名称:OpenMAP
// 功能:打开地图文件
//---------------------------------------------------------------------------
void TfrmMain::OpenMAP(AnsiString mapFILENAME)
{
    TStringList *strList = new TStringList();
    strList->LoadFromFile(mapFILENAME);
    //初始化起点和终点
    start_point_old_x = 51;
    start_point_old_y = 51;
    finish_point_old_x = 51;
    finish_point_old_y = 51;
    ClearMap();
    int x, y;
    for(x = 0; x < (*strList)[0].Length(); x++)
        for(y = 0; y < strList->Count; y++){
            switch((*strList)[y][x + 1]){
            case 'X' :
                setBrush(x * 10 + 5,y * 10 + 5, 1);
                imgMAP->Canvas->Rectangle(rectBrush);
                break;
            case '.' :
                setBrush(x * 10 + 5,y * 10 + 5, 2);
                imgMAP->Canvas->Rectangle(rectBrush);
                break;
            case 's' :
                setBrush(x * 10 + 5,y * 10 + 5, 3);
                imgMAP->Canvas->Rectangle(rectBrush);
                start_point_old_x = x;
                start_point_old_y = y;
                break;
            case 'f' :
                setBrush(x * 10 + 5,y * 10 + 5, 4);
                imgMAP->Canvas->Rectangle(rectBrush);
                finish_point_old_x = x;
                finish_point_old_y = y;
                break;
            default :
                setBrush(x * 10 + 5,y * 10 + 5, 2);
                imgMAP->Canvas->Rectangle(rectBrush);
                break;
            }
            pgbMAP->Position =  x * 2 + y * (2 / 50);
        }
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::btnClearMapClick(TObject *Sender)
{
    ClearMap();
}
//---------------------------------------------------------------------------
// 名称:ClearMap
// 功能:清空地图
//---------------------------------------------------------------------------
void TfrmMain::ClearMap()
{
    //初始化imgMAP
    rectBrush.Left = 0;
    rectBrush.Top = 0;
    rectBrush.Right = imgMAP->Width;
    rectBrush.Bottom = imgMAP->Height;
    imgMAP->Canvas->Brush->Style = bsSolid;
    imgMAP->Canvas->Pen->Color = clBlack;
    imgMAP->Canvas->Brush->Color = clBlack;
    imgMAP->Canvas->Rectangle(rectBrush);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnClearPathClick(TObject *Sender)
{
    ClearMapPath();
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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