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. CDockablePane 记忆界面布局的问题

    CWinAppEx类的LoadCustomState()和SaveCustomState()用于向注册表读取和保存应用程序的界面信息,重载该方法可以取消自动记忆界面布局. void CxxxApp:: ...

  2. Android中布局文件中使用onClick属性

    安卓开发中,布局文件中的控件有一个属性,是onClick,例如:           <Button             android:id="@+id/button1" ...

  3. android笔记20170116

    封装http请求类,利用回调机制获取返回值 public interface HttpCallbackListener { void onFinish(String response); void o ...

  4. http://www.iteye.com/job/topic/1133159

    Lucene 的索引体系是一个写独占,读共享的结构,这意味着,我们在使用多线程进行添加索引时,性能并不会得到明显的提升,所以任何时刻只能有一个线程对索引进行写 入操作,而保障这个操作的安全性则是来自于 ...

  5. Android Studio 连接真机调试

    以小米4为例,先将手机通过USB连接电脑,在设备管理器中确保驱动安装正确. 对手机的设置 1.设置手机为开发者模式(设置->关于手机->连续点击MIUI版本--开启成功) 2.在更多设置中 ...

  6. Windows API 之 CreateThread、GetExitCodeThread(未完)

    GetExitCode Retrieves the termination status of the specified thread. BOOL WINAPI GetExitCodeThread( ...

  7. Zencart批量删除无图片产品

    Zencart批量删除无图片产品 2012-04-23 07:26:18|  分类: 默认分类 |字号 订阅 转自 http://zhongjia33.blog.163.com/blog/#m=0   ...

  8. 优化Android App性能?十大技巧必知!

    无论锤子还是茄子手机的不断冒出,Android系统的手机市场占有率目前来说还是最大的,因此基于Android开发的App数量也是很庞大的.那么,如何能开发出更高性能的Android App?相信是软件 ...

  9. iOS中FMDB和GCD剖析

    转载至:http://www.cocoachina.com/industry/20130819/6821.html 英文链接:https://github.com/ccgus/fmdb 由于FMDB是 ...

  10. script 两则

    script1: PATH=/usr/local/bin:/usr/bin:$PATH:. . $HOME/utility/macro/macro.env OVO_DIR=/tmp LOGFILE=$ ...