当前位置: 首页 > article >正文

Fire Game (连通块+两点bfs)

题目链接:https://cn.vjudge.net/problem/FZU-2150

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

 

题意:

两个人在一个平面草地上某两个点放火,每一单位时间火就向四周四格蔓延(如果是草地的话),求问两个人最少用多少时间能把整片草地烧完。

 

网上题解:

n,m很小,可以暴力枚举两个人的起始放火点,然后BFS。对于每次BFS,返回把这片草地烧完所需的时间。

我的思路:

先求连通块数目cnt,

cnt大于2,输出-1

cnt等于0,输出0

cnt等于1,块内两点bfs

cnt等于2,每个块内进行单点bfs

(具体看代码)

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;
const int N=15;
int cnt;
int n,m;
struct node{int x,y,temp;node(){}node(int xx,int yy,int tt){x=xx;y=yy;temp=tt;}
};
char mp[N][N];
int t[4][2]={1,0,0,1,0,-1,-1,0};
bool vis[N][N];
void book(int x,int y){vis[x][y]=true;
}
void dfs(int x,int y){      //判断有多少个连通块 (把#变成$) mp[x][y]='$';for(int i=0;i<4;i++){int tx=x+t[i][0];int ty=y+t[i][1];if(tx<1||tx>n||ty<1||ty>m) continue;if(mp[tx][ty]=='#') dfs(tx,ty);}return ;
}
void dfs(int x,int y,char c){//给每个连通块标号 mp[x][y]=c;for(int i=0;i<4;i++){int tx=x+t[i][0];int ty=y+t[i][1];if(tx<1||tx>n||ty<1||ty>m) continue;if(mp[tx][ty]=='$'){dfs(tx,ty,c);} }return ;
}
int bfs(int x,int y){//单点bfs int ans=0;memset(vis,0,sizeof(vis));queue<node> q;q.push(node(x,y,0));book(x,y);while(!q.empty()){node p=q.front();ans=max(ans,p.temp);q.pop();for(int i=0;i<4;i++){int tx=p.x+t[i][0];int ty=p.y+t[i][1];if(tx<1||tx>n||ty<1||ty>m) continue;if(vis[tx][ty]==true||mp[tx][ty]=='.') continue;book(tx,ty);q.push(node(tx,ty,p.temp+1));	}}return ans;
}
int bfs(int x1,int y1,int x2,int y2){//双点bfs int ans=0;memset(vis,0,sizeof(vis));queue<node> q;q.push(node(x1,y1,0));q.push(node(x2,y2,0));book(x1,y1);book(x2,y2);while(!q.empty()){node p=q.front();ans=max(ans,p.temp);q.pop();for(int i=0;i<4;i++){int tx=p.x+t[i][0];int ty=p.y+t[i][1];if(tx<1||tx>n||ty<1||ty>m) continue;if(vis[tx][ty]==true||mp[tx][ty]=='.') continue;book(tx,ty);q.push(node(tx,ty,p.temp+1));	}}return ans;
}
int main(){int T;scanf("%d",&T);for(int Case=1;Case<=T;Case++){scanf("%d%d",&n,&m);for(int i=1;i<=n;i++)scanf("%s",mp[i]+1);cnt=0;//连通块数目 for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){if(mp[i][j]=='#'){cnt++;dfs(i,j);}}}if(cnt>2) printf("Case %d: -1\n",Case);else if(cnt==0) printf("Case %d: 0\n",Case);else{if(cnt==1){  //只有一个连通块,在块内取两个点(可是同一个点)作为起点,遍历所有情况,求最小值 int ans=999999999;vector<node>v;for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){if(mp[i][j]=='$'){v.push_back(node(i,j,0));}}}for(int i=0;i<v.size();i++){for(int j=i;j<v.size();j++){//不能从i+1枚举,可能只有一个点(特判也行)ans=min(ans,bfs(v[i].x,v[i].y,v[j].x,v[j].y));}}printf("Case %d: %d\n",Case,ans);}else{//两个连通块,一人一个,在块内枚举起点 char c='1';for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){if(mp[i][j]=='$'){dfs(i,j,c);c++;}	}}int ans1=999999999;int ans2=999999999;for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){if(mp[i][j]=='1'){ans1=min(ans1,bfs(i,j));}else if(mp[i][j]=='2'){ans2=min(ans2,bfs(i,j));}}}printf("Case %d: %d\n",Case,max(ans1,ans2));}}}return 0;
} 

 

http://www.lryc.cn/news/2414630.html

相关文章:

  • sobel算子实现原理和c++实现sobel()检测边缘函数
  • IO流原理及流的分类FileInputStream类和FileOutputStream类基本介绍
  • 一些使用频率高的指令及库版本安装切换
  • android——Spinner下拉列表案例详解(2),程序员真的是吃青春饭吗
  • PeopleSoft集成EasyExcel
  • 高级网络安全管理员 - 网络设备和安全配置:SSH 配置
  • XAPOFX1_5.dll文件丢失导致程序无法运行问题
  • Go切片(slices)
  • QT_快速入门
  • 深度linux 官网,深度OS
  • BZOJ 4698: Sdoi2008 Sandy的卡片
  • AfxBeginThread和CreateThread具体区别
  • 将 Notepad++ 或 Ultra Edit 添加到鼠标右键菜单
  • 深入了解在Linux中重命名文件和目录的技巧!
  • 关于 DRM 中 DUMB 和 PRIME 名字的由来
  • VirtualBox
  • Word公式编辑器的使用方法
  • 服务器租用一年需要多少钱?
  • 学生成绩管理系统的设计与实现-附源码070942
  • 行列式的几种运算方法
  • 从linaro下载安装二进制文件安装交叉编译工具
  • BCR认证是什么?
  • answer的汉语_Answer的中文意思是什么?
  • 进程间的通信方式(六种)
  • rails 入门笔记
  • 【史上最全,从经典到最新】24种信号分解方法(附matlab代码)
  • N2N组建虚拟局域网
  • USB驱动(一、概念介绍及USB总线驱动程序代码分析)
  • element日期选择器datepicker用法大全
  • UCOS-III 操作系统深度剖析与实战应用教程