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
Case 1: 1
Case 2: -1
Case 3: 0
Case 4: 2
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int INF=0x3f3f3f3f;
int dir_x[4]={1,-1,0,0};
int dir_y[4]={0,0,1,-1};
struct node
{
int x,y,t;
};
char map[15][15];
int vis[15][15],n,m,tot,ans,run;
bool flag=false;
queue<node> q[2];
node NA[120];
bool inside(node a)
{
if(a.x>=0&&a.x<n&&a.y>=0&&a.y<m) return true;
return false;
}
void bfs(int k,int sum)
{
if(sum==tot) flag=true;
node u,v;int sin=0;
int f=0;
while(!q[k].empty())
{
v=q[k].front();q[k].pop();
for(int i=0;i<4;i++)
{
u=v;u.x+=dir_x[i];u.y+=dir_y[i]; u.t+=1;
if(inside(u)&&map[u.x][u.y]=='#'&&vis[u.x][u.y]==0)
{
vis[u.x][u.y]=1; sin++;
run=max(run,u.t);
q[!k].push(u);
}
}
}
if(sum+sin==tot) flag=true;
if(!q[!k].empty())
bfs(!k,sum+sin);
}
int main()
{
int t,cas=1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
memset(map,0,sizeof(map));
memset(NA,0,sizeof(NA));
memset(vis,0,sizeof(vis));
ans=INF;tot=0;run=0;
while(!q[0].empty()) q[0].pop();
while(!q[1].empty()) q[1].pop();
for(int i=0;i<n;i++) scanf("%s",map[i]);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(map[i][j]=='#')
{
NA[tot++]=(node){i,j,0};
}
}
}
if(tot<=2) ans=0;
else
{
for(int i=0;i<tot;i++)
{
for(int j=i+1;j<tot;j++)
{
q[0].push(NA[i]); q[1].push(NA[j]);
memset(vis,0,sizeof(vis));
run=0;flag=false;
vis[NA[i].x][NA[i].y]=vis[NA[j].x][NA[j].y]=1;
bfs(0,2);
if(flag&&ans>run) ans=run;
}
}
}
if(ans==INF) ans=-1;
printf("Case %d: %d\n",cas++,ans);
}
return 0;
}
相关推荐
对于想要掌握这门技能的人来说,参加在线编程评测,如fzu online judge,是一个行之有效的途径。fzu online judge是一个面向编程爱好者和学习者的平台,提供了丰富的算法题目,供用户在线解答。这些题目虽然难度不一...
求最大乘积 的源代码 次题是fzu 4月月赛题 是一道数学题啊
不要下载此版的,请下载最新的http://download.csdn.net/source/1664620 离线版的福大acm在线评测OJ系统题目 更新到2009年8月 (注:chm电子书格式化)
从给定文件的内容中,我们可以提取以下计算机视觉和机器学习的知识点: 1. **MNIST手写数字数据库**: 这是一个非常著名的用于计算机视觉和机器学习研究的手写数字图像集合。它包含了成千上万的灰度图,每个图像是28...
计算机视觉是研究如何让计算机理解和解释视觉信息,使之能够复制人类对视觉信息的处理、分析和理解能力,并做出相应的行为决策。它涉及图像采集、处理、分析和理解等多个方面,广泛应用于工业自动化、监控系统、医疗...
fzu大数据基础实验4
在计算机视觉领域中,灰度共生矩阵(GLCM)是一种用于纹理特征分析的技术。它通过计算图像中像素值的相对位置和分布来揭示图像的纹理特性,这些特性在图像分割、特征提取和图像分析等领域中非常有用。...
FZU ACM 2025寒假集训 1.21-1.23
【标题】"2011 ACM 多校联合 2011 MU11 13 FZU" 指的是一项编程竞赛,ACM(国际计算机学会)每年都会举办多场这样的竞赛,旨在提升学生的算法设计和编程能力。ACM竞赛通常包括一系列的编程题目,参赛队伍需要在限定...
这是关于我们飞跃手册项目的相关文档,包括成员分组信息表格,各组成员任务概要文档,项目日记等文档。 (This is a collection of documents relating to our Leapfrog Handbook project, including member ...
FZU软件工程操作系统课程复习资料-整理 本资源摘要信息是关于FZU软件工程操作系统课程复习资料的整理,涵盖操作系统的基本概念、进程和线程、存储管理和文件系统等方面的知识点。 一、操作系统的定义和主要功能 ...
"FZU软件工程web课程复习资料-整理" 本资源是FZU软件工程web课程的复习资料,涵盖了web开发的基础知识和技术。下面是对该资源中所涉及的知识点的详细解释: 第一讲 web 开发概述 1. 因特网与万维网 因特网是一种...
根据给定的文件标题“ACM数学_FZU绝密资料”及描述“ACM数学_FZU...............绝密..........”,我们可以看出这份资料主要聚焦于数学在ACM竞赛中的应用,尤其是在解决算法问题时数学理论的重要性。下面我们将从...
【C# miniword 完整版】是一款基于C#编程语言开发的小型文字处理软件,类似于微软的Word,主要用于FZU(福州大学)的教学与作业。该项目旨在让学生熟悉C#编程环境,掌握Windows Forms应用程序的开发技术,以及对文本...
FZU ACM 上的半数集问题 的源代码
FZU2021计算机视觉慕课是一门面向学生和研究人员的基础课程,其中包含了丰富的实践案例和练习题。从提供的文件内容中,我们可以提取一些计算机视觉中的基础知识点,包括图像的读取、显示、转换、保存、以及图像处理...
1812 coin 解题代码 多重背包的应用
《fzu—java张苏老师课件》是一个针对Java初学者的课程资料集合,由一系列PPT文件组成,包括了从基础到进阶的多个章节。这个资源旨在帮助初学者系统地学习Java编程语言,逐步掌握其核心概念和技术。下面我们将详细...
标题“2021FZU计算机视觉答案(五)”表明本文档是一份关于计算机视觉题目的解答集,特别是涉及到与图像处理和分析相关的内容。从给出的部分内容来看,文档包含实现特定图像处理任务的Python代码示例,具体涉及到...
仅作参考,抄被发现后果自负哈。