zoukankan      html  css  js  c++  java
  • UVa11624-Fire!(bfs)

    题目链接

    分析:
    火是不会自动熄灭的
    所以我们先预处理出火蔓延到每一个格子的最早时间
    然后bfsJoe的行动路线就可以了

    tip

    我把所有的信息都放在一个矩阵中维护
    结果就一直WA
    然后我就把火和地图两个信息分别维护
    就变成了RE
    (鬼知道为什么。。。有人愿意解答吗???!!!)

    //这里写代码片
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    
    using namespace std;
    
    const int N=1002;
    int zz[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
    int mp[N][N],xx,yy;
    int n,m,f[N][N];
    int q[N*N][2],tou,wei;
    
    void bfs1()
    {
        do
        {
            int x=q[++tou][0];
            int y=q[tou][1];
            int time=f[x][y]+1;
            int xa,ya;
            for (int i=0;i<4;i++)
            {
                xa=x+zz[i][0];
                ya=y+zz[i][1];
                if (xa>n||ya>m||xa<1||ya<1||f[xa][ya]!=0||mp[xa][ya]==-1) continue;
                f[xa][ya]=time;
                wei++;
                q[wei][0]=xa;
                q[wei][1]=ya;
            }
        }
        while (tou<wei);
    }
    
    int bfs2()
    {
        tou=wei=0;
        q[++wei][0]=xx;
        q[wei][1]=yy;
        mp[xx][yy]=1;
        do
        {
            int x=q[++tou][0];
            int y=q[tou][1];
            int xa,ya;
            if (x>=n||y>=m||x<=1||y<=1) return mp[x][y];
            int time=mp[x][y]+1;
            for (int i=0;i<4;i++)
            {
                xa=x+zz[i][0];
                ya=y+zz[i][1];
                if (mp[xa][ya]==-1||f[xa][ya]<=time) continue;
                mp[xa][ya]=time;
                wei++;
                q[wei][0]=xa;
                q[wei][1]=ya;
            }
        }
        while (tou<wei);
        return 0;
    }
    
    int main()
    {
        int T;
        scanf("%d",&T);
        while (T--)
        {
            memset(mp,0,sizeof(mp));
            memset(f,0,sizeof(f));
            tou=wei=0;
            scanf("%d%d",&n,&m);
            char s[N];
            for (int i=1;i<=n;i++)
            {
                scanf("%s",&s);
                for (int j=0;j<m;j++)
                    if (s[j]=='#') mp[i][j+1]=-1;
                    else if (s[j]=='F')
                    {
                        q[++wei][0]=i;
                        q[wei][1]=j+1;
                        f[i][j+1]=1;
                    }
                    else if (s[j]=='J') xx=i,yy=j+1;
            }
            bfs1();   //fire
            int ans=bfs2();
            if (ans>0)  //Jeo
                printf("%d
    ",ans);   
            else printf("IMPOSSIBLE
    ");
        }
        return 0;
    }
  • 相关阅读:
    关于.net core https支持的整理
    数据库动态字段 个人设计的思考
    解决PowerDesigner中table的code长度限制和column的code长度限制
    Git内网服务器搭建
    解决.net core读取appSetting.json文件中文字符乱码
    excel 转换成pdf 总结
    .net生成荣誉证书
    Adobe Acrobat DC 安装
    swagger生成错误问题 汇总解决
    svn或git 提交文件排除
  • 原文地址:https://www.cnblogs.com/wutongtong3117/p/7673031.html
Copyright © 2011-2022 走看看