问题描述
- 一段代码,迷宫问题,求大神加注释 5C
- maze::maze()
{
int ij;
cout<<""请输入迷宫规模,长、宽,以空格隔开:"";
cin>>Height>>Width;
a = new int*[Height];
for(i = 0;i != Height;i++)
{
a[i] = new int[Width];
}
rd = new Road*[Height*Width];
Road *temp = new Road[Height*Width];
cout<<""请输入迷宫矩阵:"";
cout<<endl;
for(i = 0;i != Height;i++)
{
for(j = 0;j != Width;j++)
{cin>>a[i][j]; rd[(j+1)+i*Width-1] = &temp[(j+1)+i*Width-1]; rd[(j+1)+i*Width-1]->state = a[i][j]; rd[(j+1)+i*Width-1]->num = (j+1)+i*Width-1; //cout<<rd[(j+1)+i*n-1]->state; }}for(i = 0;i != Height;i++){ for(j = 0;j != Width;j++) { if(i == 0) { if(j == 0) { rd[(j+1)+i*Width-1]->N = NULL; rd[(j+1)+i*Width-1]->S = rd[(j+1)+(i+1)*Width-1]; rd[(j+1)+i*Width-1]->W = NULL; rd[(j+1)+i*Width-1]->E = rd[(j+1+1)+i*Width-1]; } else if(j == Width-1) { rd[(j+1)+i*Width-1]->N = NULL; rd[(j+1)+i*Width-1]->S = rd[(j+1)+(i+1)*Width-1]; rd[(j+1)+i*Width-1]->W = rd[(j-1+1)+i*Width-1]; rd[(j+1)+i*Width-1]->E = NULL; } else { rd[(j+1)+i*Width-1]->N = NULL; rd[(j+1)+i*Width-1]->S = rd[(j+1)+(i+1)*Width-1]; rd[(j+1)+i*Width-1]->W = rd[(j-1+1)+i*Width-1]; rd[(j+1)+i*Width-1]->E = rd[(j+1+1)+i*Width-1]; } } else if(i == (Height-1)) { if(j == 0) { rd[(j+1)+i*Width-1]->N = rd[(j+1)+(i-1)*Width-1]; rd[(j+1)+i*Width-1]->S = NULL; rd[(j+1)+i*Width-1]->W = NULL; rd[(j+1)+i*Width-1]->E = rd[(j+1+1)+i*Width-1]; } else if(j == Width-1) { rd[(j+1)+i*Width-1]->N = rd[(j+1)+(i-1)*Width-1]; rd[(j+1)+i*Width-1]->S = NULL; rd[(j+1)+i*Width-1]->W = rd[(j-1+1)+i*Width-1]; rd[(j+1)+i*Width-1]->E = NULL; } else { rd[(j+1)+i*Width-1]->N = rd[(j+1)+(i-1)*Width-1]; rd[(j+1)+i*Width-1]->S = NULL; rd[(j+1)+i*Width-1]->W = rd[(j-1+1)+i*Width-1]; rd[(j+1)+i*Width-1]->E = rd[(j+1+1)+i*Width-1]; } } else { if(j == 0) { rd[(j+1)+i*Width-1]->N = rd[(j+1)+(i-1)*Width-1]; rd[(j+1)+i*Width-1]->S = rd[(j+1)+(i+1)*Width-1]; rd[(j+1)+i*Width-1]->W = NULL; rd[(j+1)+i*Width-1]->E = rd[(j+1+1)+i*Width-1]; } else if(j == Width-1) { rd[(j+1)+i*Width-1]->N = rd[(j+1)+(i-1)*Width-1]; rd[(j+1)+i*Width-1]->S = rd[(j+1)+(i+1)*Width-1]; rd[(j+1)+i*Width-1]->W = rd[(j-1+1)+i*Width-1]; rd[(j+1)+i*Width-1]->E = NULL; } else { rd[(j+1)+i*Width-1]->N = rd[(j+1)+(i-1)*Width-1]; rd[(j+1)+i*Width-1]->S = rd[(j+1)+(i+1)*Width-1]; rd[(j+1)+i*Width-1]->W = rd[(j-1+1)+i*Width-1]; rd[(j+1)+i*Width-1]->E = rd[(j+1+1)+i*Width-1]; } } }}cout<<""迷宫矩阵:""<<endl;for(i = 0;i != Height;i++){ for(j = 0;j != Width;j++) { cout<<a[i][j]<<""t""; } cout<<endl;}cout<<endl;cout<<""迷宫节点序号矩阵:""<<endl;for(i = 0;i != Height;i++){ for(j = 0;j != Width;j++) { cout<<rd[(j+1)+i*Width-1]->num<<""t""; } cout<<endl;}cout<<endl;cout<<""请输入入口序号:"";cin>>innum;cout<<""请输入出口序号:"";cin>>outnum;if(rd[innum]->state == 0){ if(getout(rd[innum]->num)>0) { cout<<rd[innum]->num<<""<--进入""<<endl; } else { cout<<""没有出口!""<<endl; }}else{ cout<<""没有此入口!""<<endl;}
}
int maze::getout(int num) //shendu
{
rd[num]->flog = 1;
if(num == outnum)
{
cout<<""出口<--"";
return 1;
}
else if(rd[num]->N != NULL && rd[num]->N->state == 0 && rd[num]->N->flog != 1 && getout(rd[num]->N->num)>0)
{
cout<N->num<<""<--"";
return rd[num]->N->num;
}
else if(rd[num]->S != NULL && rd[num]->S->state == 0 && rd[num]->S->flog != 1 && getout(rd[num]->S->num)>0)
{
cout<S->num<<""<--"";
return rd[num]->S->num;
}
else if(rd[num]->W != NULL && rd[num]->W->state == 0 && rd[num]->W->flog != 1 && getout(rd[num]->W->num)>0)
{
cout<W->num<<""<--"";
return rd[num]->W->num;
}
else if(rd[num]->E != NULL && rd[num]->E->state == 0 && rd[num]->E->flog != 1 && getout(rd[num]->E->num)>0)
{
cout<E->num<<""<--"";
return rd[num]->E->num;
}
else
{
return -1;
}
}
解决方案
csdn如何提问问题,我找了整个页面,半个钟都没找到