• 20200131今天是学习写bfs的我(新冠快走


    自己完整的敲一遍代码还是和看不一样,发现挺多问题

      1.标记放在入列之前比较好

      2.多维空间迷宫问题(方向距离

      3.G++测试MLE C++测试TLE是什么迷幻操作?

        但是发现了问题是真的

        以及  明明之前测试的时候发现了问题,还是忽略了它

    Trick & Skill

      1.清空队列:

        (1).直接赋值

        (2).逐个出列

        (3).Swap

        https://www.cnblogs.com/zhonghuasong/p/7524624.html

      2.生成随机数

    #include<ctime>
    int main(){
        srand(time(NULL));
        cout<<rand()%6;
    }
    

      3.查错

    ===================================分割线===================================

    今日代码

    题目 POJ-2251

    #include <iostream>
    #include <cstring>
    #include <queue>
    #include <math.h>
    #include <map>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    char a[35][35][35];
    int vis[35][35][35];
    int n,m,f;
    struct point{int l,x,y;};
    typedef pair<point,int>p;
    int dx[]={0,0,-1,1};
    int dy[]={1,-1,0,0};
    point now,newo,ende;
    queue<p> qu;
    inline int read(){
        int x=0,f=1;char c=getchar();
        while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
        while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
        return x*f;
    }
    
    inline void clean(){
        while(!qu.empty())qu.pop();
        memset(vis,0,sizeof(vis));
    }
    
    int bfs(){
        while(!qu.empty()){
            now=qu.front().first;
            int dis=qu.front().second;
    
            qu.pop();
            //cout<<"now: "<<now.l<<" "<<now.x<<" "<<now.y<<" "<<dis<<endl;//检验
            if(now.l==ende.l && now.x==ende.x && now.y==ende.y){printf("Escaped in %d minute(s).
    ",dis);return 0;}
            if(now.l+1<=f && (a[now.l+1][now.x][now.y]=='.' || a[now.l+1][now.x][now.y]=='E') && !vis[now.l+1][now.x][now.y]){
                now.l++;
                qu.push(p(now,dis+1));vis[now.l][now.x][now.y]=1;
                now.l--;
            }
            if(now.l-1>=0 && (a[now.l-1][now.x][now.y]=='.' || a[now.l-1][now.x][now.y]=='E') && !vis[now.l-1][now.x][now.y]){
                now.l--;
                qu.push(p(now,dis+1));vis[now.l][now.x][now.y]=1;
                now.l++;
            }
            newo.l=now.l;
            for(int i=0;i<4;i++){
                newo.x=now.x+dx[i];
                //cout<<"new1 x: "<<newo.l<<" "<<newo.x<<" "<<newo.y<<" "<<a[newo.l][newo.x][newo.y]<<endl;//检验
                if(newo.x<1 || newo.x>n)continue;
                newo.y=now.y+dy[i];
                //cout<<"new2 y: "<<newo.l<<" "<<newo.x<<" "<<newo.y<<" "<<a[newo.l][newo.x][newo.y]<<endl;//检验
                if(newo.y<1 || newo.y>m)continue;
                if(!vis[newo.l][newo.x][newo.y] && (a[newo.l][newo.x][newo.y]=='.' || a[newo.l][newo.x][newo.y]=='E')){
                    vis[newo.l][newo.x][newo.y]=1;qu.push(p(newo,dis+1));//coun++;cout<<coun<<endl;
                }
            }
        }
        cout<<"Trapped!"<<endl;
    }
    
    int main()
    {
    	while(cin>>f>>n>>m && n){
            char temp;
            clean();
            getchar();
            for(int j=1;j<=f;j++){
                for(int i=1;i<=n;i++){
                    for(int t=1;t<=m;t++){
                        temp=getchar();
                        a[j][i][t]=temp;
                        if(temp=='S'){now.l=j,now.x=i,now.y=t;}
                        if(temp=='E'){ende.l=j,ende.x=i,ende.y=t;}
                    }
                    getchar();
                }
                getchar();
            }
            qu.push(p(now,0));
            bfs();
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    [PHP动态]0001.关于 PHP 7 你必须知道的五件事
    [PHP工具推荐]0001.分析和解析代码的7大工具
    Java数据结构和算法(八)——递归
    Java数据结构和算法(七)——链表
    Java数据结构和算法(六)——前缀、中缀、后缀表达式
    Java数据结构和算法(五)——队列
    Java数据结构和算法(四)——栈
    Java数据结构和算法(三)——冒泡、选择、插入排序算法
    Java数据结构和算法(二)——数组
    Java数据结构和算法(一)——简介
  • 原文地址:https://www.cnblogs.com/tabshh/p/12245583.html
Copyright © 2020-2023  润新知