Accept: 3033 Submit: 10475
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
Sample Output
着火问题bfs,题意是给你一个图。
可以设置两个着火点,每分钟会向四个方向扩散。
着火点可以任意选取。
如果能够点燃所有草地,则输出最短所需时间,否则输出-1
做法是依次遍历图,遇到草地则从当前点开始继续往下寻找草地,构成两个点。
具体的bfs过程:将两个着火点加入队列。
将符合条件的点加入队列。
最后遍历结束后,遍历used数组,如果有草地没有被走到return -1
遍历所有的time值找出其中的最大值返回。
最后找到最小的时间值。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <string>
#include <iostream>
#include <math.h>
#include <queue>
#include <vector>
int n,m;
const int maxn = 1e5;
using namespace std;
bool used[200][200];
char maze[50][50];
typedef pair<int,int> P;
int ans = -1;
int time[200][200];
int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};
int bfs(int x1,int y1,int x2,int y2)
{
queue<P> Q;
ans = -1;
Q.push(P(x1,y1));
Q.push(P(x2,y2));
while(Q.size()){
P now = Q.front();
Q.pop();
for(int i = 0; i < 4; i++)
{
int px = now.first + dx[i],py = now.second + dy[i];
if(px >= 0 && px < n && py >= 0 && py < m && used[px][py] == 0 && maze[px][py] == '#')
{
used[px][py] = 1;
time[px][py] = time[now.first][now.second] + 1;
Q.push(P(px,py));
}
}
}
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
if(used[i][j] == 0 && maze[i][j] == '#')
return -1;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
if(maze[i][j] == '#')
ans = max(ans,time[i][j]);
return ans;
}
int main()
{
std::ios::sync_with_stdio(false);
int T;
cin>>T;
for(int index = 1; index <= T; index++)
{
cin>>n>>m;
for(int i = 0; i < n; i++)
cin>>maze[i];
int res = 0x3f3f3f3f;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
{
if(maze[i][j] == '#')
for(int ii = i; ii < n; ii++)
for(int jj = 0; jj < m; jj++)
{
if(ii == i && jj <j) continue;
if(maze[ii][jj] == '#')
{
used[i][j] = 1;
used[ii][jj] = 1;
int tmp = bfs(i,j,ii,jj);
if(tmp != -1)
res = min(res,tmp);
memset(used,0,sizeof(used));
memset(time,0,sizeof(time));
}
}
}
cout<<"Case "<<index<<": ";
res = (res == 0x3f3f3f3f) ? -1: res;
cout<<res<<endl;
}
return 0;
}