(简单) 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) ...
随机推荐
- 使用oracle数据库开发,异常总结
最近两天使用的oracle数据库开发项目时遇到了2个异常,第一个是执行sql语句时报异常:“ORA-00911: 无效字符”,如下图: sql语句如下: 断点调试,把sql语句拷贝到pl/sql里执行 ...
- 转 Oracle12c/11个 Client安装出现"[INS-30131]"错误“请确保当前用户具有访问临时位置所需的权限”解决办法之完整版
错误分析:安装时exe会自动解压到C:\Users\Administrator\AppData\Local\Temp再进行安装,当文件夹权限不足时就会拒绝安装程序的访问: 第一步: 在win+R输入 ...
- Hibernate 系列教程9-自关联
自关联:本质还是原来双向一对多,原来要配置两个类,现在全部都配置在一个类里面 Employee public class Employee { private Long id; private Str ...
- 设计模式-中介者模式(Mediator)
/***中介者模式在消息队列中的应用*/package test.mediator; public abstract class Message { private Messages messages ...
- CDockablePane 关闭的问题
显示或者隐藏 当点击CDockablePane上的关闭按钮时,并不能将其关闭,知识将其隐藏了起来,如果需要重新显示或隐藏,则相关命令的响应函数如下: if(m_Panes.GetSafeHwnd()) ...
- java 实例变量和类变量的区别
Example4_10.java public class Example4_10 { public static void main(String args[]) { Lader.下底=100; / ...
- AngularJS 基础用法
判断语句: <li ng-repeat=”person in persons”> <span ng-switch on=”person.sex”> <span ng-sw ...
- 修改6S Fortran77 代码,建立查找表
逐像元大气校正,常预先计算查找表(LUT,LookUp Tabel),6S大气辐射传输模式也可以用来计算LUT.但6S源程序输出信息多,且浮点数输出精度低,不利于提取关键信息生成LUT,本文描述了 ...
- pur-ftpd在ubuntu上的安装
1.安装 apt-get install pure-ftpd 2.建立ftp目录 /var/ftp/public 3.建立ftp用户组 groupadd ftpgroup 4.建立ftp非系统用户 u ...
- ubuntu11.10server 安装redis-2.6.7
1.下载安装: 1 2 3 4 5 6 cd /tmp wget http://redis.googlecode.com/files/redis-2.6.7.tar.gz tar -zxf redis ...