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

#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;

}

你可能感兴趣的:(null,delete,Graph,Class,Path,include)