FZU 2150(DFS+BFS)

Problem 2150 Fire Game

Accept: 1357    Submit: 4807
Time Limit: 1000 mSec    Memory Limit : 32768 KB

《FZU 2150(DFS+BFS)》 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.

《FZU 2150(DFS+BFS)》 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

《FZU 2150(DFS+BFS)》 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.

《FZU 2150(DFS+BFS)》 Sample Input

43 3.#.###.#.3 3.#.#.#.#.3 3…#.#…3 3###..##.#

《FZU 2150(DFS+BFS)》 Sample Output

Case 1: 1Case 2: -1Case 3: 0Case 4: 2

Submit  Back  Status  Discuss

题意:你可以从2个地方点火,问能否把所有的草烧光,在一个联通块里的草火会传递,如果可以烧掉所有的草输出最少的时间

题解:这一题一定要小心的写,稍微写的不好就超时了。。。。。。。。。。。,开始思路是对的,但是一直TLE,先说思路,先使用DFS判断联通块的个数,枚举2个点进行BFS找最短时间,这里可以先把所有的草的点记录下来,双重循环枚举跑一下,(开始是使用四重循环,枚举2个点,现在想一下可能会重复的跑2个点,导致了超时)。注意这里如果有一棵草是搜不出来的,要特判一下

#include<cstdio>  
#include<cstring>  
#include<cstdlib>  
#include<cmath>  
#include<iostream>  
#include<algorithm>  
#include<vector>  
#include<map>  
#include<set>  
#include<queue>  
#include<string>  
#include<bitset>  
#include<utility>  
#include<functional>  
#include<iomanip>  
#include<sstream>  
#include<ctime>  
using namespace std;

#define N int(20)  
#define inf int(0x3f3f3f3f)  
#define mod int(1e9+7)  
typedef long long LL;

char s[N][N];
int vis[N][N];
int vx[] = { 0, 0, 1, -1 };
int vy[] = { 1, -1, 0, 0 };
int kuai,tot;
int n, m;
bool judge(int x, int y)
{
	return (x >= 0 && x<n&&y >= 0 && y<m);
}
int bfs(int x, int y, int x2, int y2)
{
	int sum = 0;
	int tans = -1;
	memset(vis, 0, sizeof(vis));
	vis[x][y] = 1;
	vis[x2][y2] = 1;
	pair<int, pair<int, int> > top;
	queue<pair<int, pair<int, int> > >q;
	q.push(make_pair(0, make_pair(x, y)));
	q.push(make_pair(0, make_pair(x2, y2)));
	while (!q.empty())
	{
		sum++;
		top = q.front(); q.pop();
		tans = max(tans, top.first);
		for (int i = 0; i<4; i++)
		{
			int tx = top.second.first + vx[i];
			int ty = top.second.second + vy[i];
			int dep = top.first;
			if (!judge(tx, ty) || vis[tx][ty] || s[tx][ty] != '#')
				continue;
			vis[tx][ty] = 1;
			q.push(make_pair(dep + 1, make_pair(tx, ty)));
		}
	}
	if(sum==tot)
	return tans;
	return inf;
}

void dfs(int x, int y)
{
	vis[x][y] = 0;
	for (int i = 0; i<4; i++)
	{
		int xx = vx[i] + x;
		int yy = vy[i] + y;
		if (judge(xx, yy) && vis[xx][yy] && s[xx][yy] == '#')
		{
			dfs(xx, yy);
		}
	}
}

vector<pair<int, int> >pos;
int main()
{
#ifdef CDZSC  
	freopen("i.txt", "r", stdin);
	//freopen("o.txt", "w", stdout);
	int _time_jc = clock();
#endif  

	int t, k = 1;
	scanf("%d", &t);
	while (t--)
	{
		pos.clear();
		scanf("%d%d", &n, &m);
		tot = 0;kuai = 0;
		memset(vis, 0, sizeof(vis));
		for (int i = 0; i < n; i++)
			scanf("%s", s[i]);
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
			{
				if (s[i][j] == '#')
				{
					vis[i][j] = 1;
					tot++;
					pos.push_back(make_pair(i, j));
				}
			}
		}
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j<m; j++)
			{
				if (vis[i][j])
				{
					dfs(i, j);
					kuai++;
				}
			}
		}
		if (tot == 1)
		{
			printf("Case %d: %d\n", k++, 0);
			continue;
		}
		int realans = inf;
		if (kuai <=2)
		{
			for (int i = 0; i<pos.size(); i++)
			{
				for (int j = i+1; j<pos.size(); j++)
				{
					realans=min(realans,(bfs(pos[i].first, pos[i].second, pos[j].first, pos[j].second)));
				}
			}
		}
		printf("Case %d: %d\n", k++, realans==inf?-1:realans);

	}
	return 0;
}

    原文作者:DFS
    原文地址: https://blog.csdn.net/qq_24489717/article/details/51331611
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞