2 noisewuwei noisewuwei 于 2014.12.09 14:17 提问

一段代码,迷宫问题,求大神加注释 5C
c++

maze::maze()
{
int i,j;
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;
}
}

1个回答

qqlindexi
qqlindexi   2014.12.09 21:39

csdn如何提问问题,我找了整个页面,半个钟都没找到

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!