Problem 2150 Fire Game (DFS+BFS)

Problem 2150 Fire Game

Accept: 3417    Submit: 11710
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,否则输出,可以的最短时间。

                对于第一个问题,能否全部烧到,就是看有多少连通区域,没遇到没经历过地方用DFS查一遍,看看有多少联通块就好了。

               对于第二个问题,因为数据范围挺小的(10*10),所以我们就可以直接暴力一边,每个情况都查找一下,看每种情况下的最长时间是多少,然后到最后再看看这些时间里面用时最短的是多少就好了;

#include<iostream>
#include<cstdio>
#include<queue>
#include<utility>
#include<cstring>
using namespace std;
#define inf 0x3f3f3f3f
int vis[20][20];
char map[20][20];
int fx[4][2] = { 1,0,0,1,-1,0,0,-1 };
int m, n;
int BFS(int a, int b, int c, int d)
{
	memset(vis, 0, sizeof(vis));
	queue<pair<int, int>>q;
	q.push(make_pair(a, b));
	q.push(make_pair(c, d));
	vis[a][b] = 1;
	vis[c][d] = 1;
	int maxn = 0;
	while (!q.empty())
	{
		pair<int, int>top = q.front();
		q.pop();
		maxn = max(maxn, vis[top.first][top.second]);
		for (int s = 0; s < 4; s++)
		{
			int r = top.first + fx[s][0];
			int e = top.second + fx[s][1];
			if (r >= 0 && r < m&&e >= 0 && e < n && vis[r][e] == 0 && map[r][e] == '#')
			{
				vis[r][e] = vis[top.first][top.second] + 1;
				q.push(make_pair(r, e));
			}
		}
	}
	for (int s = 0; s < m; s++)
	{
		for (int w = 0; w < n; w++)
		{
			if (vis[s][w] == 0 && map[s][w] == '#')
			{
				return inf;
			}
		}
	}
	return maxn-1;
}
void DFS(int x, int y)
{
//	cout << x << " " << y << endl;
	for (int s = 0; s < 4; s++)
	{
		int ri = x + fx[s][0];
		int li = y + fx[s][1];
		if (ri >= 0 && ri < m && li >= 0 && li < n && vis[ri][li]==0 && map[ri][li]=='#')
		{
			vis[ri][li] = 1;
			DFS(ri, li);
		}
	}
}
int main()
{
	int te;
	scanf("%d", &te);
	int cas = 1;
	while (te--)
	{
		ios::sync_with_stdio(0);
		int ans = inf;
		memset(vis, 0, sizeof(vis));
		cin >> m >> n;
		for (int s = 0; s < m; s++)
		{
			for (int w = 0; w < n; w++)
			{
				cin >> map[s][w];
			}
		}
		int sum = 0;
		for (int s = 0; s < m; s++)
		{
			for (int w = 0; w < n; w++)
			{
				if (map[s][w] == '#' && vis[s][w] == 0)
				{
					DFS(s, w);
					sum++;
				}
			}
		}
		if (sum > 2)
		{
			printf("Case %d: -1\n", cas++);
			continue;
		}
		for (int x1 = 0; x1 < m; x1++)
		{
			for (int y1 = 0; y1 < n; y1++)
			{
				for (int x2 = 0; x2 < m; x2++)
				{
					for (int y2 = 0; y2 < n; y2++)
					{
						if (map[x1][y1] == '#'&&map[x2][y2] == '#')
							ans = min(ans, BFS(x1, y1, x2, y2));
					}
				}
			}
		}
		printf("Case %d: %d\n", cas++, ans);
	}
	return 0;
}

全部评论

相关推荐

个人背景:学院二本计科专业&nbsp;大二开始实习个人经历:安克创新&nbsp;、理想汽车、字节跳动碎碎念:我做事只有三分钟热度。看到进了大厂的同学,我会羡慕,也会跟着努力上进;但遇到好看的小说,我又会放下手头的事沉迷其中,之前的坚持也就中断了。我有些自卑,总觉得自己学历和外貌都不够好。之前偶然在网上受到关注,我就喜欢上了上网,因为这里有很多人认可我。但我也很在意别人的评价,偶尔看到嘲讽的言论,会触发我的自卑情绪,让我感到愤怒。有时候我会强硬地回怼,有时候又会懦弱地选择无视。我也有虚荣心。不管是拿到安克、理想还是字节的机会,我在分享的时候都会带着这份心思。我会特意强调自己学历不好,是为了衬托出过程的艰难,以此显得自己更厉害。我知道,人往往会炫耀自己缺少的东西,来掩盖内心的空洞。我总想着走捷径,不太喜欢踏踏实实地做事。找实习的时候,我花了更多时间在研究面试技巧上,而不是提升专业能力。我会反复听面试录音分析技巧,看面试教程学习怎么和不同的面试官沟通,还会每天自言自语练习语言表达,同学都觉得我有点奇怪。我的实习生涯里,侥幸和运气占了很大一部分。我总在想,如果有一天我失去了这份幸运,这些特质可能会让我一蹶不振。ps:&nbsp;很多人会问我学习路线和经验&nbsp;但是就像我上面说的&nbsp;我的实习过程靠的很多是关键节点的运气&nbsp;技术上面我可能不如很多人&nbsp;&nbsp;所以请大家理性求助和理性参考我的回答&nbsp;附上我的投递记录
我的offer在哪里...:从去年看到现在,飞升哥就是榜样
我的求职进度条
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务