(简单) FZU 2150 Fire Game ,Floyd。

摘要:
代码如下:#include#include#include#include#includeusingspacestd;intmap1[15][15];利息[15][15];整数N,M;整数;美洲;intflo[12][12][12][1][12];booljudge(intti,inttj){if(ti˂=0||tjN| |tj˃M)return0;return1;}voidbfs(intsi,intsj,intst){queue<int>que;inttemp,ti,tj;que.push(si*100+sj);map1[si][sj]=st;而(!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);map 1[ti-1][tj]=st;}如果(判断(ti+1,tj)&&map1[ti+1][tj]==0){que push((ti+1)*100+tj);map1[ti+1][tj]=st;}如果(判断(ti,tj-1)&&map1[ti][tj-1]==0){que push(ti*100+tj-1;map1[ti][tj-1]=st;}如果(判断(ti,tj+1)&&map1[ti][tj+1]==0){que push(ti*100+tj+1);map1[ti][tj+1]=st;}}voidfloyd(){对于(inti1=1;i1˂=N;++i1)对于(inti2=1;i2˂=M;++i2)对于(int j1=1;j1˂=N;++j1)对于对于(int j2=1;j20&map1[j2]˃0)&&((i1==j1&&(i2-j2==1||i2-j2==-1))||(i2==j2&&(i1-j1==1||i1-j1==-1)k1][k2]˃0)对于(inti1=1;i1˂=N;++i1)(inti2=1;i20)对于(intj1=1;j1˂=N;++j1)对于(int j2=1;j2˂=M,++j2)如果][k2][j1][j2]);}intslove(){cou=0;memset(rem,-1,sizeof(rem));for(inti=1;i˂=N;++i)for(intj=1;j0){maxn=-10e8;对于(intj1=1,j1<=N;++j1)对于2]˃maxn){maxn=flo[i1][i2][j1][j2];if(maxn˃minn[map1[i1][i2])gotonext1;}next1:如果(最大值˂最小值[map1[i1][i2

  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倍以上,复杂度都一样的。。。

代码如下:

(简单) FZU 2150 Fire Game ,Floyd。第1张(简单) FZU 2150 Fire Game ,Floyd。第2张
#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;
}
View Code

免责声明:文章转载自《(简单) FZU 2150 Fire Game ,Floyd。》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇OSPF协议总结使用 SCons 轻松建造程序下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

db2 reorg详解

reorgchk,检查tableindex是否需要重组。reorg重组,重新放置数据位置。5)db.tb_reorg_req运行状况指示器处于ATTENTION状态。可以分为对系统表和用户表两部分分别进行REORGCHK:1)针对系统表进行REORGCHKdb2reorgchkupdatestatisticsontablesystem使用UPDATESTAT...

RF(一)RF的安装步骤

7.安装Appium 8,安装最新版本的nodeJS:node-v6.9.49,在命令行上执行Appium命令,您应该能够启动Appium服务器~~~~...

mysql修改字段防止锁表

步骤1:修改大表、addcolumn或dropcolumn的字段,操作完成后将锁定该表。此时,查询ok、insert和update将等待锁定。...

10 TCP限流技术

TCP流限制的原因是接收方可以完全接受消息,以确保数据安全而不会丢失。首先,窗口机制引入了发送方和接收方都有一个窗口。当发送方发送数据时,将发送落入窗口中的数据。当接收器接收到数据时,落入接收器窗口的数据将被接受。可以看出,流量会受到窗口大小II的限制。滑动窗口技术1TCP滑动窗口技术通过动态改变窗口大小来调整两台主机之间的数据传输。...

Corn表达式

CronTriggerCronTrigger通常比SimpleTrigger更有用。如果您需要基于日历的概念,而不是SimpleTrigger完全指定的时间间隔,则重复启动工作的时间表。CronTrigger,您可以指定触发器计划,例如“每周五中午”、“每工作日9:30”,甚至“每周一上午、周三和周五9:00和10:00每五分钟”。即使如此,就像Simple...

文件(夹)对比利器WinMerge

IDE中自带的svn功能较弱,还好有winMerge弥补了它的缺陷,它可以对比文件、文件夹,使用起来还是较为方便,界面也是中文。“开始”菜单,弹出对话框中选择需要进行对比的文件夹或文件然后选择一个过滤器,它自带就可以过滤掉svn目录,如需要过滤其它一些指定的目录,则需要自己修改过滤器的规则了,也很简单。...