您的位置:首页 > 其它

迷宫的最短路径

2011-05-27 17:22 176 查看
5 6
0 0 0 0 0 0
0 3 1 1 1 0
0 1 1 0 1 0
0 1 1 0 4 0
0 0 0 0 0 0
(1 ,2)->(1 ,3)->(1, 4)->(2 ,4)->(3 ,4)

#include<iostream>
using namespace std;
#include "stdlib.h"
#include "stdio.h"
///===================================================================================================
///准备阶段
typedef struct
{
int _line,_row;
} coordinates;
typedef struct Node
{
coordinates position;
int depth;
} MazeGraph;
void CreateMazeGraph(MazeGraph** &MG,int &line,int &row,
coordinates &inlet,coordinates &outlet)
{
cin>>line>>row;
MG = (MazeGraph**)calloc(line,sizeof(MazeGraph*));
for(int i = 0; i < line; ++i)
{
MG[i] = (MazeGraph*)calloc(row,sizeof(struct Node));
for(int j = 0; j < row; ++j)
{
cin>>MG[i][j].depth;
MG[i][j].position._line = i;
MG[i][j].position._row = j;
if(MG[i][j].depth == 3)///入口
{
MG[i][j].depth = 1;///入口路径深度为0
inlet._line = i;
inlet._row = j;
}
else if(MG[i][j].depth == 4)///出口
{
MG[i][j].depth = 1;
outlet._line = i;
outlet._row = j;
}
}
}
}
void PrintMazeGraph(MazeGraph** MG,int line,int row)
{
for(int i = 0; i < line; ++i)
{
for(int j = 0; j < row; ++j)
{
cout<<MG[i][j].depth<<' ';
}
cout<<endl;
}
}
void DestoryMazeGraph(MazeGraph** &MG,int line)
{
for(int i = 0; i < line; ++i)
{
free(MG[i]);
}
free(MG);
MG = NULL;
}
///===============================================================================
///求最短路径的算法_队列准备
#define MAXQSIZE 100000
typedef struct
{
struct Node *base;
int front,rear;
} SqQueue;
void InitQueue(SqQueue &Q)
{
///初始化队列
Q.base = (struct Node*)calloc(MAXQSIZE,sizeof(struct Node));
Q.front = Q.rear = 0;
}
bool EnQueue(SqQueue &Q,struct Node e)
{
///入队
if((Q.rear + 1)%MAXQSIZE == Q.front)
{
puts("The Queue is full!");
return false;
}
else
{
Q.base[Q.rear] = e;
Q.rear = (Q.rear + 1)%MAXQSIZE;
return true;
}
}
bool DeQueue(SqQueue &Q,struct Node &e)
{
///出队
if(Q.rear == Q.front)
{
puts("The Queue is empty!");
return false;
}
else
{
e = Q.base[Q.front];
Q.front = (Q.front + 1)%MAXQSIZE;
return true;
}
}
///=====================================================================================
///核心算法部分
void FindShortestPath(MazeGraph** &MG,int &line,int &row,coordinates &outlet);///寻觅最短路径
void PrintShortestPath(MazeGraph** &MG,int &line,int &row,
coordinates &inlet,coordinates &outlet);///输出找到的最短路径
int main(void)
{
MazeGraph** MG;
int line,row;//line行,row列
coordinates inlet,outlet;//inlet入口,outlet出口
CreateMazeGraph(MG,line,row,inlet,outlet);
//PrintMazeGraph(MG,line,row);
FindShortestPath(MG,line,row,outlet);
//PrintMazeGraph(MG,line,row);
PrintShortestPath(MG,line,row,inlet,outlet);
DestoryMazeGraph(MG,line);
return 0;
}
void FindShortestPath(MazeGraph** &MG,int &line,int &row,coordinates &outlet)
{
SqQueue Q;
struct Node e;
InitQueue(Q);
EnQueue(Q,MG[outlet._line][outlet._row]);
while(Q.front != Q.rear)
{
DeQueue(Q,e);
int i = e.position._line,j = e.position._row;
if(i > 0 && i < line && MG[i - 1][j].depth != 0)  ///上
{
if((e.depth + 1 < MG[i - 1][j].depth) || (MG[i - 1][j].depth == 1))
{
MG[i - 1][j].depth = e.depth + 1;
EnQueue(Q,MG[i - 1][j]);
}
}
if(j >= 0 && j < row - 1 && MG[i][j + 1].depth != 0)///右
{
if((e.depth + 1 < MG[i][j + 1].depth) || (MG[i][j + 1].depth == 1))
{
MG[i][j + 1].depth = e.depth + 1;
EnQueue(Q,MG[i][j + 1]);
}
}
if(i >= 0 && i < line - 1 && MG[i + 1][j].depth != 0)  ///下
{
if((e.depth + 1 < MG[i + 1][j].depth) || (MG[i + 1][j].depth == 1))
{
MG[i + 1][j].depth = e.depth + 1;
EnQueue(Q,MG[i + 1][j]);
}
}
if(j > 0 && j < row && MG[i][j - 1].depth != 0)///左
{
if((e.depth + 1 < MG[i][j - 1].depth) || (MG[i][j - 1].depth == 1))
{
MG[i][j - 1].depth = e.depth + 1;
EnQueue(Q,MG[i][j - 1]);
}
}
}
}
void PrintShortestPath(MazeGraph** &MG,int &line,int &row,
coordinates &inlet,coordinates &outlet)
{
if(MG[inlet._line][inlet._row].depth == 1 || MG[inlet._line][inlet._row].depth == 0)
{
puts("Can't pass!");
}
else
{
int i = inlet._line,j = inlet._row;
printf("%d %d/n",i,j);
while(MG[i][j].depth != 2)
{
if(i > 0 && MG[i - 1][j].depth == MG[i][j].depth -1)///上
i--;
else if(j < row - 1 && MG[i][j + 1].depth == MG[i][j].depth -1)///右
j++;
else if(i < line - 1 && MG[i + 1][j].depth == MG[i][j].depth -1)///下
i++;
else  ///左
j--;
printf("%d %d/n",i,j);
}
printf("%d %d/n",outlet._line,outlet._row);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: