FZU 2150 Fire Game(BFS)
题意 :就是有两个熊孩子要把一个正方形上的草都给烧掉,他俩同时放火烧,烧第一块的时候是不花时间的,每一块着火的都可以在下一秒烧向上下左右四块#代表草地,.代表着不能烧的。问你最少花多少时间可以烧掉,如果烧不掉就输出-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)的更多相关文章
- FZU 2150 fire game (bfs)
Problem 2150 Fire Game Accept: 2133 Submit: 7494Time Limit: 1000 mSec Memory Limit : 32768 KB ...
- FZU Problem 2150 Fire Game(bfs)
这个题真要好好说一下了,比赛的时候怎么过都过不了,压点总是出错(vis应该初始化为inf,但是我初始化成了-1....),wa了n次,后来想到完全可以避免这个问题,只要入队列的时候判断一下就行了. 由 ...
- FZU 2150 Fire Game(点火游戏)
FZU 2150 Fire Game(点火游戏) Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description - 题目描述 ...
- FZU 2150 Fire Game (暴力BFS)
[题目链接]click here~~ [题目大意]: 两个熊孩子要把一个正方形上的草都给烧掉,他俩同一时候放火烧.烧第一块的时候是不花时间的.每一块着火的都能够在下一秒烧向上下左右四块#代表草地,.代 ...
- 【FZU - 2150】Fire Game(bfs)
--> Fire Game 直接写中文了 Descriptions: 两个熊孩子在n*m的平地上放火玩,#表示草,两个熊孩子分别选一个#格子点火,火可以向上向下向左向右在有草的格子蔓延,点火的地 ...
- 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 ...
- 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 ...
- foj 2150 Fire Game(bfs暴力)
Problem Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M ...
- Fire Game--FZU2150(bfs)
http://acm.fzu.edu.cn/problem.php?pid=2150 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=659 ...
随机推荐
- 记工作中的git遇到的问题
看了 git 回退到某版本后,再在此版本上更新,无法push 操作前,我备份了修改了目录,准备建一个分支进行操作 我在本地revert了一次,commit到了远程仓库.然后上个版本的修改给恢复了... ...
- c# 远程监控(4) 接收端 RTP包重组 分屏显示
我们在上一期使用RTP协议,并进行了配置,打包了视频数据,这一期我们就对发送的数据进行重组,并显示在接受端上.最后对其进行扩展,支持多客户端视频发送,并在接收端分屏显示.完成远程监控的模拟. 先来个效 ...
- Apache Mesos总体架构
http://developer.51cto.com/art/201401/426507.htm 1. 前言 同其他大部分分布式系统一样,Apache Mesos为了简化设计,也是采用了master/ ...
- 第五章 jQuery中的动画
通过jQuery中的动画方法,能轻松地为网页添加精彩的视觉效果,给用户一种全新体验. 1.show()方法和hide()方法 该方法的功能与css()方法设置display属性效果相同. 给show( ...
- Azure Redis Cache作为ASP.NET Session状态提供程序
从上一篇博客<使用Azure Redis Cache>我们已经可以创建并使用Redis Cache为我们服务了. 作为Web开发者,我们都知道Session状态默认是保存在内存中的,它的优 ...
- Linq DataTable Group By 分组显示人员明细
实现功能: 多个字段分组源码样例: 原始数据: 分组后的输出结果: 源代码: public static void PrintPersons() { //准备数据 DataTable dt ...
- vs2010开发android的准备工作
安装 Mono for Android for Visual Studio 2010 需要下面4个步骤: 安装 JDK 安装 Android SDK 配置模拟器 安装 Mono for Android ...
- Hql 中实用查询时候 引号的使用
出错代码://List vlist = this.getHibernateTemplate().find("from AndroidCustomer ct where ct.token = ...
- angular2 select change 事件
刚开始这是啥?(wrong!!! change事件会在 选择option行为 之前执行prodDirId,是取不到选择后正确的id值的,取得是选择行为前prodDirId的值(有试过setTi ...
- KMP的模版实现(以hdu1711为例)
贴代码,觉得带template的有一些大材小用……不过还是按自己风格写吧! /************************************************************* ...