main.cpp

来自「我自己编写的A*搜索算法」· C++ 代码 · 共 71 行

CPP
71
字号
#include <iostream>
#include <cstdlib>
#include "AStar.hpp"

using namespace std;


char map[6*6] = { 'o', 'o', 'o', 'o', 'o', 'o',
                  'o', 's', ' ', ' ', ' ', 'o',
                  'o', ' ', 'o', ' ', ' ', 'o',
                  'o', 'o', 'o', ' ', 'o', 'o',
                  'o', ' ', ' ', ' ', 'd', 'o',
                  'o', 'o', 'o', 'o', 'o', 'o'};

void printMap(char *m, const int& rows, const int& cols){
    for(int i=0; i<rows; ++i){
        for(int j=0; j<cols; ++j)
            cout<<m[i * cols + j];
        cout<<endl;
    }
    cout<<endl;
}

int main()
{/*
    typedef AStar::priorityQueue PQueue;
    typedef AStar::ANode Node;
    PQueue q;
    Node nodes[6];
    for(int i=0; i<6; ++i){
        nodes[i].f = i + i;
        q.push(&nodes[i]);
    }

    Node temp;
    temp.f = 7;
    q.push(&temp);

    while(!q.empty()){
        cout<<q.top()->f<<endl;
        q.pop();
    }
*/
#ifdef _DEBUG
    TRACE_OFF();
    MEM_ON();
#endif
    printMap(map, 6, 6);

    vector<Point> path;
	AStar s(map, 6, 6);
    if(!s.findPath(path))
        cout<<"no path!"<<endl;

    for(vector<Point>::iterator itr=path.begin(); itr!=path.end(); ++itr){
        map[(*itr).x * 6 + (*itr).y] = '.';
    }

    for(int i=0; i<6; ++i){
        for(int j=0; j<6; ++j)
            cout<<map[i * 6 + j];
        cout<<endl;
    }
    cout<<endl;
#ifdef _DEBUG
    MEM_OFF();
#endif
	system("PAUSE");
	return 0;
}

⌨️ 快捷键说明

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