哪位前辈有关于2D游戏中的A*算法的源程序?包括测试界面的,能不能给在下用用,解燃眉之急??我的邮箱:liyongic@yahoo.com.cn
我在网上下过一个ClassAStar的程序,可是里面缺少一个“CDX.H”的头文件,更郁闷的是,我又不记得是在哪里下到的,有知道下载地址的也请帮帮在下哦,救急如救火啊!拜谢!!
只有迷宫的A*算法程序^_^
以前写的哦,没弄界面
没有哦:(
先闪人睡觉了,明天还上课
刚好以前写过,在注释里面有输入文件格式的说明
//==================================================
//
// Maze (using A* search)
//
//
//
// by nasi
//
//==================================================
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <queue>
#define __STRICT
//--------------------------------------------------
// Class: elem
// DESC: used for construct a priority queue
//--------------------------------------------------
class elem
{
public:
int v;
int cost;
int depth;
elem(int _v, int _c, int _d) : v(_v), cost(_c), depth(_d) {}
bool operator<(const elem& rhs) const
{
return rhs.cost < cost;
}
};
//--------------------------------------------------
// Class: cMaze
// DESC: used for solve the problem
//--------------------------------------------------
class cMaze
{
//--------------------------------------------------
// Constants
//--------------------------------------------------
protected:
enum { MAX_VERTEX = 20 };
//--------------------------------------------------
// Members
//--------------------------------------------------
protected:
int m_iCol, m_iRow;
std::vector<int> m_vGraph[MAX_VERTEX*MAX_VERTEX];
bool m_bVisited[MAX_VERTEX*MAX_VERTEX];
int m_iSrc, m_iDst;
int m_iSolve[MAX_VERTEX*MAX_VERTEX];
int m_iParent[MAX_VERTEX];
int m_iLen;
bool m_bLoaded, m_bSolved;
char m_cCanvas[MAX_VERTEX*2][MAX_VERTEX*3];
//--------------------------------------------------
// Public Interfaces
//--------------------------------------------------
public:
// constructor
cMaze();
// destructor
virtual ~cMaze();
// load a maze-map from text file
void load(const char *filename);
// solve it using A* search
void solve();
// output the results
void output();
//--------------------------------------------------
// Member Functions
//--------------------------------------------------
// calculate the cost
int cost(int v)
{
int xg = m_iDst / m_iCol;
int yg = m_iDst % m_iCol;
int xn = v / m_iCol;
int yn = v % m_iCol;
return abs(xg - xn) + abs(yg - yn);
}
// show error message when it occurs
void err(char *desc)
{
std::cout << "Error: ";
std::cout << desc << std::endl;
}
void init_canvas()
{
int i, j;
for (i = 0; i < m_iRow*2; ++i)
{
for (j = 0; j < m_iCol*3; ++j)
{
m_cCanvas[i][j] = (!(i % 2) && !(j % 3)) ? o : ;
}
}
m_cCanvas[(m_iSrc/m_iCol)*2][(m_iSrc%m_iCol)*3] = S;
m_cCanvas[(m_iDst/m_iCol)*2][(m_iDst%m_iCol)*3] = E;
}
void paint_canvas()
{
int i, j;
for (i = 0; i < m_iRow*2 - 1; ++i)
{
for (j = 0; j < m_iCol*3; ++j)
{
std::cout << m_cCanvas[i][j];
}
std::cout << std::endl;
}
}
void link_canvas(int v, int i)
{
int x, y;
x = i / m_iCol;
y = i % m_iCol;
if (v == i-1)
{
// left
m_cCanvas[x*2][y*3-1] = m_cCanvas[x*2][y*3-2] = -;
}
else if (v == i+1)
{
// right
m_cCanvas[x*2][y*3+1] = m_cCanvas[x*2][y*3+2] = -;
}
else if (v < i)
{
// up
m_cCanvas[x*2-1][y*3] = |;
}
else
{
// down
m_cCanvas[x*2+1][y*3] = |;
}
}
};
//--------------------------------------------------
// Constructor
//--------------------------------------------------
cMaze::cMaze()
{
m_iRow = m_iCol = 0;
m_iSrc = m_iDst = 0;
int i;
for(i = 0; i < MAX_VERTEX*MAX_VERTEX; ++i)
{
m_vGraph[i].clear();
m_iParent[i] = -1;
}
memset(m_bVisited, 0, MAX_VERTEX * MAX_VERTEX * sizeof(bool));
memset(m_iSolve, 0, MAX_VERTEX * MAX_VERTEX * sizeof(int) );
m_iLen = 0;
m_bLoaded = m_bSolved = false;
}
//--------------------------------------------------
// Destructor
//--------------------------------------------------
cMaze::~cMaze()
{
// only use for override
}
//--------------------------------------------------
// Load()
// @param const char *filename: the text file name
// @describe
// the text file format
// --------------------
// number_of_row number_of_column
// source_vertex destination_vertex
// number_of_edge
// vertex0 vertex1 (a edge between v0 and v1)
// ...
// the map format
// --------------
// v[0] v[1] v[2] .... v[m]
// v[m+1] ... v[2m]
//--------------------------------------------------
void cMaze::load(const char *filename)
{
std::ifstream fin(filename);
fin >> m_iRow >> m_iCol;
fin >> m_iSrc >> m_iDst;
int edge, i, src, dst;
fin >> edge;
for (i = 0; i < edge; ++i)
{
fin >> src >> dst;
m_vGraph[src].push_back(dst);
m_vGraph[dst].push_back(src);
}
m_bLoaded = true;
}
//--------------------------------------------------
// Solve
//--------------------------------------------------
void cMaze::solve()
{
#ifdef __STRICT
if (!m_bLoaded)
{
err("Map is not loaded yet");
}
#endif
std::priority_queue<elem> q;
// push the source to the priority queue
q.push( elem(m_iSrc, cost(m_iSrc), 0) );
m_bVisited[m_iSrc] = true;
while (! q.empty())
{
// got the top one (the least cost)
elem e = q.top();
q.pop();
// check if it is the destination
if (e.v == m_iDst)
{
// construct the solution
m_iLen = 0;
m_iSolve[m_iLen++] = m_iDst;
int v = m_iParent[m_iDst];
while( -1 != v )
{
m_iSolve[m_iLen++] = v;
v = m_iParent[v];
}
m_bSolved = true;
return;
}
// extend the vertex
int i;
for (i = 0; i < m_vGraph[e.v].size(); ++i)
{
int v = m_vGraph[e.v][i];
if (!m_bVisited[v])
{
m_bVisited[v] = true;
q.push( elem(v, cost(v) + e.depth + 1, e.depth+1) );
m_iParent[v] = e.v;
}
}
}
}
//--------------------------------------------------
// Output
//--------------------------------------------------
void cMaze::output()
{
#ifdef __STRICT
if (!m_bSolved)
{
err("Maze is not solved correctly");
}
#endif
int i, j;
// output the maze
std::cout << "the Maze:" << std::endl;
init_canvas();
for(i = 0; i < m_iRow*m_iCol; ++i)
{
for(j = 0; j < m_vGraph[i].size(); ++j)
{
link_canvas(m_vGraph[i][j], i);
}
}
paint_canvas();
std::cout << std::endl;
if (0 == m_iLen)
{
std::cout << "the Path doesnt exist." << std::endl;
return;
}
// output the path
std::cout << "the Path:" << std::endl;
init_canvas();
for(i = 0; i < m_iLen-1; ++i)
{
link_canvas(m_iSolve[i], m_iSolve[i+1]);
}
paint_canvas();
}
//--------------------------------------------------
// Main Function
//--------------------------------------------------
int main()
{
cMaze *maze;
#ifdef __DEBUG
#include <cstdio>
freopen("debug.txt", "w", stdout);
#endif
maze = new cMaze;
maze->load("map2.txt");
maze->solve();
maze->output();
delete maze;
return 0;
}
能否贴map2.txt上来
map2.txt是一个5K的文件,里面全是数据……
这个注释描述了文本文件的格式
//--------------------------------------------------
// Load()
// @param const char *filename: the text file name
// @describe
// the text file format
// --------------------
// number_of_row number_of_column
// source_vertex destination_vertex
// number_of_edge
// vertex0 vertex1 (a edge between v0 and v1)
// ...
// the map format
// --------------
// v[0] v[1] v[2] .... v[m]
// v[m+1] ... v[2m]
//--------------------------------------------------
给你贴一个简单的吧
5 5
23 14
26
0 1
0 5
1 2
1 6
2 3
2 7
3 8
5 6
5 10
6 11
7 8
7 12
8 9
8 13
10 11
11 12
12 13
12 17
13 14
13 18
15 16
15 20
17 18
17 22
18 19
18 23
mark and learning...