Time Limit:1000MS Memory Limit:32768KB
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
题目链接:FZU-2150
题目大意:有两个小朋友在草坪上烧草,他俩同时放火烧,烧第一块的时候是不花时间的,每一块着火的都可以在下一秒烧向上下左右四块#代表草地,.代表着不能烧的。问你最少花多少时间可以烧掉,如果烧不掉就输出-1
题目思路:BFS,暴力选取两个点作为起点。然后判断是否可行,并更新答案。
以下是代码:
//
// I.cpp
// 搜索
//
// Created by pro on 16/3/25.
// Copyright (c) 2016年 pro. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#include<iomanip>
using namespace std;
struct node
{
int x,y,cnt;
};
string s[100];
int vis[20][20];
vector <pair <int,int> > block;
int dx[4] = {0,0,-1,1};
int dy[4] = {-1,1,0,0};
int n,m;
#define INF 99999999
queue <node> que;
node init(int num1,int num2,int num3)
{
node tmp;
tmp.x = num1;
tmp.y = num2;
tmp.cnt = num3;
return tmp;
}
int bfs(pair<int, int> a,pair<int, int> b)
{
node aa = init(a.first,a.second,0);
node bb = init(b.first,b.second,0);
que.push(aa);
que.push(bb);
int max_step = 0;
while(!que.empty())
{
node front = que.front();
que.pop();
for (int i = 0; i < 4; i++)
{
int x = front.x + dx[i];
int y = front.y + dy[i];
if (!vis[x][y] && x >= 0 && x < n && y >= 0 && y < m && s[x][y] == '#')
{
vis[x][y] = 1;
node tmp = init(x,y,front.cnt + 1);
que.push(tmp);
}
}
max_step = max(max_step,front.cnt);
}
return max_step;
}
int main()
{
int t;
cin >> t;
int cntt = 1;
while(t--)
{
block.clear();
while (!que.empty()) {
que.pop();
}
cin >> n >> m;
for (int i = 0;i < n; i++) cin >> s[i];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (s[i][j] == '#')
{
block.push_back(make_pair(i, j));
}
}
}
printf("Case %d: ",cntt++);
if (block.size() <= 2)
{
cout << 0 << endl;
continue;
}
int ans = INF;
for(int i = 0; i < block.size(); i++)
{
for (int j = i; j < block.size(); j++)
{
memset(vis,0,sizeof(vis));
vis[block[i].first][block[i].second] = 1;
vis[block[j].first][block[j].second] = 1;
int tmp = bfs(block[i],block[j]);
int flag = 0;
for (int k = 0; k < n; k++)
{
for (int h = 0; h < m; h++)
{
if (vis[k][h] == 0 && s[k][h] == '#')
{
flag = 1;
break;
}
}
if(flag == 1) break;
}
if (flag == 0)
{
ans = min(ans,tmp);
}
}
}
if (ans == INF) cout << -1 << endl;
else cout << ans << endl;
}
return 0;
}