这个题真要好好说一下了,比赛的时候怎么过都过不了,压点总是出错(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. @Value 注解获取properties值

    转自:使用Spring 3的@value简化配置文件的读取 Spring 3支持@value注解的方式获取properties文件中的配置值,大简化了读取配置文件的代码. 1.在application ...

  2. int与byte的区别

    Java中涉及byte.short和char类型的运算操作首先会把这些值转换为int类型,然后对int类型值进行运算,最后得到int类型的结果.因此,如果把两个byte类型值相加,最后会得到一个int ...

  3. 矩阵快速幂——将运算推广到矩阵上HDU 1575

    /* 本题的思路比较简单,就是将递推公式写出来,然后表达成为一个矩阵的形式 最后通过计算就可以得到一个符合题目要求的矩阵, 然后就是将矩阵上面所有的对角线元素相加 得到的结果即为所求的目标 */ #i ...

  4. spark第二篇--基本原理

    ==是什么 == 目标Scope(解决什么问题) 在大规模的特定数据集上的迭代运算或重复查询检索 官方定义 aMapReduce-like cluster computing framework de ...

  5. hdu 1212 Big Number(大数取模)

    Problem Description As we know, Big Number is always troublesome. But it's really important in our A ...

  6. PHP:preg_replace

    关于preg_match: http://www.cnblogs.com/helww/p/3466720.html 关于preg_match_all:暂时没有完整的 preg_replace_call ...

  7. Unity3d 物体沿着正七边形轨迹移动

    不对之处,敬请谅解. 1.圆内接正七边形半径 public static float r = 10; 2.存储七个顶点的值 Vector3[] ar = new Vector3[7]; 3.圆心角 s ...

  8. Q_DISABLE_COPY

    QObject 中没有提供一个拷贝构造函数和赋值操作符给外界使用,其实拷贝构造和赋值的操作都是已经声明了的,但是它们被使用了Q_DISABLE_COPY() 宏放在了private区域.因此所有继承自 ...

  9. C#中的Virtual、Override和new关键词理解

    来源:http://blog.csdn.net/jackiezhw/article/details/2673992 在 C# 中,派生类可以包含与基类方法同名的方法. 基类方法必须定义为 virtua ...

  10. docker installation on ubuntu

    Ubuntu Docker is supported on these Ubuntu operating systems: Ubuntu Xenial 16.04 (LTS) Ubuntu Trust ...