FZU 2150(BFS+枚举)

版权声明:Dream_dog专属 https://blog.csdn.net/Dog_dream/article/details/82258191

Problem 2150 Fire Game

Accept: 3771    Submit: 12867
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


解法:BFS+暴力枚举两点


#include<cstring>
#include<cstdio>
#include<iostream>
#include<string>
#include<queue>
#include<vector>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<functional>
using namespace std;
#define eb(x) push_back(x)
#define pb(x) push_back(x)
#define ps(x) push(x)
#define clr(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f
#define MAX_N 100+5
#define MAX_M 10005
typedef long long ll;
//typedef priority_queue<int,vector<int>,less<int>> pql;
//typedef priority_queue<int,vector<int>,greater<int>>pqg;
struct Point
{
	int x;
	int y;
	Point(){}
	Point(int x,int y):x(x),y(y){}
};
/*枚举两点*/
int n, m;
char grid[15][15];
int dis[15][15];
queue<Point>que;
vector<Point> ve;
int dx[4] = { -1, 1, 0, 0 };
int dy[4] = { 0, 0, -1, 1 };

int bfs(Point p1, Point p2)
{
	Point cur, nex;
	clr(dis,inf);
	dis[p1.x][p1.y] = 0;
	dis[p2.x][p2.y] = 0;
	que.push(p1);
	que.push(p2);
	while (!que.empty())
	{
		cur = que.front();
		que.pop();
		for (int i = 0; i<4; i++)
		{
			int x = cur.x + dx[i];
			int y = cur.y + dy[i];
			if (x>=0&&y>=0&&x<n&&y<m&&grid[x][y] == '#'&&dis[x][y]>dis[cur.x][cur.y] + 1)
			{
				dis[x][y] = dis[cur.x][cur.y] + 1;
				nex.x = x;
				nex.y = y;
				que.ps(nex);
			}
		}
	}
	int ans = 0;
	for (int i = 0; i <ve.size();i++)
	{
	    int x=ve[i].x,y=ve[i].y;
		ans = max(ans, dis[x][y]);
	}
	return ans;
}

int main()
{
    //freopen("data.txt","r",stdin);
	int t;
	scanf("%d",&t);
	for (int ti = 1; ti <= t; ti++)
	{
	    ve.clear();
		scanf("%d%d",&n,&m);
		for (int i = 0; i<n; i++)
		{
		   for(int j=0;j<m;j++)
           {
               cin>>grid[i][j];
               if(grid[i][j]=='#')
               {
                   ve.pb(Point(i,j));
               }
           }
		}
		int temp;
		int ans = inf,flag=0;
		for(int i=0;i<ve.size();i++)
        {
            for(int j=i+1;j<ve.size();j++)
            {
            flag=1;
			temp = bfs(ve[i],ve[j]);
			ans = min(ans, temp);
            }
        }
		if (ans == inf) ans = -1;
		if (flag==0)ans=0;
		printf("Case %d: %d\n",ti,ans);
	}
}

猜你喜欢

转载自blog.csdn.net/Dog_dream/article/details/82258191