poj 3984 迷宫问题 学会了一招计算路径

    技术2022-05-20  36

    #include<iostream>

    using namespace std;

    #include<queue>

    int a[5][5];

    int dir[4][2]={0,1,1,0,-1,0,0,-1};

    int map[5][5];

    void bfs(int x,int y)

    {

    queue<int> q;

     

    q.push(x);

    q.push(y);

    // system("pause");

    while(!q.empty())

         {

    int m=q.front();q.pop();

    int n=q.front();q.pop();

    // cout<<m<<' '<<n;

    // system("pause");

    for(int i=0;i<4;i++)

    {

    int mm=m+dir[i][1];

    int nn=n+dir[i][0];

    if(mm==4 && nn==4)

               {

               map[mm][nn]=m*5+n;

       //       cout<<map[mm][nn];

        //       system("pause");

               return ;

    }

    if(mm>=0 && mm<=4 && nn>=0 && nn<=4 && a[mm][nn]!=1)

    {

    q.push(mm);

    q.push(nn);

    map[mm][nn]=(5*m+n);//计算路径的好方法

    a[mm][nn]=1;

    }

         }

    }

    }

    void print(int i,int j)

    {

    // cout<<map[i][j]<<' '<<j<<endl;

    // system("pause");

         if(i==0&&j==0)

         {

    printf("(0, 0)/n");

    return ;

    }

    print(map[i][j]/5,map[i][j]%5);

    printf("(%d, %d)/n",i,j);

    }

    int main()

    {

    for(int i=0;i<5;i++)

    {

    for(int j=0;j<5;j++)

    {

    cin>>a[i][j];

    }

    }

    bfs(0,0);

    print(4,4);

    // system("pause");

    return 0;

    }


    最新回复(0)