(简单) 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[][];
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。的更多相关文章
- FZU 2150 Fire Game(点火游戏)
FZU 2150 Fire Game(点火游戏) Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description - 题目描述 ...
- FZU 2150 Fire Game
Fire Game Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- fzu 2150 Fire Game 【身手BFS】
称号:fzupid=2150"> 2150 Fire Game :给出一个m*n的图,'#'表示草坪,' . '表示空地,然后能够选择在随意的两个草坪格子点火.火每 1 s会向周围四个 ...
- FZU 2150 fire game (bfs)
Problem 2150 Fire Game Accept: 2133 Submit: 7494Time Limit: 1000 mSec Memory Limit : 32768 KB ...
- FZU 2150 Fire Game (暴力BFS)
[题目链接]click here~~ [题目大意]: 两个熊孩子要把一个正方形上的草都给烧掉,他俩同一时候放火烧.烧第一块的时候是不花时间的.每一块着火的都能够在下一秒烧向上下左右四块#代表草地,.代 ...
- (FZU 2150) Fire Game (bfs)
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150 Problem Description Fat brother and Maze are playing ...
- FZU 2150 Fire Game 广度优先搜索,暴力 难度:0
http://acm.fzu.edu.cn/problem.php?pid=2150 注意这道题可以任选两个点作为起点,但是时间仍足以穷举两个点的所有可能 #include <cstdio> ...
- 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 ...
- 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) ...
随机推荐
- wpf中ListBox的选中项与ComboBox间的绑定
产品类: public class Product:NotificationObject { private int productID; public int ProductID { get { r ...
- Mishka and Interesting sum
Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes input ...
- POJ 2182/暴力/BIT/线段树
POJ 2182 暴力 /* 题意: 一个带有权值[1,n]的序列,给出每个数的前面比该数小的数的个数,当然比一个数前面比第一个数小的个数是0,省略不写,求真正的序列.(拗口) 首先想到的是从前到后暴 ...
- JS-运动基础(一)续
2.淡入淡出的图片 用变量存储透明度 <title>无标题文档</title> <style> #div1{width:293px; height:220px; b ...
- HDU1865--More is better(统计并查集的秩(元素个数))
More is better Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others) ...
- 读《Ext.JS.4.First.Look》随笔
Ext JS 4是最大的改革已经取得了Ext框架.这些变化包括一个新类系统,引入一个新的平台,许多API变化和改进,和新组件,如新图表和新画组件.Ext JS 4是更快,更稳定,易于使用.(注意:Ex ...
- 用MyEclipse自动生成hibernate映射文件和实体类
创建数据库,创建相应的表 点击图标,选择MyEclipse Datebase Explorer 右击空白区域,选择new菜单,根据提示创建数据库连接,创建好后会显示你所创建的连接名,如图mysqldb ...
- hdu_5691_Sitting in Line(状压DP)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5691 题意:中文,不解释 题解:设dp[i][j]表示当前状态为i,以第j个数为末尾的最忧解,然后dp ...
- java开发地三天——数据库介绍
又是一天萌萌哒地过去了,今天是处理数据库的部分.SQL Server 2008,这东西是上学期搞MFC的时候接触到的,那时候话说安装就是一个大问题,然后在学SQL语句的时候感觉还好,一切都还过得去.现 ...
- 使用print2flash开发在线文档
www.print2flash.com 命令行调用: A:\Program Files (x86)\Print2Flash3>p2fServer.exe a.pdf a.swf