FZU 2150(DFS+BFS)

Problem 2150 Fire Game

Accept: 1357    Submit: 4807

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

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

 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;
}
时间: 2024-11-10 15:53:46

FZU 2150(DFS+BFS)的相关文章

POJ 3083:Children of the Candy Corn(DFS+BFS)

Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: 4039 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, ch

POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE

POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 2)再输出右转优先时,从S到E的步数 3)最后输出S到E的最短步数 解题思路: 前两问DFS,转向只要控制一下旋转方向就可以 首先设置前进方向对应的数字 向上--N--0 向右--E--1 向下--S--2 向左--W--3 比如说右转优先,即为向右,向前,向左,向后,即逆时针方向for(int i

POJ 3083 Children of the Candy Corn (DFS+BFS)

题目链接:http://poj.org/problem?id=3083 题目大意:给你一个迷宫,S是起点,E是终点,#是墙,.是路,S.E在迷宫的边界,并且有唯一解:求优先左转S到E的步数,优先右转S到E的步数,以及S到E的最短步数. 题解: 1.本题的难点在于左转优先以及右转优先,下一步的方向取决于当前位置的方向,用DFS不断的按优先方向遍历一遍迷宫即可:我定义如图:   前(0)   左(1) 当前位置方向(dir) 右(3)   后(2)   以左转优先为例,便利迷宫的方向依次为:左.前.

搜索模板(DFS/BFS)

DFS int b[4][2] = {-1,0,0,1,1,0,0,-1}; int DFS( pair<int,int> x ) { int res=0; visited[x.first][x.second]=1; for( int i=0;i<4;i++ ){ pair<int,int> tmp; tmp.first=x.first+b[i][0]; tmp.second=x.second+b[i][1]; if( check(tmp) ) { visited[tmp.f

FZOJ 2150 Fire Game (DFS + BFS)

题目链接:Fire Game 题意:一块n*m的矩形中,'#'代表是草,'.'代表空地,空地点不着火.两个人同时开始点火,问最短多少时间能把所有草地点着,不能输出'-1'. 解析:先用dfs预判断草地的连通块数,超过2则无法全部点燃 任选两个草地作起点,两者看作是一个整体,用bfs搜到起点到所有草地的最短时间,然后保留其中最长的时间 在所有的最长时间中,选择最短的,即为所求. AC代码: #include <cstdio> #include <cstring> #include &

HDU 3313 Key Vertex(dfs + bfs)

HDU 3313 Key Vertex 题目链接 题意:一个有向无环图,求s,t之间的割点 思路:先spfa找一条最短路出来,如果不存在,就n个都是割点. 然后每次从s进行dfs,找到能经过最短路上的最远点,然后这个点就是割点,然后下次在以这个为起点dfs,不断迭代直到找到t为止 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> #include <a

数据结构 - 图的存储结构表示及其遍历 (DFS &amp;&amp; BFS)

1.邻接矩阵表示的图结构 /* 邻接矩阵表示的图结构 */ #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <queue> #include <stack> using namespace std; typedef char VertexType; //顶点类型应由用户定义 typedef int EdgeType; /

UVA 657-The die is cast(双重BFS)

InterGames is a high-tech startup company that specializes in developing technology that allows users to play games over the Internet. A market analysis has alerted them to the fact that games of chance are pretty popular among their potential custom

hdu 5024 Wang Xifeng&#39;s Little Plot (dfs+暴力)

Wang Xifeng's Little Plot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 194    Accepted Submission(s): 131 Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>)