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[][];
int rem[][];
int N,M;
int minans;
int cou;
int flo[][][][]; bool judge(int ti,int tj)
{
if(ti<=||tj<=||ti>N||tj>M)
return ; return ;
} void bfs(int si,int sj,int st)
{
queue <int> que;
int temp,ti,tj; que.push(si*+sj);
map1[si][sj]=st; while(!que.empty())
{
temp=que.front();
que.pop(); ti=temp/;
tj=temp%; if(judge(ti-,tj)&&map1[ti-][tj]==)
{
que.push((ti-)*+tj);
map1[ti-][tj]=st;
}
if(judge(ti+,tj)&&map1[ti+][tj]==)
{
que.push((ti+)*+tj);
map1[ti+][tj]=st;
}
if(judge(ti,tj-)&&map1[ti][tj-]==)
{
que.push(ti*+tj-);
map1[ti][tj-]=st;
}
if(judge(ti,tj+)&&map1[ti][tj+]==)
{
que.push(ti*+tj+);
map1[ti][tj+]=st;
}
} } void floyd()
{
for(int i1=;i1<=N;++i1)
for(int i2=;i2<=M;++i2)
for(int j1=;j1<=N;++j1)
for(int j2=;j2<=M;++j2)
if(i1==j1&&i2==j2)
flo[i1][i2][j1][j2]=;
else if((map1[i1][i2]>&&map1[j1][j2]>)&&((i1==j1&&(i2-j2==||i2-j2==-))||(i2==j2&&(i1-j1==||i1-j1==-))))
flo[i1][i2][j1][j2]=;
else
flo[i1][i2][j1][j2]=10e7; for(int k1=;k1<=N;++k1)
for(int k2=;k2<=M;++k2)
if(map1[k1][k2]>)
for(int i1=;i1<=N;++i1)
for(int i2=;i2<=M;++i2)
if(map1[i1][i2]>)
for(int j1=;j1<=N;++j1)
for(int j2=;j2<=M;++j2)
if(map1[j1][j2]>)
flo[i1][i2][j1][j2]=min(flo[i1][i2][j1][j2],flo[i1][i2][k1][k2]+flo[k1][k2][j1][j2]);
} int slove()
{
cou=; memset(rem,-,sizeof(rem));
for(int i=;i<=N;++i)
for(int j=;j<=M;++j)
if(map1[i][j]==)
{
++cou;
if(cou==)
return -; bfs(i,j,cou); } int temp,temp1;
int maxn=-10e8;
minans=10e8;
int minn[]={10e8,10e8,10e8}; if(cou==)
return ; floyd(); if(cou==)
{
for(int i1=;i1<=N;++i1)
for(int i2=;i2<=M;++i2)
if(map1[i1][i2]>)
{
maxn=-10e8;
for(int j1=;j1<=N;++j1)
for(int j2=;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[],minn[]);
}
else
{
for(int i1=;i1<=N;++i1)
for(int i2=;i2<=M;++i2)
for(int j1=;j1<=N;++j1)
for(int j2=;j2<=M;++j2)
if(map1[i1][i2]>&&map1[j1][j2]>)
{
maxn=-10e8;
for(int k1=;k1<=N;++k1)
for(int k2=;k2<=M;++k2)
if(map1[k1][k2]>)
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=;cas<=T;++cas)
{
cin>>N>>M; for(int i=;i<=N;++i)
for(int j=;j<=M;++j)
{
cin>>c;
if(c=='#')
map1[i][j]=;
else
map1[i][j]=-;
} ans=slove(); cout<<"Case "<<cas<<": ";
if(ans==-)
cout<<-<<endl;
else
cout<<ans<<endl;
} return ;
}

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

  1. FZU 2150 Fire Game(点火游戏)

    FZU 2150 Fire Game(点火游戏) Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Description - 题目描述 ...

  2. FZU 2150 Fire Game

    Fire Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

  3. fzu 2150 Fire Game 【身手BFS】

    称号:fzupid=2150"> 2150 Fire Game :给出一个m*n的图,'#'表示草坪,' . '表示空地,然后能够选择在随意的两个草坪格子点火.火每 1 s会向周围四个 ...

  4. FZU 2150 fire game (bfs)

    Problem 2150 Fire Game Accept: 2133    Submit: 7494Time Limit: 1000 mSec    Memory Limit : 32768 KB ...

  5. FZU 2150 Fire Game (暴力BFS)

    [题目链接]click here~~ [题目大意]: 两个熊孩子要把一个正方形上的草都给烧掉,他俩同一时候放火烧.烧第一块的时候是不花时间的.每一块着火的都能够在下一秒烧向上下左右四块#代表草地,.代 ...

  6. (FZU 2150) Fire Game (bfs)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150 Problem Description Fat brother and Maze are playing ...

  7. FZU 2150 Fire Game 广度优先搜索,暴力 难度:0

    http://acm.fzu.edu.cn/problem.php?pid=2150 注意这道题可以任选两个点作为起点,但是时间仍足以穷举两个点的所有可能 #include <cstdio> ...

  8. FZU 2150 Fire Game (bfs+dfs)

    Problem Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board ...

  9. FZU 2150 Fire Game 【两点BFS】

    Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns) ...

随机推荐

  1. 上海赛趣-top.mainFrame.tabAddHandler方法详解

    top.mainFrame.tabAddHandler("item"+Id,'项目:'+itemname,'<%=basePath%>bizitem/goEditIte ...

  2. n++与++n的区别

    n++ 是先执行n++再进行赋值返回的只却是n. ++n 是先赋值之后再执行++n. 其实执行 n++ and ++n 都算是一次赋值 所以若 n = n++ and n = ++n 其实就是2次赋值 ...

  3. Ubuntu下修改DNS重启也能用的方法

    1.通过修改:/etc/resolvconf/resolv.conf.d/base(这个文件默认是空的)实现 内容填上需要修改的nameserver

  4. linux视频学习3(shell和网络)

    1.shell的学习. shell的种类比较多,主要有三种: /bin/sh, /bin/csh, /bin/ksh. 查看当前使用的是哪种shell : 命令env (显示当前操作系统的环境变量). ...

  5. mysql 修改 添加 删除 表字段

    添加表的字段    alter table 表名  add  字段名  字段的类型 例子:        alter table table1 add transactor varchar(10) n ...

  6. this的应用

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  7. HDU 4460 Friend Chains(map + spfa)

    Friend Chains Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

  8. andorid 自定义view属性declare-styleable

    1. reference:参考某一资源ID. (1)属性定义: <declare-styleable name = "名称"> <attr name = &quo ...

  9. PAT (Advanced Level) 1107. Social Clusters (30)

    简单并查集. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  10. JSON对象长度和遍历方法(转)

    最 近在修改一个HTML页面的JS的时候遍历JSON对象,却怎么也调试不通过.怪这个HTML网页不知道用了什么方法禁止了js错误提示,刚开始的时候不 知道有这个问题,用chrome的开发人员工具都没发 ...