题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150
题目大意:给定n*m地图,#代表可以烧的草,.代表空地,选择两个起火点(可以是同一个点),火每分钟可以向四个方向扩散,问能否把地图上的草烧完,可以要求输出最短时间,否则输出-1
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
题目思路:先用dfs求出最小块和有多少个点,然后枚举两个起火点,bfs路径得到最短时间
代码:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)
int n,m,maxx,ans;
int cnt; //记录总数
int mp[15][15];
int vis[15][15];
int dir[4][2]={1,0,-1,0,0,1,0,-1};
struct node
{
int x,y;
int step;
};
bool check(int x,int y)
{
if(x<=0||x>n||y<=0||y>m||vis[x][y]==1||mp[x][y]=='.')
return false;
return true;
}
void dfs(int x,int y)
{
vis[x][y]=1;
//cout<<"enter11.."<<"x="<<x<<",y="<<y<<endl;
cnt++;
for(int i=0;i<4;i++)
{
int next_x=x+dir[i][0];
int next_y=y+dir[i][1];
if(check(next_x,next_y))
dfs(next_x,next_y);
}
}
int fun(int x,int y)
{
if(x%y==0)
return x/y;
return x/y+1;
}
void bfs(int i,int j,int k,int l)
{
queue<node>q;
MEM(vis,0);
node a,b,now,next;
int tmp;
if(i==k&&j==l)
{
a.x=i;
a.y=j;
a.step=0;
q.push(a);
vis[i][j]=1;
tmp=1;
}
else
{
a.x=i;
a.y=j;
a.step=0;
q.push(a);
vis[i][j]=1;
a.x=k;
a.y=l;
a.step=0;
q.push(a);
vis[k][l]=1;
tmp=2;
}
int maxx=0;
while(!q.empty())
{
now=q.front();
q.pop();
for(int i=0;i<4;i++)
{
next.x=now.x+dir[i][0];
next.y=now.y+dir[i][1];
next.step=now.step+1;
if(check(next.x,next.y))
{
vis[next.x][next.y]=1;
q.push(next);
maxx=max(maxx,next.step);
tmp++;
}
}
}
if(tmp==cnt)
ans=min(maxx,ans);
return ;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
//std::ios::sync_with_stdio(false);
int t,xx=0;
scanf("%d",&t);
while(t--)
{
xx++;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf(" %c",&mp[i][j]);
maxx=0;
cnt=0;
int x=0;
MEM(vis,0);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(mp[i][j]=='#'&&vis[i][j]==0)
{
x++;
dfs(i,j);
}
}
}
//cout<<"maxx="<<maxx<<endl;
//cout<<"cnt="<<cnt<<endl;
//cout<<"x="<<x<<endl;
if(x>2)
printf("Case %d: %d\n",xx,-1);
else
{
ans=1000000;
//枚举两个点
if(cnt<=2)
ans=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(mp[i][j]=='#')
{
for(int k=1;k<=n;k++)
{
for(int l=1;l<=m;l++)
{
if(mp[k][l]=='#')
{
if(k>i||(k==i&&l>=j))
{
//cout<<"1"<<endl;
bfs(i,j,k,l);
}
}
}
}
}
}
}
printf("Case %d: %d\n",xx,ans);
}
}
return 0;
}