L - Fire Game
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
一开始想用dfs,超过三堆就输出-1,两堆就输出较大一堆的最小值,但是发现一堆的时候没法算
比如这种的时候答案是1,dfs做不出来
.##.
.##.
所以用bfs,发现其实bfs也不难哈哈哈,因为只有10*10,所以可以暴力每两个点,O(n^2)复杂度,不会T,记录可以烧光的两个点的时间,输出最小的就好了
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
//#define pb push_back
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
//#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
typedef long double ld;
typedef double db;
const ll mod=1e9+100;
const db e=exp(1);
using namespace std;
const double pi=acos(-1.0);
int n,m,sum;
int dir[4][2]={0,-1,0,1,-1,0,1,0};
int ans=mod,visit[15][15];
struct node
{
int x,y;
int state;
node(int a,int b,int c)
{
x=a;y=b;state=c;
}
node(){
}
};
char map[15][15],a[15][15];
queue<node>v;
bool bfs(node a,node b)
{
if(map[a.x][a.y]=='#')
v.push(a);
if(map[b.x][b.y]=='#')
v.push(b);
if(v.empty()) return false;
node t,tt;
while(!v.empty())
{
t=v.front();
v.pop();
rep(i,0,4)
{
tt.x=t.x+dir[i][0];
tt.y=t.y+dir[i][1];
tt.state=t.state+1;
if(map[tt.x][tt.y]=='#'&&visit[tt.x][tt.y]==0)
{
visit[tt.x][tt.y]=1;
v.push(tt);
sum=max(sum,tt.state);
}
}
}
rep(i,1,n+1)
rep(j,1,m+1)
if(map[i][j]=='#'&&visit[i][j]==0)
return false;
return true;
}
int main()
{
int re;
cin>>re;
rep(m9,1,re+1)
{
ans=mod;
mm(map,'.');
cin>>n>>m;
rep(i,1,n+1)
{
sf("%s",&map[i][1]);
map[i][m+1]='.';
}
int count=0;
mm(a,'.');
rep(i,1,n+1)
rep(j,1,m+1)
{
if(map[i][j]=='#')
count++;
}
if(count<=2)//少于等于两堆一次就烧光了
{
pf("Case %d: 0\n",m9);
continue;
}
rep(i,1,n+1)
{
rep(j,1,m+1)
{
rep(k,1,n+1)
{
rep(l,1,m+1)
{
mm(visit,0);
sum=0;
visit[i][j]=1;visit[k][l]=1;
if(bfs(node(i,j,0),node(k,l,0)))
if(sum)//这个判断似乎多余了,因为bfs可以烧光的话,sum不会等于0,应该可以删了,懒得试,
{
ans=min(ans,sum);
}
}
}
}
}
if(ans==mod)//等于mod的时候说明没有办法烧光
pf("Case %d: -1\n",m9);
else
pf("Case %d: %d\n",m9,ans);
}
return 0;
}
L - Fire Game的更多相关文章
- Quartz.net配置文件实例及cron表达式详解
从XML文件创建作业 最新版本的quartz.net支持直接从xml文件创建作业,使用起来很方便.配置文件的格式可以参考下面的例子 <?xml version="1.0" e ...
- quartz集群分布式(并发)部署解决方案-Spring
项目中使用分布式并发部署定时任务,多台跨JVM,按照常理逻辑每个JVM的定时任务会各自运行,这样就会存在问题,多台分布式JVM机器的应用服务同时干活,一个是加重服务负担,另外一个是存在严重的逻辑问题, ...
- 注解式开发spring定时器
1:spring 配置文件中增加这句 <task:annotation-driven/> 2:确保扫描程序能够扫描后 下面第3步骤的java类 <context:co ...
- 任务调度框架-Quartz.Net
使用Quartz.Net依赖于以下3个组件:Common.Logging.dll.Common.Logging.Core.dll.Quartz.dll 简单封装 using Quartz; using ...
- Quartz集群
为什么选择Quartz: 1)资历够老,创立于1998年,比struts1还早,但是一直在更新(27 April 2012: Quartz 2.1.5 Released),文档齐全. 2)完全由Jav ...
- Quartz:Cron Expressions
原文地址:http://www.quartz-scheduler.net/documentation/quartz-2.x/tutorial/crontrigger.html 注意: 位也可能是7位, ...
- Quartz 2.2.x CronTrigger Tutorial
http://www.quartz-scheduler.org/documentation/quartz-2.2.x/tutorials/crontrigger.html Introduction c ...
- 项目中使用Quartz集群分享--转载
项目中使用Quartz集群分享--转载 在公司分享了Quartz,发布出来,希望大家讨论补充. CRM使用Quartz集群分享 一:CRM对定时任务的依赖与问题 二:什么是quartz,如何使用, ...
- Quartz Scheduler(2.2.1) - Usage of CronTriggers
Cron is a UNIX tool that has been around for a long time, so its scheduling capabilities are powerfu ...
随机推荐
- windows命令行下杀死进程的方法
xp和win7下有两个好东东tasklist和tskill.tasklist能列出所有的进程,和相应的信息.tskill能查杀进程,语法很简单:tskill程序名!或者是tskill 进程id 例如: ...
- 检测三种不同操作系统的Bash脚本
检测三种不同操作系统(GNU/Linux, Mac OS X, Windows NT)的Bash脚本. 设计: 1.使用“uname”命令获取系统信息,带上“-s”参数个打印内核名称. 2.使用“ex ...
- AI通过了艺术创作图灵测试,你根本分不出来作者是不是人
各位geek朋友们,今年不用再看画了:近年来最大的艺术成就已经发生了. 这项艺术成就的诞生地,不是北京.新加坡.柏林郊区颜料四溅的画室中,不是威尼斯双年展上.请记住它出现的地点:美国新泽西州新布朗斯维 ...
- N1, T1刷机记录
硬件配置 N1和T1使用的是晶晨Amlogic方案的芯片, 配置明细分别如下, 都是现在盒子的主流配置 N1CPU: Amlogic S905, ARM Cortex-A53 四核 up to 2.0 ...
- 关于PHP中的webshell
一.webshell简介 webshell就是以asp.php.jsp或者cgi等网页文件形式存在的一种命令执行环境,也可以将其称做为一种网页后门.黑客在入侵了一个网站后,通常会将asp或php后门文 ...
- 转 qInstallMsgHandler实现日志输出
#include <QtDebug> #include <QFile> #include <QTextStream> #define _TIME_ qPrintab ...
- 【Babble】批量学习与增量学习、稳定性与可塑性矛盾的乱想
一.开场白 做机器学习的对这几个词应该比较熟悉了. 最好是拿到全部数据,那就模型慢慢选,参数慢慢调,一轮一轮迭代,总能取得不错效果. 但是面对新来数据,怎么能利用已经训练好的模型,把新的信息加进去? ...
- 一篇文章让你读懂iOS和Android的历史起源
智能手机虽说是移动电话,但我们完全可以将其作为小型化的电脑来思考.这样一来也能够显示出智能手机OS的高性能.我们首先一起来回顾下智能手机OS的历史. OS的黎明期 其实在很早之前就已经有这样的想法,即 ...
- 2.5 Apache Axis2 快速学习手册之JiBx 构建Web Service
5. 使用JiBX生成服务(通过JIBX 命令将wsdl 生成 services ) 要使用JiBX数据绑定生成和部署服务,请执行以下步骤. 通过在Axis2_HOME / samples / qui ...
- [k8s]kube-dns/dashboard排错历险记(含sa加载用法/集群搭建)
kube-dns原理 参考: 组件架构看这个就够了 http://cizixs.com/2017/04/11/kubernetes-intro-kube-dns 设置细节看这个就够了 http://b ...