这个题真要好好说一下了,比赛的时候怎么过都过不了,压点总是出错(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. Oracle小技术集锦

  2. CSS3秘笈:第二章

    1.一个样式由两个元素组成:浏览器对其设置格式的网页元素(选择器,selector)和实际的格式化指令(声明块,declaration block). 2.简单的样式也包含了以下几个元素: (1)Se ...

  3. ios 导航页面

    //  AppDelegate.m#import "AppDelegate.h"#import "ViewController.h" @interface Ap ...

  4. js获取后台json数据显示在jsp页面元素

    jsp id <font size=2 >Today:</font> <font id ="todaytotal" size=2 color=&quo ...

  5. 《Head.First设计模式》的学习笔记(9)--外观模式

    意图:为子系统中的一组接口提供一个一致的界面,Facade 模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 结构: 例子: 假设你有一套杀手级的家庭影院系统,内含DVD播放器.投影仪.自 ...

  6. Qt Quick里的图形效果:阴影(Drop Shadow)

    Qt Quick提供了两种阴影效果: DropShow,阴影.这个元素会根据源图像,产生一个彩色的.模糊的新图像,把这个新图像放在源图像后面,给人一种源图像从背景上凸出来的效果. InnerShado ...

  7. Mybatis 一对一,一对多,多对一,多对多的理解

    First (一对一) 首先我来说下一对一的理解,就是一个班主任只属于一个班级,一个班级也只能有一个班主任.好吧这就是对于一对一的理解 怎么来实现呢? 这里我介绍了两种方式: 一种是:使用嵌套结果映射 ...

  8. Day02——widow对象

    window - 计时器 1、setTimeout()可以用来在指定的时间之后单次调用函数. setTimeount(f,1000);//一秒后调用函数f clearTimeout();取消函数的执行 ...

  9. Spring Boot 系列教程6-全局异常处理

    @ControllerAdvice源码 package org.springframework.web.bind.annotation; import java.lang.annotation.Ann ...

  10. iOS8.0以后的相册

    在 iOS 8.0 后, 使用the Photos framework 代替 the Assets Library framework , The Photos framework 提供更特色和更好的 ...