zoukankan      html  css  js  c++  java
  • FZU 2124 bfs+vis记录

    第一次团队训练赛的题 自己看完题没看到不能用舌头吃道具..以为是什么贪心混合bfs..果断放弃..悄悄的背锅了

    然后其实比较简单 只是利用vis记录的时候要分两种状态记录 有没有道具

    每到一个地方 就朝四个方向都尝试一下能不能吃到 如果吃到了就更新ans

    需要注意的是刚开始就要尝试四个方向

    vis[2][25][25]的第一维记录道具状态

    一开始认为图太小 不用vis 然而不用vis就会出现跳不出循环的状况

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<math.h>
    #include<map>
    #include<queue>
    using namespace std;
    char ma[25][25];
    bool vis[2][25][25];///0 没有 1 有了
    int n,m;
    int dx[4]={-1,1,0,0};
    int dy[4]={0,0,-1,1};
    bool check(int x,int y)
    {
        if(x>=0&&x<n&&y>=0&&y<m)
        return true;
        return false;
    }
    double ans;
    struct node
    {
        int x;
        int y;
        double time;
        bool daoju;
    };
    bool ok;
    double find(int x,int y)
    {
        ///1
        double a=0.0;
        int xx,yy;
        xx=x;
        yy=y;
        while(xx>0)
        {
            xx--;
            a+=0.2;
            if(ma[xx][yy]=='X')
            break;
            if(ma[xx][yy]=='B')
            {
                ok=true;
                return a;
            }
        }
        a=0.0;
        xx=x;
        yy=y;
        while(xx<n-1)
        {
            xx++;
            a+=0.2;
            if(ma[xx][yy]=='X')
            break;
            if(ma[xx][yy]=='B')
            {
                ok=true;
                return a;
            }
        }
        a=0.0;
        xx=x;
        yy=y;
        while(yy>0)
        {
            yy--;
            a+=0.2;
            if(ma[xx][yy]=='X')
            break;
            if(ma[xx][yy]=='B')
            {
                ok=true;
                return a;
            }
        }
        a=0.0;
        xx=x;
        yy=y;
        while(yy<m-1)
        {
            yy++;
            a+=0.2;
            if(ma[xx][yy]=='X')
            break;
            if(ma[xx][yy]=='B')
            {
                ok=true;
                return a;
            }
        }
        return 200.0;
    }
    void bfs(int x,int y)
    {
        vis[0][x][y]=false;
        node a;
        a.x=x;
        a.y=y;
        a.time=0.0;
        a.daoju=false;
        double aa=find(a.x,a.y);
        if(aa<150.0)
        {
            double b=aa+a.time;
            if(b<ans)
            {
                ans=b;
                ok=true;
            }
        }
        queue<node >q;
        q.push(a);
        while(!q.empty())
        {
            node b;
            b=q.front();q.pop();
            node c;
            for(int i=0;i<4;i++)
            {
                c=b;
                c.x+=dx[i];
                c.y+=dy[i];
                int v;
                if(c.daoju==false)
                v=0;
                else v=1;
                if(check(c.x,c.y)&&vis[v][c.x][c.y]==true)
                {
                    if(ma[c.x][c.y]!='X')
                    {
                        vis[v][c.x][c.y]=false;
                        if(c.daoju==false)
                        c.time+=1.0;
                        else c.time+=0.5;
                        if(ma[c.x][c.y]=='B')
                        {
                            if(c.time<ans)
                            ans=c.time;
                        }
                        else if(ma[c.x][c.y]=='S')
                        {
                            c.daoju=true;
                            double a=find(c.x,c.y);
                            if(a<150.0)
                            {
                                ok=true;
                                double b=a+c.time;
                                if(b<ans)
                                ans=b;
                            }
                            q.push(c);
                        }
                        else
                        {
                            double a=find(c.x,c.y);
                            if(a<150.0)
                            {
                                ok=true;
                                double b=a+c.time;
                                if(b<ans)
                                ans=b;
                            }
                            q.push(c);
                        }
                    }
                }
            }
        }
    }
    int main(){
    while(~scanf("%d%d",&n,&m))
    {
        memset(vis,true,sizeof(vis));
        for(int i=0;i<n;i++)
        {
            scanf("%s",ma[i]);
        }
        ok=false;
        ans=9999.0;
        for(int i=0;i<n;i++)
        {
            for(int k=0;k<n;k++)
            {
                if(ma[i][k]=='P')
                {
                    bfs(i,k);
                    if(ok)
                    printf("%.1f
    ",ans);
                    else printf("-1
    ");
                }
            }
        }
    }
    }
    
  • 相关阅读:
    【备忘录】Sublime Text编辑器如何在选中的多行行首增加字符串
    微信卡券领取页面提示签名错误,微信卡券JSAPI签名校验工具对比签名一模一样,cardExt扩展字段有问题
    程序运行时动态生成缓存时存在的问题
    golang的beego框架开发时出现的问题纪录
    【备忘录】CentOS服务器mysql忘记root密码恢复
    试玩swoole扩展 第一天
    spring 定时任务执行2次
    JVM CUP占用率过高排除方法,windows环境
    spring cloud 服务A调用服务B自定义token消失,记录
    java.sql.SQLException: Value '0000-00-00' can not be represented as java.sql.Timestamp
  • 原文地址:https://www.cnblogs.com/rayrayrainrain/p/5342834.html
Copyright © 2011-2022 走看看