急急急!!!!求助C语言大作业解答.有题目在这里(kk)

2024-12-28 18:47:32
推荐回答(1个)
回答1:

//第二题,迷宫算法
#include
#include
#define MaxRow 20 //地图大小
#define MaxCol 30
int themap[MaxRow][MaxCol]={
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,2,0,0,0,0,0,0,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,1,1,1,1,1,1,0,1,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0,0,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,0,1,1,1,1,1,1,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,1,1,0,0,0,1,0,0,1,0,1,0,1,0,0,1,0,1,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,1,0,0,0,1,1,0,1,0,1,0,0,1,0,1,0,0,0,0,0,0,0,0},
{0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,0,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,0,1,1,0,1,1,1,0,1,0,0,0,0,0,1,1,1,1,1,1,0},
{0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,1,0},
{0,0,0,0,0,0,1,1,0,0,0,0,0,1,0,1,0,1,0,0,0,0,0,1,0,4,0,0,1,0},
{0,0,0,0,0,0,0,1,1,1,1,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,1,1,1,0,0,0,0,0,1,0,1,0,1,0,0},
{0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}}; //地图 ,1是障碍,0是可行
struct aStar //双向链表
{
int X;
int Y; //坐标
aStar* Pnext;
aStar* Ppre;
};
aStar* pHeader = NULL;//链表头
aStar* ptail = NULL; //链表尾
void main()
{
int X= 2;
int Y= 1; //起始坐标
int xE = 24;
int yE = 15; //目的坐标
pHeader = new aStar; //分配结构体内存
pHeader->X = X;
pHeader->Y = Y;
pHeader->Ppre = NULL;
pHeader->Pnext = NULL; //表头
ptail = pHeader; //表头=表尾
aStar* pNew = NULL;
static bool BRes = false; //判断是否到达目的
for (aStar* ptemp=pHeader;ptemp!=NULL;ptemp=ptemp->Pnext)
{
if (themap[ptemp->Y+1][ptemp->X] !=1 && themap[ptemp->Y+1][ptemp->X]!=3 && ptemp->Y+1 { //1、看纵坐标正方向。不是障碍,不是父节点,没到达边界
pNew = new aStar;
pNew->X = ptemp->X;//map横坐标
pNew->Y = ptemp->Y+1;//map纵坐标
themap[ptemp->Y+1][ptemp->X] = 3; //父节点
pNew->Pnext = NULL;
pNew->Ppre = ptemp; //存储父节点
ptail->Pnext = pNew;
ptail = pNew;
if (pNew->X == xE && pNew->Y == yE)//判断是否到达目的地
{
BRes = true;
break;
}
}
if (themap[ptemp->Y][ptemp->X+1] != 1 && themap[ptemp->Y][ptemp->X+1]!=3 && ptemp->X+1 { //2、看横坐标正方向。不是障碍,不是父节点,没到达边界
pNew = new aStar;
pNew->X = ptemp->X+1;
pNew->Y = ptemp->Y;
themap[ptemp->Y][ptemp->X+1] = 3; //父节点
pNew->Pnext = NULL;
pNew->Ppre = ptemp;
ptail->Pnext = pNew;
ptail = pNew;
if (pNew->X == xE && pNew->Y == yE) //判断是否到达目的地
{
BRes = true;
break;
}
}
if (themap[ptemp->Y-1][ptemp->X] != 1 && themap[ptemp->Y-1][ptemp->X]!=3 && ptemp->Y-1>0)
{ //3、看纵坐标负方向。不是障碍,不是父节点,没小于边界
pNew = new aStar;
pNew->X = ptemp->X;
pNew->Y = ptemp->Y-1;
themap[ptemp->Y-1][ptemp->X] = 3;
pNew->Pnext = NULL;
pNew->Ppre = ptemp;
ptail->Pnext = pNew;
ptail = pNew;
if (pNew->X == xE && pNew->Y == yE)
{
BRes = true;
break;
}
}
if (themap[ptemp->Y][ptemp->X-1] != 1 && themap[ptemp->Y][ptemp->X-1]!=3 && ptemp->X-1>0)
{ //4、看横坐标负方向。不是障碍,不是父节点,没小于边界
pNew = new aStar;
pNew->X = ptemp->X-1;
pNew->Y = ptemp->Y;
themap[ptemp->Y][ptemp->X-1] = 3;
pNew->Pnext = NULL;
pNew->Ppre = ptemp;
ptail->Pnext = pNew;
ptail = pNew;
if (pNew->X == xE && pNew->Y == yE)
{
BRes = true;
break;
}
}
if (
!(themap[ptemp->Y][ptemp->X-1] ==1 || themap[ptemp->Y-1][ptemp->X] ==1)
&&
themap[ptemp->Y-1][ptemp->X-1] != 1 && themap[ptemp->Y-1][ptemp->X-1]!=3 && ptemp->Y-1>0 && ptemp->X-1>0 )
{ //5、看左上方向。不是障碍,不是父节点,没小于边界
pNew = new aStar;
pNew->X = ptemp->X-1;
pNew->Y = ptemp->Y-1;
themap[ptemp->Y-1][ptemp->X-1] = 3;
pNew->Pnext = NULL;
pNew->Ppre = ptemp;
ptail->Pnext = pNew;
ptail = pNew;
if (pNew->X == xE && pNew->Y == yE)
{
BRes = true;
break;
}
}
if (
!(themap[ptemp->Y-1][ptemp->X] ==1 || themap[ptemp->Y][ptemp->X+1] ==1)
&&
themap[ptemp->Y-1][ptemp->X+1] != 1 && themap[ptemp->Y-1][ptemp->X+1]!=3 &&ptemp->Y-1>0 && ptemp->X+1 { //6、看左下方向。不是障碍,不是父节点,没小于边界,没超出界限
pNew = new aStar;
pNew->X = ptemp->X+1;
pNew->Y = ptemp->Y-1;
themap[ptemp->Y-1][ptemp->X+1] = 3;
pNew->Pnext = NULL;
pNew->Ppre = ptemp;
ptail->Pnext = pNew;
ptail = pNew;
if (pNew->X == xE && pNew->Y == yE)
{
BRes = true;
break;
}
}
if (

!(themap[ptemp->Y+1][ptemp->X] ==1 || themap[ptemp->Y][ptemp->X-1] ==1)
&&
themap[ptemp->Y+1][ptemp->X-1] != 1 && themap[ptemp->Y+1][ptemp->X-1]!=3 && ptemp->Y+1X-1>0)
{ //7、看右上方向。不是障碍,不是父节点,没超出界限 ,没小于边界
pNew = new aStar;
pNew->X = ptemp->X-1;
pNew->Y = ptemp->Y+1;
themap[ptemp->Y+1][ptemp->X-1] = 3;
pNew->Pnext = NULL;
pNew->Ppre = ptemp;
ptail->Pnext = pNew;
ptail = pNew;
if (pNew->X == xE && pNew->Y == yE)
{
BRes = true;
break;
}
}
if (
!(themap[ptemp->Y][ptemp->X+1] ==1 || themap[ptemp->Y+1][ptemp->X] ==1)
&&
themap[ptemp->Y+1][ptemp->X+1] != 1 && themap[ptemp->Y+1][ptemp->X+1]!=3 &&
ptemp->Y+1X+1 { //8、看右下方向。不是障碍,不是父节点,没超出界限
pNew = new aStar;
pNew->X = ptemp->X+1;
pNew->Y = ptemp->Y+1;
themap[ptemp->Y+1][ptemp->X+1] = 3;
pNew->Pnext = NULL;
pNew->Ppre = ptemp;
ptail->Pnext = pNew;
ptail = pNew;
if (pNew->X == xE && pNew->Y == yE)
{
BRes = true;
break;
}
}
}

for (int n = 0;n{
for (int t = 0;t {
if(themap[n][t] == 3)
{
themap[n][t] = 0; //把父节点值恢复为0
}
}
}
if (BRes == true) //若找到目的地
{
for (aStar* ptempE = ptail;ptempE->Ppre!=NULL;ptempE=ptempE->Ppre)
{
themap[ptempE->Y][ptempE->X] = 5; //父节点
}
}
for (int nE = 0;nE{

for (int tE = 0;tE{
//printf("%d ",themap[nE][tE]);
switch(themap[nE][tE])
{
case 0:
cout<<"※"; //可行
break;
case 1:
cout<<"■"; //障碍
break;
case 2:
cout<<"☆"; //起点
break;
case 4:
cout<<"№"; //终点
break;
case 5:
cout<<"◎"; //路径
break;
default:
break;
}
}
cout<}
aStar* ptempE = NULL; //释放指针
while (pHeader!=NULL) //释放链表
{
ptempE = pHeader;
pHeader = pHeader->Pnext;
delete ptempE;
}
}