(简单) FZU 2150 Fire Game ,Floyd。

  Problem Description

  Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. Firstly they choose two grids which are consisting of grass and set fire. As we all know, the fire can spread among the grass. If the grid (x, y) is firing at time t, the grid which is adjacent to this grid will fire at time t+1 which refers to the grid (x+1, y), (x-1, y), (x, y+1), (x, y-1). This process ends when no new grid get fire. If then all the grid which are consisting of grass is get fired, Fat brother and Maze will stand in the middle of the grid and playing a MORE special (hentai) game. (Maybe it’s the OOXX game which decrypted in the last problem, who knows.)

  You can assume that the grass in the board would never burn out and the empty grid would never get fire.

  Note that the two grids they choose can be the same.

  这个题是要求找到放火的地方,然后能够最快烧完,刚开始想的是用BFS,首先枚举每两个点,然后BFS,10^6的复杂度,可是居然超时了,可能是用的STL,然后就想各种减枝,后来就直接用了Floyd,就是求任意两个点之间的最短距离,然后再枚举每两个点,求出最小值,居然只用了109ms,想不通居然快了10倍以上,复杂度都一样的。。。

代码如下:

#include<iostream>
#include<cstring>
#include<queue>
#include<ctime>
#include<cstdio>

using namespace std;

int map1[15][15];
int rem[15][15];
int N,M;
int minans;
int cou;
int flo[12][12][12][12];

bool judge(int ti,int tj)
{
    if(ti<=0||tj<=0||ti>N||tj>M)
        return 0;

    return 1;
}

void bfs(int si,int sj,int st)
{
    queue <int> que;
    int temp,ti,tj;

    que.push(si*100+sj);
    map1[si][sj]=st;

    while(!que.empty())
    {
        temp=que.front();
        que.pop();

        ti=temp/100;
        tj=temp%100;

        if(judge(ti-1,tj)&&map1[ti-1][tj]==0)
        {
            que.push((ti-1)*100+tj);
            map1[ti-1][tj]=st;
        }
        if(judge(ti+1,tj)&&map1[ti+1][tj]==0)
        {
            que.push((ti+1)*100+tj);
            map1[ti+1][tj]=st;
        }
        if(judge(ti,tj-1)&&map1[ti][tj-1]==0)
        {
            que.push(ti*100+tj-1);
            map1[ti][tj-1]=st;
        }
        if(judge(ti,tj+1)&&map1[ti][tj+1]==0)
        {
            que.push(ti*100+tj+1);
            map1[ti][tj+1]=st;
        }
    }

}

void floyd()
{
    for(int i1=1;i1<=N;++i1)
        for(int i2=1;i2<=M;++i2)
            for(int j1=1;j1<=N;++j1)
                for(int j2=1;j2<=M;++j2)
                    if(i1==j1&&i2==j2)
                        flo[i1][i2][j1][j2]=0;
                    else if((map1[i1][i2]>0&&map1[j1][j2]>0)&&((i1==j1&&(i2-j2==1||i2-j2==-1))||(i2==j2&&(i1-j1==1||i1-j1==-1))))
                        flo[i1][i2][j1][j2]=1;
                    else
                        flo[i1][i2][j1][j2]=10e7;

    for(int k1=1;k1<=N;++k1)
        for(int k2=1;k2<=M;++k2)
            if(map1[k1][k2]>0)
            for(int i1=1;i1<=N;++i1)
                for(int i2=1;i2<=M;++i2)
                if(map1[i1][i2]>0)
                    for(int j1=1;j1<=N;++j1)
                        for(int j2=1;j2<=M;++j2)
                            if(map1[j1][j2]>0)
                                flo[i1][i2][j1][j2]=min(flo[i1][i2][j1][j2],flo[i1][i2][k1][k2]+flo[k1][k2][j1][j2]);
}

int slove()
{
    cou=0;

    memset(rem,-1,sizeof(rem));
    for(int i=1;i<=N;++i)
        for(int j=1;j<=M;++j)
            if(map1[i][j]==0)
            {
                ++cou;
                if(cou==3)
                    return -1;

                bfs(i,j,cou);

            }

    int temp,temp1;
    int maxn=-10e8;
    minans=10e8;
    int minn[3]={10e8,10e8,10e8};

    if(cou==0)
        return 0;

    floyd();

    if(cou==2)
    {
        for(int i1=1;i1<=N;++i1)
            for(int i2=1;i2<=M;++i2)
                if(map1[i1][i2]>0)
            {
                maxn=-10e8;
                for(int j1=1;j1<=N;++j1)
                    for(int j2=1;j2<=M;++j2)
                        if(flo[i1][i2][j1][j2]<10e7&&flo[i1][i2][j1][j2]>maxn)
                        {
                            maxn=flo[i1][i2][j1][j2];
                            if(maxn>minn[map1[i1][i2]])
                                goto next1;
                        }

            next1:
                if(maxn<minn[map1[i1][i2]])
                    minn[map1[i1][i2]]=maxn;
            }

        return max(minn[1],minn[2]);
    }
    else
    {
        for(int i1=1;i1<=N;++i1)
            for(int i2=1;i2<=M;++i2)
                for(int j1=1;j1<=N;++j1)
                    for(int j2=1;j2<=M;++j2)
                        if(map1[i1][i2]>0&&map1[j1][j2]>0)
                        {
                            maxn=-10e8;
                            for(int k1=1;k1<=N;++k1)
                                for(int k2=1;k2<=M;++k2)
                                    if(map1[k1][k2]>0)
                                        if(min(flo[i1][i2][k1][k2],flo[j1][j2][k1][k2])>maxn)
                                        {
                                            maxn=min(flo[i1][i2][k1][k2],flo[j1][j2][k1][k2]);
                                            if(maxn>minans)
                                                goto next2;
                                        }

                        next2:
                            if(maxn<minans)
                                minans=maxn;
                        }

        return minans;
    }
}

int main()
{
    ios::sync_with_stdio(false);

    int T;
    char c;
    int ans;
    cin>>T;

    for(int cas=1;cas<=T;++cas)
    {
        cin>>N>>M;

        for(int i=1;i<=N;++i)
            for(int j=1;j<=M;++j)
            {
                cin>>c;
                if(c==‘#‘)
                    map1[i][j]=0;
                else
                    map1[i][j]=-1;
            }

        ans=slove();

        cout<<"Case "<<cas<<": ";
        if(ans==-1)
            cout<<-1<<endl;
        else
            cout<<ans<<endl;
    }

    return 0;
}

时间: 2024-11-05 21:00:11

(简单) FZU 2150 Fire Game ,Floyd。的相关文章

FZU 2150 Fire Game(点火游戏)

p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-size: 10.5000pt } h2 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 18.0000pt } h3 {

FZU 2150 Fire Game --两点同步搜索

枚举两点,然后同步BFS,看代码吧,很容易懂的. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #define Mod 1000000007 using namespace std; struct Po

FZU 2150 Fire Game (暴力BFS)

[题目链接]click here~~ [题目大意]: 两个熊孩子要把一个正方形上的草都给烧掉,他俩同时放火烧,烧第一块的时候是不花时间的,每一块着火的都可以在下一秒烧向上下左右四块#代表草地,.代表着不能烧的.问你最少花多少时间可以烧掉,如果烧不掉就输出-1 [解题思路]: 数据比较弱的情况下直接暴力枚举每块草坪上可以放的位置,比较高端的写法目前没有想到,以后想到了文章更新下~~ ps:由于一个细节没注意,导致WA了几乎一页,还以为FZU 判题出错了,后来突然发现每次从队列里拿出队首的元素,才是

FZU 2150 Fire Game(DFS+BFS)

题意  在n*m个格子组成的草地上   你可以选择两个是草('#')的格子点燃  每个点燃的格子在下一秒其四个相邻的是草的格子也会被点燃   问点燃所有的草至少需要多少秒 DFS和BFS的综合  如果'#'连通块的数量大于2个是肯定不能点燃所有的  先dfs判断连通块个数  再bfs找出选哪两个格子可以最快把草烧完 #include <map> #include <cstdio> #include <cstring> using namespace std; const

(FZU 2150) Fire Game (bfs)

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150 Problem Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty a

fzu 2150 Fire Game 【技巧BFS】

题目:fzu2150 Fire Game 题意:给出一个m*n的图,'#'表示草坪,' . '表示空地,然后可以选择在任意的两个草坪格子点火,火每 1 s会向周围四个格子扩散,问选择那两个点使得燃烧所有的草坪花费时间最小? 分析:这个题目如果考虑技巧的话有点难度,但是鉴于数据范围比较小,我们可以暴力枚举任意的草坪所在的点,然后两个点压进队列里面BFS,去一个满足条件的最小值即可. 顺便说一下 fzu 2141 Sub-Bipartite Graph 的思路,比赛的时候没有做出来. 这个题目想的复

FZU 2150 Fire Game(BFS)

Problem Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. Firstl

FZU 2150 Fire Game(BFS)

Fire Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is cons

FZU 2150 Fire Game (高姿势bfs--两个起点)

Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. Firstly they c