• 67.迷宫问题(广搜)


    67.迷宫问题(广搜)

    样例输入1

    5 9

    1 1 4 9

    0 -1 0 0 0 0 0 0 -1

    0 0 0 0 -1 0 0 0 -1

    -1 0 0 0 0 0 -1 -1 -1

    0 0 -1 -1 0 0 0 0 0

    0 0 0 0 0 0 0 -1 -1

    样例输出1

    (1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(3,5)->(3,6)->(4,6)->(4,7)->(4,8)->(4,9)

    样例输入2

    5

    2 1

    8 4

    -1  -1 -1  -1 -1

      -1

    -1  -1 -1  -1

    -1  -1

    -1  -1 -1

    -1  -1

    -1  -1 -1  -1

    -1  -1

    样例输出2

    (2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(4,4)->(4,3)->(5,3)->(6,3)->(6,4)->(7,4)->(8,4)

    样例输入3

    8 5

    2 1

    8 4

    -1  -1 -1 -1 -1

      0 -1

    -1  -1 -1  0 -1

    -1  0 -1

    -1  0 -1 -1

    -1  0 -1

    -1  -1 -1 -1 -1

    -1  0 -1

    样例输出3

    no way

    代码:
    一:广搜:

    #include

    using namespace std;

    #include

    int n,m,qx,qy,zx,zy;

    int jz[101][101];

    int pre[101];

    int xx[]={0,0,1,-1};

    int yy[]={1,-1,0,0};

    struct Point{

           int x,y;

    };

    Point point[101];

    void input()

    {

           scanf("%d%d",&n,&m);

           scanf("%d%d%d%d",&qx,&qy,&zx,&zy);

           for(int i=1;i<=n;++i)

             for(int j=1;j<=m;++j)

             scanf("%d",&jz[i][j]);

          

    }

    void out(int d)

    {

           if(pre[d])

           out(pre[d]);

           printf("(%d,%d)->",point[d].x,point[d].y);

    }

    void BFS()

    {

           int head=0,tail=1;

           point[tail].x=qx;

           point[tail].y=qy;

           pre[tail]=0;

           while(head

           {

                  ++head;

                  int x0=point[head].x,y0=point[head].y;

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

                  {

                         int x1=x0+xx[i],y1=y0+yy[i];

                         if(x1>=1&&x1<=n&&y1>=1&&y1<=m&&jz[x1][y1]==0)

                         {

                                jz[x1][y1]=-1;

                                ++tail;

                                pre[tail]=head;

                                point[tail].x=x1;

                                point[tail].y=y1;

                         }

                         if(point[tail].x==zx&&point[tail].y==zy)

                         {

                                out(pre[tail]);

                                printf("(%d,%d)",point[tail].x,point[tail].y);

                                return ;

                         }

                  }

           }

           printf("no way");

           return;

    }

    int main()

    {

           freopen("1.out","w",stdout);

           input();

           BFS();

           return 0;

    }

  • 相关阅读:
    详细解析Windows按键突破专家的原理
    详细解析Windows按键突破专家的原理
    简单线程注入的实现
    运用 Evince 阅读 PDF 电子书
    Sonata 0.7
    P7ZIP-Linux 中的 7Zip
    Firefox 特征扩展:Video Download
    若何在嵌入式Linux及下建造QPF字库
    Wink-Flash 演示录制软件
    自由软件:理想与实践
  • 原文地址:https://www.cnblogs.com/csgc0131123/p/5290326.html
Copyright © 2020-2023  润新知