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

由于数据比较小,所以可以暴力的去解,不过先判断一下联通块可以解决一小部分问题的.

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
#define inf 99999999
char maps[][];
int n,m,vis[][],go[][] = {,,-,,,,,-};
struct Pos
{
int x,y;
};
bool ok(Pos a)
{
return (a.x>= && a.x<n && a.y>= && a.y<m && maps[a.x][a.y] == '#');
}
queue<Pos>que;
int bfs(Pos f1,Pos f2)
{
while(!que.empty()) que.pop();
que.push(f1);
que.push(f2);
for(int i = ; i < n; i++)
{
for(int j = ; j < m; j++)
{
vis[i][j] = inf;
}
}
vis[f1.x][f1.y] = vis[f2.x][f2.y] = ;
while(!que.empty())
{
Pos now = que.front();
que.pop();
Pos nex;
for(int i = ; i < ; i++)
{
nex.x = now.x + go[i][];
nex.y = now.y + go[i][];
if(ok(nex) && vis[nex.x][nex.y] > vis[now.x][now.y] + )
{
vis[nex.x][nex.y] = vis[now.x][now.y] + ;
que.push(nex);
}
}
}
int maxv = -;
for(int i = ; i < n; i++)
{
for(int j = ; j < m; j++)
{
if(maps[i][j] == '#')
maxv = max(maxv,vis[i][j]);
}
}
return maxv;
}
int main()
{
int t,ca = ;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i = ; i < n; i++)
scanf("%s",maps[i]);
int cnt = ;
for(int i = ; i < n; i++)
{
for(int j = ; j < m; j++)
{
if(maps[i][j] == '#')
cnt++;
}
}
printf("Case %d: ",++ca);
if(cnt <= )
{
printf("%d\n",);
continue;
}
int ans = inf;
for(int i = ; i < n; i++)
{
for(int j = ; j < m; j++)
{
if(maps[i][j] == '#')
{
for(int k = ; k < n; k++)
{
for(int q = ; q < m; q++)
{
if(j == q && i == k) continue;
if(maps[k][q] == '#')
{
Pos f1,f2;
f1.x = i,f1.y = j;
f2.x = k,f2.y = q;
ans = min(ans,bfs(f1,f2));
}
}
}
}
}
}
if(ans == inf)
{
cout<<-<<endl;
}
else cout<<ans<<endl;
}
return ;
}

FZU Problem 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 2150 Fire Game(BFS)

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

  3. FZU Problem 2150 Fire Game

    Problem 2150 Fire Game Accept: 145    Submit: 542 Time Limit: 1000 mSec    Memory Limit : 32768 KB P ...

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

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

  5. foj 2150 Fire Game(bfs暴力)

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

  6. Problem 2285 迷宫寻宝 (BFS)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2285 Problem 2285 迷宫寻宝 Accept: 323    Submit: 1247Time Li ...

  7. Fire Game--FZU2150(bfs)

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

  8. Fire Game FZU - 2150 (bfs)

    Problem 2150 Fire Game Accept: 3772    Submit: 12868Time Limit: 1000 mSec    Memory Limit : 32768 KB ...

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

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

随机推荐

  1. 测试TCP/IP配置

    安装网络硬件和网络协议之后,我们一般要进行TCP/IP协议的测试工作,那么怎样测试才算是比较全面的测试呢?我们认为,全面的测试应包括局域网和互联网两个方面,因此应从局域网和互联网两个方面测试,以下是我 ...

  2. 【Machine Learning in Action --2】K-近邻算法构造手写识别系统

    为了简单起见,这里构造的系统只能识别数字0到9,需要识别的数字已经使用图形处理软件,处理成具有相同的色彩和大小:宽高是32像素的黑白图像.尽管采用文本格式存储图像不能有效地利用内存空间,但是为了方便理 ...

  3. ios控件 UILabel

    UILabel 的作用是显示文本 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 40)]; lab ...

  4. Linux 添加ssh 公钥访问

    登陆被管理的服务器,进入需要远程登陆的用户目录,把公钥放到用户目录的 .ssh 这个目录下(如果目录不存在,需要创建~/.ssh目录,并把目录权限设置为700),把公钥改名为authorized_ke ...

  5. Django Template模板

    Django Template 你可能已经注意到我们在例子视图中返回文本的方式有点特别. 也就是说,HTML被直接硬编码在 Python 代码之中. 下面我们来调用html views def ind ...

  6. window窗体程序意外崩溃,EventType clr20r3错误的解决方法

    EventType clr20r3, P1 ggreadcard.exe, P2 1.0.0.0, P3 51d3d283, P4 zljy.common, P5 1.0.0.0, P6 4fc6c2 ...

  7. git repository 的使用

    1. You should only be pushing to a bare repository. A bare repository is a repository that has no ch ...

  8. 【多重背包模板】poj 1014

    #include <iostream> #include <stdio.h> #include <cstring> #define INF 100000000 us ...

  9. LightOJ 1058 平行四边形的判断定理

    题目大意:给你n个点,求这n个点最多能组成多少个平行四边形. 题目思路:这道题卡时间,而且卡内存.你要尽可能的想办法优化. 平行四边形的判定定理: 两组对边分别平行的四边形是平行四边形(定义判定法): ...

  10. POJ 1182 食物链 经典并查集+关系向量简单介绍

    题目: 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有 ...