Problem 2150 Fire Game

Accept: 145    Submit: 542
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

Case 1: 1 Case 2: -1 Case 3: 0 Case 4: 2

Submit Back Status

::今天组队赛我的时间基本都用在这道题上了,马力不强,还是要多注意细节

主要思路:  bfs 。。先bfs遍历看图的连通分量tree。若tree>2,则直接输出-1.

若tree==2,则相当于要遍历两个子图,枚举两个图遍历的起点。

若tree==1,则只要遍历一个图,注意这里可以同时点燃2个草地,枚举1个起点,固定,再枚举第2个起点。

下面的代码现在看自己都有点恐惧,汗:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
using namespace std;
const int INF=;
const int maxn=+;
const int mod=;
typedef long long ll;
struct node
{
int x,y,st;
};
int t;
char g[][];
int biao[][],tt[][];
int dx[]={,,,-};
int dy[]={,-,,};
queue<node> q; int bfs(int x,int y)
{
node u,v;
biao[x][y]=;
int ans=;
u.x=x;u.y=y;u.st=;
q.push(u);
while(!q.empty())
{
u=q.front();
q.pop();
for(int i=; i<; i++)
{
int x1=u.x+dx[i],y1=u.y+dy[i];
if(g[x1][y1]=='#'&&biao[x1][y1]==)
{
biao[x1][y1]=;
v.x=x1;v.y=y1;v.st=u.st+;
q.push(v);
ans=max(v.st,ans);
}
}
}
return ans;
} int bfs2(int x,int y,int a,int b)
{
node u,v;
biao[x][y]=;
biao[a][b]=;
int ans=;
u.x=x;u.y=y;u.st=;
q.push(u);
u.x=a;u.y=b;u.st=;
q.push(u);
while(!q.empty())
{
u=q.front();
q.pop();
for(int i=; i<; i++)
{
int x1=u.x+dx[i],y1=u.y+dy[i];
if(g[x1][y1]=='#'&&biao[x1][y1]==)
{
biao[x1][y1]=;
v.x=x1;v.y=y1;v.st=u.st+;
q.push(v);
ans=max(v.st,ans);
}
}
}
return ans;
} int is_ok(int n,int m)
{
memset(biao,,sizeof(biao));
int i,j;
for(i=; i<=n ;i++)
{
for(j=; j<=m; j++)
{
if(g[i][j]=='#')
{
bfs(i,j);
break;
}
}
if(j<=m) break;
} for(i=; i<=n ;i++)
{
for(j=; j<=m; j++)
{
if(g[i][j]=='#'&&biao[i][j]==)
{
bfs(i,j);
break;
}
}
if(j<=m) break;
}
if(i>n) return ; for(i=; i<=n; i++)
{
for(j=; j<=m; j++)
{
if(g[i][j]=='#'&&biao[i][j]==)
return ;
}
}
return ;
} void fu(int n,int m,int a[][],int b[][])
{
for(int i=; i<=n; i++)
{
for(int j=; j<=m; j++)
a[i][j]=b[i][j];
}
} void run()
{
int T,cas=;
scanf("%d",&T);
while(T--)
{
int n,m,i;
scanf("%d%d",&n,&m);
for(i=; i<=m+; i++)//把边界附上值' .',方便后来处理
{
g[][i]='.';
g[n+][i]='.';
}
for(int i=; i<=n; i++)
{
scanf("%s",g[i]+);
g[i][]=g[i][m+]='.';//
}
int j,k,h,a,b,ans=INF;
//show(n,m);
int tree=is_ok(n,m);
printf("Case %d: ",cas++);
if(tree==) {printf("-1\n"); continue;} for(i=; i<=n; i++)
{
for(j=; j<=m ;j++)
{
if(g[i][j]=='#')
{
memset(biao,,sizeof(biao));
if(tree==)//两个子图
{
a=bfs(i,j);
for(k=i; k<=n; k++ )
{
if(k==i) h=j+;
else h=;
for( ;h<=m ; h++)
{
if(biao[k][h]==&&g[k][h]=='#')
{
fu(n,m,tt,biao);
b=bfs(k,h);
fu(n,m,biao,tt);
int tmp=max(a,b);
ans=min(tmp,ans);
}
}
} }
else//一个子图,固定起点i,j,枚举其后的点
{
for(k=i; k<=n; k++)
{
if(i==k) h=k+; else h=;
for(; h<=m; h++)
{
biao[i][j]=;
if(biao[k][h]==&&g[k][h]=='#')
{
a=bfs2(i,j,k,h);
ans=min(ans,a);
memset(biao,,sizeof(biao));
}
}
} }
} }
}
if(ans==INF) ans=;
printf("%d\n",ans);
}
} int main()
{
//freopen("in.txt","r",stdin);
run();
return ;
}

FZU Problem 2150 Fire Game的更多相关文章

  1. FZU Problem 2150 Fire Game(bfs)

    这个题真要好好说一下了,比赛的时候怎么过都过不了,压点总是出错(vis应该初始化为inf,但是我初始化成了-1....),wa了n次,后来想到完全可以避免这个问题,只要入队列的时候判断一下就行了. 由 ...

  2. FZOJ Problem 2150 Fire Game

                                                                                                        ...

  3. FZU 2150 fire game (bfs)

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

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

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

  5. fzu 2150 Fire Game 【身手BFS】

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

  6. UVA Problem B: Fire!

    Problem B: Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and t ...

  7. FZu Problem 2233 ~APTX4869 (并查集 + sort)

    题目链接: FZu Problem 2233 ~APTX4869 题目描述: 给一个n*n的矩阵,(i, j)表示第 i 种材料 和 第 j 种材料的影响值,这个矩阵代表这n个物品之间的影响值.当把这 ...

  8. FZu Problem 2236 第十四个目标 (线段树 + dp)

    题目链接: FZu  Problem 2236 第十四个目标 题目描述: 给出一个n个数的序列,问这个序列内严格递增序列有多少个?不要求连续 解题思路: 又遇到了用线段树来优化dp的题目,线段树节点里 ...

  9. 翻翻棋(找规律问题)(FZU Problem 2230)

    题目是这样的: FZU Problem 2230 象棋翻翻棋(暗棋)中双方在4*8的格子中交战,有时候最后会只剩下帅和将.根据暗棋的规则,棋子只能上下左右移动,且相同的级别下,主动移动到地方棋子方将吃 ...

随机推荐

  1. jquery ajax跨域访问webservice配置

    1.webservice方法 [System.Web.Script.Services.ScriptService] public class TestService : System.Web.Serv ...

  2. PHP功能齐全的发送邮件类

    下面这个类的功能则很强大,不但能发html格式的邮件,还可以发附件 <?php class Email { //---设置全局变量 var $mailTo = ""; // ...

  3. 内核移植和文件系统制作(4):UBIFS根文件系统制作总结

    UBIFS文件系统简介: 无排序区块图像文件系统(UnsortedBlock Image File System, UBIFS)是用于固态硬盘存储设备上,并与LogFS相互竞争,作为JFFS2的后继文 ...

  4. 安装多JDK后,java编译环境和运行环境版本(JDK版本) 不一致解决:

    由于之前安装过JDK1.7 ,现在一个项目是JDK1.5的,那么需要更改了环境变量了,此处不再赘述如何设置JDK 的环境变量了.然后网上找来方法: 在安装多个jdk后,出现了java -version ...

  5. ASP.NET本质论第一章网站应用程序学习笔记1

    1.统一资源标示符 1) 格式:协议://主机[.端口号][绝对路径[?参数]],在Http://www.kencery.com/hyl/index/login中,http表示协议的名称,www.ke ...

  6. js中的排序

    不靠谱的sort() 众所周知,js中的sort()排序是按字母表顺序排序的,这就导致如下现象: var a = [9,60,111,55,8,7777]; a.sort(); alert(a); / ...

  7. 用css伪类实现提示框效果

    题目要求用css实现下图效果: 很明显难点就在那个多出去的三角形上,下面代码是用一个div来实现的,用到了伪类 : befor和 : after,使用这两个伪类活生生的在div之前和之后多出了&quo ...

  8. chenxi的js学习笔记

    1.本文主体源自:http://www.cnblogs.com/coco1s/p/4029708.html,有兴趣的可以直接去那里看,也可以看看我整理加拓展的. 2.js是一门什么样的语言及特点? j ...

  9. <SharePoint 2013 用户界面设计与品牌化>学习系列之---基础

    什么是SharePoint界面与品牌化设计 这一章主要介绍了: 为什么要品牌化SharePoint 介绍一些内部和互联网的SharePoint网站 简单 中等 复杂的三种品牌化方式 简单难度: 普通用 ...

  10. 解决SwipeRefreshLayout左右滑动事件冲突的问题

    在使用SwipeRefreshLayout时我们注意到在SwipeRefreshLayout中左右滑动时可能也会触发下拉刷新的事件,这点让我们很不爽.追其原因是SwipeRefreshLayout对于 ...