您的位置:首页 > 其它

迷宫问题队列实现

2013-10-23 20:55 239 查看
#include <iostream>
using namespace std;
#define M 10
#define N 10
#define MaxSize 100
int mg[M]
=
{
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,0,0,1,1,0,0,1},
{1,0,1,1,1,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,1},
{1,0,1,0,0,0,1,0,0,1},
{1,0,1,1,1,0,1,1,0,1},
{1,1,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
struct
{
int i;
int j;
int pre;
}Qu[MaxSize];
int front=0,rear=0;
void print(int front)
{
while(front!=-1)
{
printf("a[%d][%d]\n",Qu[front].i,Qu[front].j);
front=Qu[front].pre;
}
}
void mappath(int x1,int y1,int x2,int y2)
{
rear++;
Qu[rear].i=x1;
Qu[rear].j=y1;
Qu[rear].pre=-1;
mg[x1][y1]=-1;
int find=0;
while(rear!=front)
{
int i,j;
front++;
i=Qu[front].i;
j=Qu[front].j;
if (i==x2&&j==y2)
{
find=1;
print(front);
}
int di;
for (di=0;di<3;di++)
{
switch(di)
{
case 0:i=Qu[front].i-1;j=Qu[front].j;break;
case 1:i=Qu[front].i;j=Qu[front].j+1;break;
case 2:i=Qu[front].i+1;j=Qu[front].j;break;
case 3:i=Qu[front].i;j=Qu[front].j-1;break;
}
if (mg[i][j]==0)
{
rear++;
Qu[rear].i=i;Qu[rear].j=j;
Qu[rear].pre=front;
mg[i][j]=-1;
}
}
}
if (!find)
{
printf("没路径\n");
}
}

int main()
{
mappath(1,1,8,8);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: