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 ...
随机推荐
- C#根据WSDL文件生成WebService服务端代码
转自:http://www.cnblogs.com/liyi93/archive/2012/01/30/2332320.html 虽然现在已经进入了.NET FrameWork 4.0的时代,WebS ...
- 今天学习image在html中的应用
今天学习image在html中的应用 上次在学习超级链接的使用中有一小问题,是在添加网址中href="http://www.baidu.com" 中不能忘记http://,否则链接 ...
- 初步接触html心得
接触HTML大概有七天,做一下小总结,过过记忆. html大致可分为三部分:Dtd头.Head.Body三大部分. Dtd头:是用于浏览器编辑的,也就是俗话说的给电脑看的的东西. Head:内细分下大 ...
- C#学习笔记8:HTML和CSS基础学习笔记
<!-- 1.<P>...</P>段落标签 2.<br/>折行标签. 3.<img src="" height="*px& ...
- C# string.Format格式化时间或货币
1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) string.Format("{0:C}",0.2) 结果为:¥0.20 (英文操作系统结果:$0 ...
- 误解了Windows Server AppFabric
想为自己的流程引擎找一个宿主,选择了几套方案,想先从AppFabric开始,原因主要出于以下几点: 1. 自己用过Windows Service或Form作为一些定时任务等应用的宿主,但苦于学艺不精, ...
- jfinal取消默认跳转到view.jsp页面的方法
今天为了在一个列表中添加一个删除的方法,直接在方法里面谢了一个dao.del();方法,但是调用的时候却出现404错误. 然后就写了一句下面的代码 redirect("/api/listMe ...
- Codevs 1535 封锁阳光大学
1535 封锁阳光大学 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 曹是一只爱刷街的老曹,暑假期间,他每天都欢快地在阳光大 ...
- mysql学习笔记6——用phpmyadmin和在腾讯微云中创建数据库
安装phpmyadmin就不多说了,对于新手,推荐使用wamp(windows系统),傻瓜式安装,很好用.安装完后在浏览器栏输入localhost
- 在linux环境下配置node:node + npm + forever
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3574582.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...