【简单搜索】Fire Game

Fire Game

问题描述

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代码中,不过是在记录初始状态时入队了两个结点,其他无异。
最开始的想法是先判断有几个连通子图

  • 有两个以上的连通子图,直接GG
  • 两个的话,分开广搜
  • 一个的话,取子图中任意两个起点进行广搜,记录其中最小的步数

后来看了看其他人的代码,发现直接使用最后一种情况的处理方案即可。
我们可以舍弃一些开销(事实上使用上述的方法开销还会更大),不对bfs进行修改,而是取整个草地的任意两个起点进行广搜,取其中的最小步数者并且先要判断在这种遍历中是否所有有草的地方都被访问过了。
有一个很巧妙的地方。
在有两个连通子图的情况中,可行的方案是两个起点分别位于两个子图里,而最短步数应该是两个子图的最短步数中的较长者。一般来说,我们要区分这两个子图的遍历并且要对它们的最短步数进行比较。但是bfs巧就巧在,它回避了这种分开判断。先上这部分的代码吧。

int bfs(node p, node q){
 	vist[p.x][p.y]=vist[q.x][q.y]=1;
 	queue<node> Q;
 	Q.push(p), Q.push(q);
 	while(Q.size()){
 		p=Q.front(), Q.pop();
 		for(int i=0; i<4; i++){
 			q=p;
 			q.x+=direction[i][0];
 			q.y+=direction[i][1];
 			if(check(q.x, q.y)) continue;
 			vist[q.x][q.y]=1;
 			q.step++;
 			Q.push(q);
		 }
	 }
	 return q.step;	//or return p.step
 }

我们可以看到,退出while循环的条件为队列为空。想一想,在有两个子图的情况中,最后一个出队的元素会是属于哪个子图的呢?是的,它属于那个需要步数更长的子图。我们只要返回到达该元素的步数即为本方案所需的步数。很好的回避了上面提到的问题。
其他就没有什么了。

代码样例

//I-Fire Game
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int n, m;
char map[15][15], vist[15][15];
int direction[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
 struct node{
 	int x, y, step;
 }grids[105];
 int isvist(){
 	for(int i=0; i<n; i++){
	 	for(int j=0; j<m; j++){
	 		if(map[i][j]=='#'&&!vist[i][j]){
	 			return 0;
			 }
		 }
	 }
	 return 1;
 }
 int check(int _x, int _y){
 	if(_x<0||_x>=n||_y<0||_y>=m||vist[_x][_y]||map[_x][_y]=='.'){
 		return 1;
	 }
	 return 0;
 }
 int bfs(node p, node q){
 	vist[p.x][p.y]=vist[q.x][q.y]=1;
 	queue<node> Q;
 	Q.push(p), Q.push(q);
 	while(Q.size()){
 		p=Q.front(), Q.pop();
 		for(int i=0; i<4; i++){
 			q=p;
 			q.x+=direction[i][0];
 			q.y+=direction[i][1];
 			if(check(q.x, q.y)) continue;
 			vist[q.x][q.y]=1;
 			q.step++;
 			Q.push(q);
		 }
	 }
	 return q.step;	//or return p.step
 }
 int main(void){
 	int T;
 	cin>>T;
 	for(int t=0; t<T; t++){
 		int index=0, ans=0x3f3f3f3f;
 		cin>>n>>m;
 		for(int i=0; i<n; i++){
 			cin>>map[i];
 			for(int j=0; j<m; j++){
 				if(map[i][j]=='#'){
 					grids[index].x=i;
					grids[index].y=j;
 					grids[index].step=0;
 					index++;
				 }
			 }
		 }
		for(int i=0; i<index; i++){
			for(int j=i; j<index; j++){
				memset(vist, 0, sizeof(vist));
				int counter=bfs(grids[i], grids[j]);
				if(counter<ans&&isvist()){
					ans=counter;
				}
			}
		}
		if(ans==0x3f3f3f3f){
			cout<<"Case "<<t+1<<": -1"<<endl;
		}else{
			cout<<"Case "<<t+1<<": "<<ans<<endl;
		}
	 }
	 return 0;
 }
  • 1605训练计划第九天任务
  • 按时完成
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值