Fire Game (BFS)

胖哥哥和Maze在一个N*M的棋盘上玩一个特别的游戏,棋盘上有些格子是草地。他们选取两个草地开始点火,火会按BFS方式蔓延到相邻的草地。当所有草地都被点燃,他们会进行更特别的游戏。任务是理解火的传播规律并分析游戏过程。

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,其实就是在地图中一次枚举两个点,一开始就将两个点燃的点入队列,然后按正常的BFS方式做,但是要让队列为空时停止,然后选取队列为空时所走步骤的最大值,因为只有在队列为空时才能说明一开始两个点燃的草坪已经不能再往外烧了,然后在判断一下是否所有草坪烧完了,如果烧完,再取最小值

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std;

typedef struct node
{
	int x,y;
	int time;
}Q;

const int inf = 99999999;
int way[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int book[15][15];
char maze[15][15];
int t,n,m;
int minn;
int cnt;	//草坪数量 
Q pos[150];	//记录草坪的位置 

int bfs(int a,int b)
{
	queue<Q>q;
	Q cur,next;
	int maxn = -1;
	
	cur.x = pos[a].x;
	cur.y = pos[a].y;
	cur.time = 0;
	q.push(cur);
	cur.x = pos[b].x;
	cur.y = pos[b].y;
	cur.time = 0;
	q.push(cur);
	book[pos[a].x][pos[a].y] = 1;
	book[pos[b].x][pos[b].y] = 1;
	
	while(!q.empty())
	{
		cur = q.front();
		q.pop();

		if (maxn<cur.time)
			maxn = cur.time;
			
		for(int i=0; i<4; i++)
		{
			next.x = cur.x+way[i][0];
			next.y = cur.y+way[i][1];
			if (next.x<0 || next.y<0 || next.x>=n || next.y>=m)
				continue;
			if (book[next.x][next.y]==0 && maze[next.x][next.y]=='#')
			{
				next.time = cur.time+1;
				book[next.x][next.y] = 1;
				q.push(next);		
			}	
		}	
	} 
	return maxn;
}

int main()
{
	scanf("%d",&t);
	
	for(int T=1; T<=t; T++)
	{
		scanf("%d%d",&n,&m);
		cnt = 0;
		
		for(int i=0; i<n; i++)
		{
			scanf("%s",maze[i]);
			for(int j=0; j<m; j++)
			{
				if (maze[i][j]=='#') 	//计数草坪数量并记录位置 
				{
					cnt++;
					pos[cnt].x = i;
					pos[cnt].y = j;
				}	
			}
		}
	
		printf("Case %d: ",T);
		if (cnt<=2)
		{
			printf("0\n");
			continue;
		}
		
		minn = inf;
		int flag,tmp;
		for(int i=1; i<=cnt; i++)	
		{
			for(int j=i; j<=cnt; j++)
			{	
				flag = 1;
				memset(book,0,sizeof(book));
				tmp = bfs(i,j);		
				//广搜完之后要判断是否全部烧完
				//检查是否全部烧尽 
				for(int k=0; k<n; k++)
				{
					for(int l=0; l<m; l++)
					{
						if (book[k][l]==0 && maze[k][l]=='#')	//有没有烧的草坪 
						{
							flag = 0;
							break;
						}
					}
					if (flag==0)
						break;
				} 
		
				if (flag)	//草坪全部被烧,比较最小值 
				{
					if (minn > tmp)
					{
						minn = tmp;	
					}	
				} 
			}
		}
		if (minn==inf)
			printf("-1\n");
		else
			printf("%d\n",minn);
	}
	return 0;
}


c++题解,代码没有注释,可从上网查询,输出必须符合样例,本题有8个测试点,请得满分。 T-2 Breaking Through 分数 35 作者 陈越 单位 浙江大学 In a war game, you are given a map consists of n×n square blocks. Starting from your current block, your task is to conquer a destination block as fast as you can. The difficulties are not only that some of the blocks are occupied by enemies, but also that they are shooting in some directions. When one is shooting in some direction, the whole row/column in that direction will be covered by fire, unless there is another enemy blocking the way. You must make sure that you are not shot on the way to your destination. However, it is very much likely, at the very beginning, that this task is impossible. So you must follow the following instructions: Step 1: find the shortest path from the starting block to the destination, avoiding all the enemy blocks. The path length is the number of blocks on the way, not including the starting block. If this path is not unique, select the smallest index sequence – that is, all the blocks are indexed from 1 to n 2 , starting from the upper-left corner, and row by row till the lower-right corner. An index sequence is an ordered sequence of the block indices from the starting block to the destination. One sequence { s,u 1 ​ ,⋯,u k ​ ,d } is said to be smaller than { s,v 1 ​ ,⋯,v k ​ ,d }, if there exists 1≤p≤k such that u i ​ =v i ​ for i<p and u p ​ <v p ​ . Step 2: if some of the blocks along the selected shortest path is covered by fire, conquer and clear the nearest reachable enemy block that are firing at the path (in case that such a block is not unique, take the one with the smallest index). Then take that block as the starting position, goto step 1. Keep in mind that you must always make sure that you are not shot on the way. If step 2 is impossible, then the game is over. Otherwise, keep going until you finally conquer the destination. Input Specification: Each input file contains one test case. For each case, the first line contains a positive integer n (3≤n≤100), which is the size of the map. Then n lines follow, each gives n numbers. Each number describes the status of the corresponding block: 0 means the block is clear; 1, 2, 3, or 4 means the block is occupied by an enemy, who will shoot toward up, down, left, and right, respectively; 5 means the block is your starting position; 6 means the block is your destination. All the numbers in a line are separated by a space. Note: You can never cross any of the boundaries of the map. When an enemy is shooting from block A in direction B, every block starting from A in direction B will be covered by fire, untill the boundary is reached, or another enemy block is encountered. The enemies will never kill each other. Only you will get killed if you step into a block that is covered by fire. It is guaranteed that there is no more than n enemy blocks, and your starting position is not on fire. Output Specification: Print in a line the path length for you to reach the last block from your starting position, and the block number. The two numbers must be separated by 1 space. In the next line, print Win if the destination is reached, or Lose if not. Sample Input 1: 6 6 2 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 1 0 0 0 0 3 5 0 4 0 0 0 0 0 Sample Output 1: 10 1 Win Hint: The movements are shown by the following figures 14. Figure 1 shows how the blocks are numbered. Figure 2 shows the initial status of the map, where the player’s position is green, enemy blocks are black, and red blocks are covered by fire. At the very beginning the shortest path was 29->30->24->18->12->11->10->9->8->7->1, but the path was covered by fire shooting from blocks 23, 14 and 2. Since the enemy block 23 is the nearest reachable one, it was cleared with path length 1. Now starting from block 23, the shortest path was 23->17->11->10->9->8->7->1, but the path was covered by fire shooting from blocks 14 and 2. Since the enemy block 14 is covered by fire from block 2, taking block 2 now is the only option. So next, block 2 is cleared with path length 8 (crossing the destination). Finally we can get to the destination from block 2 to 1. Sample Input 2: 4 5 0 2 0 0 0 0 0 0 1 0 0 4 0 6 3 Sample Output 2: 6 3 Lose Hint: The movements are shown by the following figures 5~8. Figure 5 shows how the blocks are numbered. Figure 6 shows the initial status of the map. At the very beginning the shortest path was 1->2->6->7->11->15, but the path was covered by fire shooting from blocks 3, 10, 13 and 16. Since the enemy block 10 is the nearest reachable one, it was cleared with path length 3. Now starting from block 10, the shortest path was 10->11->15, but the path was covered by fire shooting from blocks 3, 13 and 16. Since the enemy blocks 13 and 16 are covered by fire from each other, taking block 3 now is the only option. So next, block 3 is cleared with path length 3. Now it is clear that there is no way to conquer the destination, since block 15 is fired by 13 and 16, yet we cannot clear any of them without getting fired. Hence the last block we can reach is 3. steps2.png 代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB 栈限制 8192 KB
最新发布
08-23
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值