(FZU 2150) Fire Game (bfs)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(FZU 2150) Fire Game (bfs)相关的知识,希望对你有一定的参考价值。
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150
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
Sample Output
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
#define met(a,b) memset(a,b,sizeof(a))
#define N 109
int vis[N][N];
char str[N][N];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int k,n,m;
struct node
{
int x,y,temp;
}a[N];///统计干草的坐标
int pan()///判断干草是否都能被点燃
{
int x,y;
for(int i=0;i<k;i++)
{
x=a[i].x;y=a[i].y;
if(!vis[x][y])
return 0;
}
return 1;
}
int bfs(node a,node b)
{
int team=0;
met(vis,0);
queue<node>Q;
node q,p;
Q.push(a);Q.push(b);
vis[a.x][a.y]=1;vis[b.x][b.y]=1;
while(Q.size())
{
q=Q.front();
Q.pop();
for(int i=0;i<4;i++)
{
p.x=q.x+dir[i][0];
p.y=q.y+dir[i][1];
p.temp=q.temp+1;
if(p.x>=0 && p.x<n&& p.y>=0 && p.y<m && str[p.x][p.y]==‘#‘ && !vis[p.x][p.y])
{
vis[p.x][p.y]=1;
team=max(team,p.temp);
Q.push(p);
}
}
}
if(pan())
return team;
else
return INF;
}
int main()
{
int t,con=1;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++)
scanf("%s",str[i]);
k=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(str[i][j]==‘#‘)
{
a[k].x=i;
a[k].y=j;
a[k++].temp=0;
}
}
}
int ans=INF;
for(int i=0;i<k;i++)
{
for(int j=i;j<k;j++)
{
ans=min(bfs(a[i],a[j]),ans);
}
}
printf("Case %d: ",con++);
if(ans==INF)
printf("-1\n");
else
printf("%d\n",ans);
}
return 0;
}
以上是关于(FZU 2150) Fire Game (bfs)的主要内容,如果未能解决你的问题,请参考以下文章
FZU 2150 Fire Game (高姿势bfs--两个起点)
FZU 2150 Fire Game (高姿势bfs--两个起点)