(简单) 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) ...
随机推荐
- 【素数】 poj 2739 一个数能有多少种连续素数相加方案
简单题 素数打表 根据数据量 用n2算法遍历 开一个save[k]素数存前k个素数和即可. #include <iostream> #include <cstdio> ...
- Jquery树控件ZTree异步加载
异步加载的意思就是: 当点击展开树节点时,才去请求后台action返回点击节点的子节点数据并加载. 这里面主要设计ztree的setting变量的async属性设置: var setting = { ...
- Hibernate 系列教程7-双向一对一
双向一对一 一对一主要用在 一个一方需要的信息比较少,比如注册的登录信息 另一个一方存储的信息比较多,比如注册之后用户填写的详细信息 实现方式常用的主要有2种: java模型都是一样,其中一个映射文件 ...
- [转]解决LinearLayout中控件不能居右对齐
在LinearLayout布局时使用右对齐(android:layout_gravity="right")控件对齐方式不生效,需要设置 android:layout_weight= ...
- JS定时器的使用--延时提示框
<title>无标题文档</title> <style> div{float:left;margin:10px;} #div1{width:50px; height ...
- MVC 使用Jquery的$.post传递参数
MVC中,如果要使用 $.post 给 COntroller 传递参数,需要类实现 属性 get set,这样才行
- 使用PHP实现文件上传和多文件上传
PHP 2013 年 9 月 4 日 暂无评论 在PHP程序开发中,文件上传是一个使用非常普遍的功能,也是PHP程序员的必备技能之一.值得高兴的是,在PHP中实现文件上传功能要比在Java.C#等语言 ...
- Struts2 语法--result type
result type: dispatcher,redirect:只能跳转到jsp,html之类的页面,dispatcher属于服务器跳转, redirect属于客户端跳转 chain: 等同于for ...
- 转 Android开发者指南-Manifest.xml-<supports-screens
<supports-screens> 版本:Android 3.2 语法: <supports-screensandroid:resizeable=["true" ...
- C# 经典入门15章 -ListView 【未附代码】