Problem 2150 Fire Game

Accept: 693    Submit: 2657 Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem Description

Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. Firstly they choose two grids which are consisting of grass and set fire. As we all know, the fire can spread among the grass. If the grid (x, y) is firing at time t, the grid which is adjacent to this grid will fire at time t+1 which refers to the grid (x+1, y), (x-1, y), (x, y+1), (x, y-1). This process ends when no new grid get fire. If then all the grid which are consisting of grass is get fired, Fat brother and Maze will stand in the middle of the grid and playing a MORE special (hentai) game. (Maybe it’s the OOXX game which decrypted in the last problem, who knows.)

You can assume that the grass in the board would never burn out and the empty grid would never get fire.

Note that the two grids they choose can be the same.

 Input

The first line of the date is an integer T, which is the number of the text cases.

Then T cases follow, each case contains two integers N and M indicate the size of the board. Then goes N line, each line with M character shows the board. “#” Indicates the grass. You can assume that there is at least one grid which is consisting of grass in the board.

1 <= T <=100, 1 <= n <=10, 1 <= m <=10

 Output

For each case, output the case number first, if they can play the MORE special (hentai) game (fire all the grass), output the minimal time they need to wait after they set fire, otherwise just output -1. See the sample input and output for more details.

 Sample Input

4 3 3 .#. ### .#. 3 3 .#. #.# .#. 3 3 ... #.# ... 3 3 ### ..# #.#

 Sample Output

题目描述:

给定一张图。图中"#"代表草,'.'代表地,可能有几个草地,给你两把火,问烧完整个图中的草的最短时间。

由于n<=10,所以设立一个结构体,里面的属性有位置,和被烧的时间,然后枚举两把火的位置,开始烧,

用cnt计数烧过的草,然后将枚举的两个位置放进队列,如果两个位置重合的话。cnt=1,否则cnt=2;

其实我们可以认为i这两把火是有一把火烧起来的,那么之后就跟裸的宽搜是一样的,(其实跟锁屏样式的两种判断转换成一种差不多)time,每层+1.
#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <cstring>
#define inf 0x3f3f3f3f
using namespace std; char MAP[][];
int visit[][];
int dir[][]={-,,,,,,,-};
int n,m,sum,cnt;
struct node
{
int x, y,Time;
}; int main()
{
int t;
scanf("%d",&t);
int _case=;
while(t--)
{
queue< node > Q;
sum=;
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
{
getchar();
for(int j=;j<m;j++)
{ scanf("%c",&MAP[i][j]);
if(MAP[i][j]=='#')
{
++sum;
}
}
}
int answer=inf;
int cntll=;
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
if(MAP[i][j] == '#')
{
for(int xx=i;xx<n;xx++)
for(int yy=;yy<m;yy++)
{
if(xx==i && yy<j)
continue;
memset(visit,,sizeof(visit));
if(MAP[xx][yy]=='#')
{
cnt=;
if(i==xx && j==yy)
{
cnt=;
}
visit[i][j]=; visit[xx][yy]=;
node NODE1,NODE2,NODE;
NODE1.x=i; NODE1.y=j;NODE1.Time=;
NODE2.x=xx; NODE2.y=yy;NODE2.Time=;
Q.push(NODE1);
Q.push(NODE2);
while(!Q.empty())
{ NODE=Q.front();
Q.pop();
node point;
for(int k=;k<;k++)
{
point.x=NODE.x+dir[k][];
point.y=NODE.y+dir[k][];
if(point.x>= && point.x<n && point.y>= && point.y<m)
if(visit[point.x][point.y]== && MAP[point.x][point.y]=='#')
{
visit[point.x][point.y]=;
point.Time=NODE.Time+;
Q.push(point);
cnt=cnt+;
}
}
} if(cnt==sum)
{
answer=min(answer,NODE.Time);
}
}
}
}
}
if(answer!=inf)
printf("Case %d: %d\n",++_case,answer);
else
printf("Case %d: -1\n",++_case); } return ;
}

fzu 2150(bfs)的更多相关文章

  1. FZU - 2150 bfs [kuangbin带你飞]专题一

    题意:两个人玩很变态的游戏,将一个草坪的某两个点点燃,点燃的草坪可以向上下左右四个方向扩散,问能否将整块草坪上面的草都点燃.如果能,输出最短时间(^_^他们就能玩更变态的游戏了),如果不能,输出-1. ...

  2. ACM: FZU 2150 Fire Game - DFS+BFS+枝剪 或者 纯BFS+枝剪

    FZU 2150 Fire Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  3. FZU 2150 Fire Game (暴力BFS)

    [题目链接]click here~~ [题目大意]: 两个熊孩子要把一个正方形上的草都给烧掉,他俩同一时候放火烧.烧第一块的时候是不花时间的.每一块着火的都能够在下一秒烧向上下左右四块#代表草地,.代 ...

  4. FZU 2150 Fire Game

    Fire Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

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

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

  6. (广搜)Fire Game -- FZU -- 2150

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82828#problem/I Fire Game Time Limit:1000MS    ...

  7. fzu 2150 Fire Game 【身手BFS】

    称号:fzupid=2150"> 2150 Fire Game :给出一个m*n的图,'#'表示草坪,' . '表示空地,然后能够选择在随意的两个草坪格子点火.火每 1 s会向周围四个 ...

  8. (FZU 2150) Fire Game (bfs)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150 Problem Description Fat brother and Maze are playing ...

  9. FZU 2150 fire game (bfs)

    Problem 2150 Fire Game Accept: 2133    Submit: 7494Time Limit: 1000 mSec    Memory Limit : 32768 KB ...

随机推荐

  1. bzoj3041 水叮当的舞步 IDA*

    水叮当的舞步 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 230  Solved: 107[Submit][Status][Discuss] Des ...

  2. mapStruct笔记

    背景 mapStruct 是一个方便对象转换的工具,类似的工具还有 Dozer, BeanUtils. 实现 mapStruct的核心是在编译期生成基于转换规则的 Impl 文件,运行时直接调用 Im ...

  3. Codevs 队列练习 合并版

    3185 队列练习 1  时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 给定一个队列(初始为空),只有两种操作入队和出队,现给出这 ...

  4. tomcat并发数

    Tomcat的最大并发数是可以配置的,实际运用中,最大并发数与硬件性能和CPU数量都有很大关系的.更好的硬件,更多的处理器都会使Tomcat支持更多的并发. Tomcat默认的HTTP实现是采用阻塞式 ...

  5. POJ 2749 2SAT判定+二分

    题意:图上n个点,使每个点都与俩个中转点的其中一个相连(二选一,典型2-sat),并使任意两点最大 距离最小(最大最小,2分答案),有些点相互hata,不能选同一个中转点,有些点相互LOVE,必需选相 ...

  6. Python高级进阶(二)Python框架之Django写图书管理系统(LMS)

    正式写项目准备前的工作 Django是一个Web框架,我们使用它就是因为它能够把前后端解耦合而且能够与数据库建立ORM,这样,一个Python开发工程师只需要干自己开发的事情就可以了,而在使用之前就我 ...

  7. 学习日常笔记<day16>mysql加强

    1.数据约束 1.1什么是数据约束 对用户操作表的数据进行约束 1.2 默认值 作用:当永辉对使用默认值的字段不插入值的时候,就使用默认值 注意: 1)对默认值字段插入null是可以的 2)对默认值字 ...

  8. Java开发笔记(一百)线程同步synchronized

    多个线程一起办事固然能够加快处理速度,但是也带来一个问题:两个线程同时争抢某个资源时该怎么办?看来资源共享的另一面便是资源冲突,正所谓鱼与熊掌不可兼得,系统岂能让多线程这项技术专占好处?果然是有利必有 ...

  9. grep使用正则表达式搜索IP地址

    递归搜索当前目录及其子目录.子目录的子目录……所包含文件是否包含IP地址 grep -r "[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit: ...

  10. mysql innodb插入意向锁

    innodb中有插入意向锁.专门针对insert,假设插入前,该间隙已经由gap锁,那么Insert会申请插入意向锁. 那么这个插入意向锁的作用是什么? 1.为了唤起等待.由于该间隙已经有锁,插入时必 ...