zoukankan      html  css  js  c++  java
  • (简单) UVA 11624 Fire! ,BFS。

      Description

      Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze.

      Given Joe's location in the maze and which squares of the maze are on fire, you must determine whether Joe can exit the maze before the fire reaches him, and how fast he can do it.

      Joe and the fire each move one square per minute, vertically or horizontally (not diagonally). The fire spreads all four directions from each square that is on fire. Joe may exit the maze from any square that borders the edge of the maze. Neither Joe nor the fire may enter a square that is occupied by a wall.

      题目就是迷宫问题,然后加了火,不过这个题被坑了,原来火不只是一个,可以有很多个。。。

      这个题先从火BFS一遍,这样就可以知道火多长时间后烧到某一块,然后从人BFS,当走到这时火已经烧过来的话就算不能通过就好了。

    代码如下:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<cmath>
    
    using namespace std;
    
    bool map1[1005][1005];
    int rem[1005][1005];
    int Frem[1005][1005];
    int N,M;
    int Si,Sj,Fi[1000006],Fj[1000006];
    int cou;
    
    bool judge(int x,int y,int temp)
    {
        if(x<=0||y<=0||x>N||y>M)
            return 0;
    
        if(!map1[x][y])
            return 0;
        
        if(rem[x][y])
            return 0;
    
        if(Frem[x][y]>-1&&temp>=Frem[x][y])
            return 0;
    
        return 1;
    }
    
    bool judge1(int x,int y)
    {
        if(x<=0||y<=0||x>N||y>M)
            return 0;
    
        if(!map1[x][y])
            return 0;
    
        if(Frem[x][y]!=-1)
            return 0;
    
        return 1;
    }
    
    int slove()
    {
        queue <int> que;
        int temp,t1,t2;
    
        que.push(Si*1010+Sj);
        rem[Si][Sj]=1;
    
        while(!que.empty())
        {
            temp=que.front();
            que.pop();
    
            t1=temp/1010;
            t2=temp%1010;
            temp=rem[t1][t2];
    
            if(t1==1||t2==1||t1==N||t2==M)
                return temp;
    
            --t1;
            if(judge(t1,t2,temp))
            {
                que.push(t1*1010+t2);
                rem[t1][t2]=temp+1;
            }
            t1+=2;
            if(judge(t1,t2,temp))
            {
                que.push(t1*1010+t2);
                rem[t1][t2]=temp+1;
            }
            --t1;
            --t2;
            if(judge(t1,t2,temp))
            {
                que.push(t1*1010+t2);
                rem[t1][t2]=temp+1;
            }
            t2+=2;
            if(judge(t1,t2,temp))
            {
                que.push(t1*1010+t2);
                rem[t1][t2]=temp+1;
            }
        }
    
        return -1;
    }
    
    void init()
    {
        queue <int> que;
        int temp,t1,t2;
        for(int i=0;i<cou;++i)
        {
            que.push(Fi[i]*1010+Fj[i]);
            Frem[Fi[i]][Fj[i]]=0;
        }
    
        while(!que.empty())
        {
            temp=que.front();
            que.pop();
    
            t1=temp/1010;
            t2=temp%1010;
            temp=Frem[t1][t2];
    
            --t1;
            if(judge1(t1,t2))
            {
                que.push(t1*1010+t2);
                Frem[t1][t2]=temp+1;
            }
            t1+=2;
            if(judge1(t1,t2))
            {
                que.push(t1*1010+t2);
                Frem[t1][t2]=temp+1;
            }
            --t1;
            --t2;    
            if(judge1(t1,t2))
            {
                que.push(t1*1010+t2);
                Frem[t1][t2]=temp+1;
            }
            t2+=2;
            if(judge1(t1,t2))
            {
                que.push(t1*1010+t2);
                Frem[t1][t2]=temp+1;
            }
        }
    }
    
    int main()
    {
        ios::sync_with_stdio(false);
    
        int T;
        int ans;
        char c;
        cin>>T;
    
        while(T--)
        {
            cou=0;
            cin>>N>>M;
            for(int i=1;i<=N;++i)
                for(int j=1;j<=M;++j)
                {
                    rem[i][j]=0;
                    Frem[i][j]=-1;
                    cin>>c;
                    if(c=='#'||c=='F')
                        map1[i][j]=0;
                    else
                        map1[i][j]=1;
    
                    if(c=='J')
                        Si=i,Sj=j;
                    if(c=='F')
                    {
                        Fi[cou]=i;
                        Fj[cou++]=j;
                    }
                }
    
            init();
    
            ans=slove();
    
            if(ans==-1)
                cout<<"IMPOSSIBLE
    ";
            else
                cout<<ans<<endl;
        }
    
        return 0;
    }
    View Code
  • 相关阅读:
    Entity Framework:第三方开发MySQL,Oracle,SQLite ADO.NET Provider支持Entity Framework
    添加WCF服务引用失败解决办法
    [笔记]iBatisNET配置问题
    [转]Silverlight 使用Isolate Storage进行客户端数据缓存
    [转]Oralce之时间转换用法 TO_CHAR(DATE,FORMAT)
    在ASP.NET3.5下利用Linq,Ajax创建一个线上网络聊天室
    解决水晶报表发布后报错:不支持的操作。无法在 C++ 堆栈中打开由 JRC 引擎处理的文档。
    Windows Live Writer
    [转]VS2010中,无法嵌入互操作类型“……”,请改用适用的接口的解决方法
    SQL Server 无法生成 FRunCM 线程。请查看 SQL Server 错误日志和 Windows 事件日志
  • 原文地址:https://www.cnblogs.com/whywhy/p/4229921.html
Copyright © 2011-2022 走看看