foj 2150 Fire Game(bfs暴力)

时间:2022-06-16 03:47:56
 
 

foj 2150 Fire Game(bfs暴力) 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+ which refers to the grid (x+, y), (x-, y), (x, y+), (x, y-). 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.

foj 2150 Fire Game(bfs暴力) 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.

 <= T <=,  <= n <=,  <= m <=

foj 2150 Fire Game(bfs暴力) 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 -. See the sample input and output for more details.

foj 2150 Fire Game(bfs暴力) Sample Input


.#.
###
.#. .#.
#.#
.#. ...
#.#
... ###
..#
#.#

foj 2150 Fire Game(bfs暴力) Sample Output

Case :
Case : -
Case :
Case :

暴力枚举两点,bfs统计,最后求最小值。

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 16
#define inf 1<<26
int n,m;
char mp[N][N];
int vis[N][N];
int dirx[]={,,-,};
int diry[]={-,,,};
struct Node{
int x,y;
int t;
}node[N*N]; int bfs(Node a,Node b){
queue<Node>q;
q.push(a);
q.push(b);
vis[a.x][a.y]=;
vis[b.x][b.y]=;
int T=;
while(!q.empty()){
Node tmp=q.front();
q.pop();
Node cnt;
T=max(T,tmp.t);
for(int i=;i<;i++){
cnt.x=tmp.x+dirx[i];
cnt.y=tmp.y+diry[i];
if(cnt.x< || cnt.x>=n || cnt.y< || cnt.y>=m) continue;
if(vis[cnt.x][cnt.y]) continue;
if(mp[cnt.x][cnt.y]=='.') continue;
vis[cnt.x][cnt.y]=;
cnt.t=tmp.t+;
q.push(cnt);
}
}
return T;
} int main()
{
int t,ac=;
scanf("%d",&t);
while(t--){
int num=;
scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
scanf("%s",mp[i]);
for(int j=;j<m;j++){
if(mp[i][j]=='#'){
node[num].x=i;
node[num].y=j;
node[num++].t=;
}
}
} printf("Case %d: ",++ac);
if(num<=){
printf("0\n");
continue;
} int ans=inf;
for(int i=;i<num;i++){
for(int j=i;j<num;j++){
int flag=;
memset(vis,,sizeof(vis));
int w=bfs(node[i],node[j]);
//printf("===%d\n",w);
for(int k=;k<n;k++){
for(int l=;l<m;l++){
if(mp[k][l]=='#' && vis[k][l]==){
flag=;
break;
}
}
if(flag==){
break;
}
}
if(flag){
ans=min(ans,w);
}
}
} if(ans!=inf){
printf("%d\n",ans);
}
else{
printf("-1\n");
}
}
return ;
}