点我看题目

题意 :就是有两个熊孩子要把一个正方形上的草都给烧掉,他俩同时放火烧,烧第一块的时候是不花时间的,每一块着火的都可以在下一秒烧向上下左右四块#代表草地,.代表着不能烧的。问你最少花多少时间可以烧掉,如果烧不掉就输出-1

思路 :这个题因为可以同时向上下左右同时烧过去,所以一看我就知道是BFS,但是知道没用啊,我做不出来啊,我一开始没想过来,以为两个人烧很麻烦,其实就是向普通的那样做,不过来个6重for循环就行了,其实就是看看有几个块,如果块的个数超过两个就为-1,两个的话,分别开始烧,哪个块里有最长路,就是烧的那个时间,不过你要枚举有最长路的那个的块的每一个可烧的点,从而找出时间最短的,1个块的时候也是一样的,求出最长路,枚举每个点的时候求最短时间。这个题看着芳姐的代码才理解。。。。。

不过做的时候没有去判断几个块,因为你BFS找的时候就处理了。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue> const int INF = ;
int n,m ;
char ch[][] ;
int cnt ;
int vis[][] ;
int dir[][] = {{,},{,-},{,},{-,}} ;
struct node
{
int x,y ;
int step ;
}q1,q2 ,mapp[]; using namespace std ; int BFS(int x1,int y1,int x2,int y2)
{
int maxx = ;
queue<node>que ;
q1.x = x1,q1.y = y1,q1.step = ;
q2.x = x2,q2.y = y2,q2.step = ;
que.push(q1) ;
que.push(q2) ;
//memset(vis,0,sizeof(vis)) ;
while(!que.empty())
{
struct node st1, st = que.front() ;
que.pop() ;
for(int i = ; i < ; i++)
{
int xx = st.x+dir[i][] ;
int yy = st.y+dir[i][] ;
if(!vis[xx][yy] && ch[xx][yy] == '#' && (xx >= &&xx < n&&yy>=&&yy<m))
{
vis[xx][yy] = ;
st1.x = xx ,st1.y = yy ,st1.step = st.step+ ;
que.push(st1) ;
}
}
maxx = max(maxx,st.step) ;
}
return maxx ;
} int main()
{
int T ;
scanf("%d",&T) ;
for(int i = ; i <= T ;i++)
{
scanf("%d %d",&n,&m) ;
cnt = ;
for(int j = ; j < n ; j++)
{
scanf("%s",ch[j]) ;
for(int k = ; k < m ; k++)
if(ch[j][k] == '#')
{
cnt++ ;
mapp[cnt].x = j ;
mapp[cnt].y = k ;
}
}
printf("Case %d: ",i) ;
if(cnt <= )
{
printf("0\n") ;
continue ;
}
int minn = INF ;
for(int j = ; j < cnt ; j++)
{
for(int k = j ; k < cnt ; k++)
{
memset(vis,,sizeof(vis)) ;
vis[mapp[j].x][mapp[j].y] = ;
vis[mapp[k].x][mapp[k].y] = ;
bool flag = false ;
int minnn = BFS(mapp[j].x,mapp[j].y,mapp[k].x,mapp[k].y) ;
for(int h = ; h < n ; h++)
{
for(int l = ; l < m ; l++)
{
if(ch[h][l] != '#') continue ;
if(!vis[h][l]){flag = true ; break ;}
}
if(flag) break ;
}
if(!flag) minn = min(minn,minnn) ;
}
} if(minn == INF) printf("-1\n") ;
else printf("%d\n",minn) ;
}
return ;
}

这个是ZP的,写的差不多,不过用时100多ms,所以贴出来瞻仰一下

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<queue>
using namespace std;
#define LL __int64
int map[][];
int num[][][][];
int vis[][];
int n,m;
struct list
{
int x;
int y;
int t;
}p,q;
queue<struct list >que;
int xx[]={,-,,};
int yy[]={,,,-};
void dos(int i,int j,int a,int b,int t)
{
while(!que.empty())que.pop();
num[i][j][a][b]=;
p.x=a;
p.y=b;
p.t=;
que.push(p);
vis[a][b]=;
while(!que.empty())
{
p=que.front();
que.pop();
num[i][j][p.x][p.y]=p.t;
for(int i=;i<;i++)
{
q.x=p.x+xx[i];
q.y=p.y+yy[i];
q.t=p.t+;
if(q.x<||q.x>n||q.y<||q.y>m)continue;
if(vis[q.x][q.y])continue;
if(map[q.x][q.y]==)continue;
que.push(q);
vis[q.x][q.y]=;
}
}
}
char str[];
int main()
{
int T;
int i,j,a,b,t,k;
scanf("%d",&T);
for(int _=;_<=T;_++)
{
memset(map,,sizeof(map));
scanf("%d%d",&n,&m);
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
for(k=;k<=n;k++)
{
for(t=;t<=m;t++)
{
num[i][j][k][t]=;
}
}
}
}
for(i=;i<=n;i++)
{
scanf("%s",str);
for(j=;j<m;j++)
{
if(str[j]=='#')
{
map[i][j+]=;
}
}
}
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
if(map[i][j]==)continue;
memset(vis,,sizeof(vis));
dos(i,j,i,j,);
}
}
//cout<<num[2][1][2][1]<<endl;
//cout<<num[2][3][2][3]<<endl;
int minn=;
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
if(map[i][j]==)continue;
for(a=;a<=n;a++)
{
for(b=;b<=m;b++)
{
if(map[a][b]==)continue;
int tt=;
for(t=;t<=n;t++)
{
for(k=;k<=m;k++)
{
if(map[t][k]==)continue;
tt=max(tt,min(num[i][j][t][k],num[a][b][t][k]));
}
}
minn=min(minn,tt);
}
}
}
}
printf("Case %d: ",_);
if(minn==)
{
cout<<-<<endl;
}
else cout<<minn<<endl;
}
return ;
}

我还要贴一下会神的,因为限时1000ms,然后会神的代码就正好跑了1000ms。。。。。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 110
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
char s[][];
struct node
{
int x,y;
int num;
}p[N];
int vis[][],dis[][] = {,,,-,-,,,},n,m;
int judge(int x,int y)
{
if(x<||x>n||y<||y>m) return ;
if(vis[x][y]) return ;
if(s[x][y]!='#') return ;
return ;
}
int bfs(int x1,int y1,int x2,int y2)
{
queue<node>q;
node t1,t2;
t1.num = ;t1.x = x1,t1.y = y1;
t2.num = ;t2.x = x2,t2.y = y2;
q.push(t1);
q.push(t2);
int mm=;
while(!q.empty())
{
t1 = q.front();q.pop(); for(int i = ; i < ;i++)
{
int tx = t1.x+dis[i][];
int ty = t1.y+dis[i][];
if(judge(tx,ty))
{
vis[tx][ty] = ;
t2.x = tx,t2.y = ty,t2.num = t1.num+;
q.push(t2);
}
}mm = max(mm,t1.num);
}
return mm;
}
int main()
{
int t,i,j,kk=,g;
cin>>t;
while(t--)
{
g = ;
cin>>n>>m;
for(i = ; i <= n; i++)
for(j = ; j <= m ;j++)
{
cin>>s[i][j]; if(s[i][j]=='#')
{
g++;
p[g].x = i;
p[g].y = j;
}
}
int minz = INF;
for(i = ; i <= g ; i++)
for(j = i ;j <=g ;j++)
{
memset(vis,,sizeof(vis));
vis[p[i].x][p[i].y] = ;
vis[p[j].x][p[j].y] = ;
int t1 = bfs(p[i].x,p[i].y,p[j].x,p[j].y);
int flag = ;
for(int o = ; o <= n;o++)
{
for(int e = ; e <= m; e++)
if(s[o][e]=='#')
{
if(!vis[o][e])
{
flag = ;
break;
}
}
if(flag) break;
}
if(!flag)
{
minz = min(minz,t1);
}
}
printf("Case %d: ",++kk);
if(minz!=INF)
cout<<minz<<endl;
else
puts("-1");
}
return ;
}

FZU 2150 Fire Game(BFS)的更多相关文章

  1. FZU 2150 fire game (bfs)

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

  2. FZU Problem 2150 Fire Game(bfs)

    这个题真要好好说一下了,比赛的时候怎么过都过不了,压点总是出错(vis应该初始化为inf,但是我初始化成了-1....),wa了n次,后来想到完全可以避免这个问题,只要入队列的时候判断一下就行了. 由 ...

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

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

  4. FZU 2150 Fire Game (暴力BFS)

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

  5. 【FZU - 2150】Fire Game(bfs)

    --> Fire Game 直接写中文了 Descriptions: 两个熊孩子在n*m的平地上放火玩,#表示草,两个熊孩子分别选一个#格子点火,火可以向上向下向左向右在有草的格子蔓延,点火的地 ...

  6. FZU 2150 Fire Game (高姿势bfs--两个起点)(路径不重叠:一个队列同时跑)

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

  7. FZU 2150 Fire Game (高姿势bfs--两个起点)

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

  8. foj 2150 Fire Game(bfs暴力)

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

  9. Fire Game--FZU2150(bfs)

    http://acm.fzu.edu.cn/problem.php?pid=2150 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=659 ...

随机推荐

  1. Linux服务器命令行模式安装Matlab2014a

    Linux服务器命令行模式安装Matlab2014a,有需要的朋友可以参考下. 0.下载安装包 下载Matlab2014a for Linux安装包的ISO镜像文件(感谢万能的度娘)以及破解包(下载地 ...

  2. 【itclx面向对象二】窥探itcl面向编程源码

    从上一篇博客看出,itcl的语法其实不难,但是有个缺点,编程习惯与当前类似C++常见的面向编程还是有些区别,并且在大型项目实施中这种方式很费劲. 于是有了itclx. 例如: 1.成员变量.成员方法调 ...

  3. Android端手机测试体系

    1.冒烟测试 跟web端的测试流程一样,你拿到一个你们开发做出来的apk首先得去冒烟,也就是保证他的稳定性,指定时间内不会崩溃.这款原生sdk自带的monkey可以当做我们的测试工具.就跟我之前博客所 ...

  4. form表单按enter键自动提交的问题

    废话不多说.直接上代码. 1:form表单按enter键自动提交的情况 <!doctype html> <html lang="en"> <head& ...

  5. xml的语法与创建

    xml语法很简单,但很严格,如果出现错误则不能正常解析,而HTML如果出现局部的错误,照样解析 xml第一行必须写xml头<?xml version='1.0' encoding='utf8'? ...

  6. 比较ASP生成静态HTML文件的几种方法

    将动态页面转换生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录(特别是对接受动态参数的页面).前台访问时,脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. 当然,凡事 ...

  7. length prototype 函数function的属性,以及构造函数

    前言:学到一些JavaScript高级的知识,在这里记下,方便以后的查找 1.length代表函数定义的形参的个数,挺简单的     例如:function Pen(price,cname) {  . ...

  8. hibernate--could not initialize proxy - no Session--懒加载问题

    今天在学习hibernate时,出现了以下错误: 错误分析: 如果我们取单个对象可以用get方法没有问题:但是如果我们取的的对象还有关联对象时用get就有问题,因为它不会把关联的对象取出来 参考博客: ...

  9. 常用经典SQL语句大全(基础)

    一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sqlserver --- 创 ...

  10. [MAXscript Tool]FFX_PalyBack v1.1 ShowReel

    自己的写的一个简单的脚本方便实现大面积的烟,火,爆炸,云的效果.能实现静态动态的切换,还有快速的偏移FumeFX的缓存,支持随机缓存 具体看这个插件的ShowReel,结算的三套基础的火焰然后用此脚本 ...