• 1254:走出迷宫


    背景众所周知,这道题还是个迷宫板子。。。。。

    原理:广搜,检索路径,将走过的和墙所在区域置为0,其他区域置为1(可以走)。那么,用队列存一下路径,搜索每深一层,步数++,然后后退时步数再-回去就行了,最后遍历完,将所有的路径步数比对(或者设置minn,保存历史搜索最短步数就行了)很简单啊。

    用到QUEUE

    #include<cstdio>
    #include<cstring>
    #include<queue>
    using namespace std;
    
    int r,c;
    int X[4]={0,1,0,-1},
        Y[4]={1,0,-1,0};
    bool a[200][200];
    
    int main()
    {
        queue<int> x,y,b,f;
        int n,m;
        b.push(0);f.push(4);
    
        scanf("%d%d",&r,&c);
        memset(a,0,sizeof(a));
        for(int i=1;i<=r;++i)
        {
            char C;
            scanf("%c",&C);//去掉前面的空格 
            for(int j=1;j<=c;++j)
            {
              scanf("%c",&C);
              if(C=='#') a[i][j]=1;
              else
              if(C=='S') {x.push(i);y.push(j);}
              else
              if(C=='T') {n=i;m=j;}
            }
        }
        //printf("%d %d
    %d %d
    ",x.front(),y.front(),n,m);
        do
        {
            for(int i=0;i<4;++i)
              if(f.front()!=(i+2)%4)
              {
                int xx=x.front()+X[i],yy=y.front()+Y[i];
                if(!a[xx][yy]&&xx>=1&&xx<=r&&yy>=1&&yy<=c)
                {
                    b.push(b.front()+1);
                    x.push(xx);y.push(yy);
                    a[xx][yy]=1;f.push(i);
                    if(xx==n&&yy==m)
                    {
                        printf("%d",b.back());
                        return 0;
                    }
                }
              }
            x.pop();y.pop();
            b.pop();f.pop();
        }
        while(!x.empty());
        return 0;
    }
  • 相关阅读:
    git命令参考
    Raft 简介
    关于 K8S 的几个问题
    Ubuntu中用普通方法无法添加自启动
    so编译 链接和加载
    GDB调试命令详解
    MinGW中的头文件路径
    GDB使用详解
    dlopen、dlsym和dlclose的使用
    Windows下配置VSCode使用mingww64的gcc、g++编译器和GDB调试器
  • 原文地址:https://www.cnblogs.com/lbssxz/p/10740807.html
Copyright © 2020-2023  润新知