码迷,mamicode.com
首页 > 其他好文 > 详细

FZU - 2150 Fire Game

时间:2015-04-14 08:34:19      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

技术分享 Problem 2150 Fire Game

Accept: 623    Submit: 2445
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


        两个熊孩子在n*m的平地上放火玩,#表示草,两个熊孩子分别选一个#格子点火,火可以向上向下向左向右在有草的格子蔓延,点火的地方时间为0,蔓延至下一格的时间依次加一。求烧完所有的草需要的最少时间。如不能烧完输出-1。

依次枚举两个#格子,BFS出需要的时间,取最小的一个。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<cstdio>
#include<queue>
using  namespace std;
#define inf 0x3f3f3f

struct Point 
{
	int x;
	int y;
};

int n, m;
char map[15][15];
int dis[15][15];
queue<Point>que;
int dirx[4] = { 1, -1, 0, 0 };
int diry[4] = { 0, 0, -1, 1 };

int bfs(int x1, int y1, int x2, int y2)
{
	int xx, yy;
	Point p1, p2, cur, nex;
	memset(dis, inf, sizeof(dis));
	p1.x = x1; p1.y = y1;
	p2.x = x2; p2.y = y2;
	dis[x1][y1] = 0;
	dis[x2][y2] = 0;
	que.push(p1);
	que.push(p2);
	while (!que.empty())
	{
		cur = que.front();
		que.pop();
		for (int i = 0; i<4; i++)
		{
			xx = cur.x + dirx[i];
			yy = cur.y + diry[i];
			if (xx >= 0 && xx<n&&yy>=0 && yy<m&&map[xx][yy] == '#'&&dis[xx][yy]>dis[cur.x][cur.y] + 1)
			{
				dis[xx][yy] = dis[cur.x][cur.y] + 1;
				nex.x = xx;
				nex.y = yy;
				que.push(nex);
			}
		}
	}
	int maxx = 0;
	for (int i = 0; i < n;i++)
	for (int j = 0 ; j < m; j++)
	if (map[i][j] == '#')
	{
		maxx = max(maxx, dis[i][j]);
	}
	return maxx;
}

int main()
{
	int casen;
	cin >> casen;
	for (int cas = 1; cas <= casen; cas++)
	{
		while (!que.empty())
			que.pop();
		cin >> n >> m;

		for (int i = 0; i < n; i++)
			cin >> map[i];
	
		int temp;
		int ans = inf;
		for (int i = 0; i < n;i++)
		for (int j = 0; j < m;j++)
		if (map[i][j] == '#')
		for (int ii = 0; ii < n;ii++)
		for (int jj = 0; jj < m;jj++)
		if (map[ii][jj] == '#')
		{
			temp = bfs(i, j, ii, jj);
			ans = min(ans, temp);
		}
		if (ans == inf) ans = -1;     //枚举的各个点都存在有草没烧到
		cout << "Case " << cas << ": " << ans << endl;
	}
}



FZU - 2150 Fire Game

标签:

原文地址:http://blog.csdn.net/qq_18738333/article/details/45034657

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!