寻路小例子 没写完(待续)

#include "Graph.h"

#include <iostream>

#include <assert.h>

 

using namespace std;

 

#define  INVALID_ID -1

 

#define MAX_PATH_SIZE 900

 

#define  MAX_PATH_SCENEID  20

//#define  INFINITY 65535

#define  INFINITY 0

 

#define MAX_VEX 10

 

#define UINT int 

#define TRUE true

#define FALSE false

#define VOID void

#define BOOL bool

 

 

//循环队列

 

struct PathQueue_Element

{

BOOL Used;

int SceneID;

 

PathQueue_Element()

{

Used = FALSE;

SceneID = -1;

}

};

 

 

class AutoPathQueue

{

public:

AutoPathQueue(int nSize = MAX_PATH_SCENEID  ); //MAX_PATH_SIZE

~AutoPathQueue();

BOOL Init();

BOOL IsEmpty() { return m_Head == m_Tail ; }

 

//添加一个玩家到队列末尾

BOOL AddInQueue( int SceneID );

 

//取出队列头的玩家,并且将它从队列中删除

BOOL Pop();

 

int GetFront();

 

UINT GetCount() 

{

if(m_Head<=m_Tail)

{

return m_Tail - m_Head;

}

else

{

return m_Size - (m_Head - m_Tail);

}

 

 

public:

AutoPathQueue&  operator= (AutoPathQueue& rhs)

{

m_Head = rhs.m_Head;

m_Tail = rhs.m_Tail;

m_Size = rhs.m_Size;

for (int nSize = 0 ; nSize < m_Size ; nSize++ )

{

m_queue[nSize].Used = rhs.m_queue[nSize].Used;

m_queue[nSize].SceneID = rhs.m_queue[nSize].SceneID;

}

 

return *this;

}

 

public:

PathQueue_Element* m_queue;

int m_Head;

int m_Tail;

int m_Size;

};

 

AutoPathQueue::AutoPathQueue(int nSize)

{

m_Size = nSize;

m_Head = 0 ;

m_Tail = 0;

m_queue = new PathQueue_Element[m_Size];

}

 

AutoPathQueue::~AutoPathQueue()

{

if ( m_queue != NULL )

{

delete[] m_queue;

m_queue = NULL;

}

}

 

int AutoPathQueue::GetFront()

{

if ( !IsEmpty())

{

return m_queue[m_Head].SceneID;

}

return -1;

}

BOOL AutoPathQueue::AddInQueue( int nSceneID )

{

if ( m_queue[m_Tail].Used )

{//队列满

return FALSE;

}

m_queue[m_Tail].Used = TRUE;

m_queue[m_Tail].SceneID = nSceneID;

 

m_Tail++;

 

if ( m_Tail >= m_Size )

{

m_Tail = 0;

}

return TRUE;

};

 

BOOL AutoPathQueue::Pop( )

{

if ( m_queue[m_Head].Used == FALSE )

{ // 没有数据

return FALSE ;

}

m_queue[m_Head].Used = FALSE ; 

m_Head++;

 

if ( m_Head >= m_Size )

{

m_Head = 0;

}

 

return TRUE;

}

 

 

 

//图的算法

 

class Graph

{

 

public:

Graph();

~Graph(){};

 

int FirstVex(int i);

int NextVex(int i , int j);

void BFS_Graph(int start,int end, AutoPathQueue& AutoPathList);

 

public:

int vexs[MAX_VEX];

int arcs[MAX_VEX][MAX_VEX];

int vexnum,arcnum;

int pa[MAX_VEX];

};

 

 

Graph::Graph()

{

int i ;   

int ch1, ch2;   

 

memset(arcs,INFINITY,sizeof(arcs));

memset(pa,0,sizeof(pa));

 

printf("Enter vexnum arcnum: ");   

scanf("%d,%d", &vexnum, &arcnum);   

getchar();   

 

printf("Enter %d vexnum.../n", vexnum);   

for(i=1; i<=vexnum; i++)   

{   

printf("vex %d: ", i);   

scanf("%d", &vexs[i]);   

getchar();   

}   

printf("Enter %d arcnum.../n", arcnum);   

for(i=1; i<=arcnum; i++)   

{   

printf("arc %d: ", i);   

scanf("%d,%d", &ch1, &ch2);   

getchar();   

arcs[ch1][ch2]=arcs[ch2][ch1]=1;   

}   

 

}

 

int Graph::FirstVex(int sceneid)

{

 

if ( sceneid > 0 && sceneid <=  MAX_VEX)

{

for (int k = 1 ;  k<= vexnum ; k++ )

{

if ( arcs[sceneid][k] != INFINITY )

{

return k;

}

}

}

 

return -1;

}

 

 

//return  arcs[sceneid][k] != INFINITY ? k : -1;

 

int Graph::NextVex(int i , int j)

{

int k;

 

if ( i> 0 && i <= MAX_VEX && j > 0 && j<= MAX_VEX )

{

for (k = j+1 ; k<= vexnum ; k ++ )

{

if ( arcs[i][k] != INFINITY )

{

return k;

}

}

}

return -1;

}

 

void Graph::BFS_Graph(int start,int end, AutoPathQueue& AutoPathList)

{

int visited[MAX_VEX];

int pa[MAX_VEX];

int parent = start;

int k;

int path[MAX_VEX];

int pathlen = 0;

 

 

memset(path,0,sizeof(path));

 

 

//queue<int>q;

AutoPathQueue PathQueue;

memset(visited,0,sizeof(visited));

memset(pa,0,sizeof(pa)); 

 

visited[start]= 1;

pa[start] = -1;

//q.push(start);

PathQueue.AddInQueue(start);

 

//while ( !q.empty())

while (PathQueue.IsEmpty())

{

//k = q.front();

k = PathQueue.GetFront();

parent = k ;

//q.pop();

PathQueue.Pop();

for(int j=FirstVex( k); j>=1; j=NextVex( k, j))   

if(!visited[j])   

{   

visited[j]=1;   

pa[ vexs[j] ]= parent;  

if ( vexs[j] == end)

{

cout<<"找到目标路径"<<endl;

break;

}

 

//q.push( j );   

PathQueue.AddInQueue(j);

}   

}

 

cout<<"路径如下"<<endl;

parent = end;  

 

while (  parent != -1 )

{

//path[pathlen++] =parent;

AutoPathList.AddInQueue(parent);

cout<<parent<<"--";

parent = pa[parent];

}

cout<<endl;

cout<<"path 如下"<<endl;

 

for ( int n = 0 ; n < AutoPathList.GetCount() ; n++ )

{

if ( AutoPathList.m_queue[n].SceneID == -1)

{

break;

}

else

{

cout<<AutoPathList.m_queue[n].SceneID<<"--";

}

}

}

Graph g_Graph;

 

class AutPathTable

{

public:

VOID Init()

{

for ( int i = 0 ; i < MAX_PATH_SCENEID ; i++ )

for (int j = 0 ; j < MAX_PATH_SCENEID ; j++ )

g_Graph.BFS_Graph( i , j , m_AutoPathQueue[i][j]);

 

}

private:

AutoPathQueue m_AutoPathQueue[MAX_PATH_SCENEID][MAX_PATH_SCENEID];

};

 

 

int main()

{

 

 

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面我给你提供一个用C++实现地下城小地图A星寻路的简单示例代码,供你参考: ```c++ #include <iostream> #include <vector> #include <queue> #include <cstring> #include <algorithm> using namespace std; const int INF = 0x3f3f3f3f; // 表示无穷大 struct Node{ int x, y; // 节点坐标 int f, g, h; // f = g + h bool operator < (const Node &A) const{ // 优先级队列需要重载小于号运算符 return f > A.f; } }; const int N = 10; int n, m; // 地图大小 int sx, sy; // 起点坐标 int ex, ey; // 终点坐标 char g[N][N]; // 地图数组 bool vis[N][N]; // 标记数组,标记某个点是否被访问过 int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; // 方向数组 // 判断一个点是否可以走(不超出边界,不是墙,没有访问过) bool check(int x, int y){ if(x < 0 || x >= n || y < 0 || y >= m) return false; if(g[x][y] == '#') return false; if(vis[x][y]) return false; return true; } // 计算曼哈顿距离 int manhattan(int x, int y){ return abs(x - ex) + abs(y - ey); } // A星搜索 int A_star(){ priority_queue<Node> q; memset(vis, false, sizeof(vis)); Node start = {sx, sy, 0, 0, manhattan(sx, sy)}; q.push(start); while(!q.empty()){ Node t = q.top(); q.pop(); if(vis[t.x][t.y]) continue; vis[t.x][t.y] = true; if(t.x == ex && t.y == ey) return t.f; for(int i = 0; i < 4; i++){ int nx = t.x + d[i][0], ny = t.y + d[i][1]; if(check(nx, ny)){ Node nxt = {nx, ny, 0, 0, 0}; nxt.g = t.g + 1; nxt.h = manhattan(nx, ny); nxt.f = nxt.g + nxt.h; q.push(nxt); } } } return -1; } int main(){ cin >> n >> m; for(int i = 0; i < n; i++){ cin >> g[i]; } cin >> sx >> sy >> ex >> ey; int ans = A_star(); cout << ans << endl; return 0; } ``` 以上是一个简单的地下城小地图A星寻路的C++实现示例,希望对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值