题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150
Accept: 1661 Submit: 5947
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
4 3 3 .#. ### .#. 3 3 .#. #.# .#. 3 3 ... #.# ... 3 3 ### ..# #.#
Sample Output
Case 1: 1 Case 2: -1 Case 3: 0 Case 4: 2
题目大意:给出一个地图,#代表草地,有两个人去点火,可能点同一片草地,火焰可以上下左右的蔓延,如果能把草地烧完,输出最少的次数,
如果不能输出-1
解析:双重BFS,直接暴力下就好了,把草地两两组合下,找最小值
借鉴大牛的博客:http://blog.csdn.net/y990041769/article/details/41309759
代码如下:
#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<string>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#define N 19
using namespace std;
const int inf = 1e9;
const int mod = 1<<30;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef long long LL;
char mp[N][N];
int book[N][N], m, n, ans;
int nx[6] = {-1, 0, 0, 1};
int ny[6] = {0, -1, 1, 0};
struct node
{
int x, y, sept;
};
vector<node> v;
void bfs(node a, node b)
{
int sept = 2, x, y, k = v.size();
int h = 0;
queue<node> q;
memset(book, 0, sizeof(book));
if(a.x == b.x && a.y == b.y) sept--;
q.push(a);
q.push(b);
book[a.x][a.y] = book[b.x][b.y] = 1;
while(!q.empty())
{
a = q.front(); q.pop();
h = max(h, a.sept);
for(int i = 0; i < 4; i++)
{
x = a.x + nx[i]; y = a.y + ny[i];
if(x < 1 || y < 1 || x > m || y > n) continue;
if(book[x][y] || mp[x][y] == '.') continue;
book[x][y] = 1;
b.x = x; b.y = y; b.sept = a.sept + 1;
q.push(b);
sept++;
}
}
if(sept == k)
ans = min(ans, h);
}
int main()
{
int i, j, t, cnt = 0;
cin >> t;
while(t--)
{
v.clear();
scanf("%d%d", &m, &n);
for(i = 1; i <= m; i++)
{
for(j = 1; j <= n; j++)
{
scanf(" %c", &mp[i][j]);
if(mp[i][j] == '#') v.push_back( (node){i, j, 0} );
}
}
ans = inf;
for(i = 0; i < v.size(); i++)
{
for(j = i; j < v.size(); j++)
{
bfs(v[i], v[j]);
}
}
if(ans == inf) ans = -1;
printf("Case %d: %d\n", ++cnt, ans);
}
return 0;
}