noisewuwei 2014-12-09 06:17 采纳率: 0%
浏览 1866
已结题

一段代码,迷宫问题,求大神加注释

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 2014-12-09 13:39
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题