H - Fire Game

博客介绍了在N*M棋盘上的草地点火游戏,选择两个草地格子点火,火会向相邻草地蔓延。输入棋盘信息,输出能否烧完所有草地及所需最短时间,若不能则输出 -1 ,并给出了输入输出示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

#include<queue>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int vis[100][100];
char mise[100][100];
typedef struct
{
	int x,y;
	int step;
}N;
N s,e;
N map[100];
int n,m,cas=1,ans;
int d[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
void print()
{
	memset(mise,0,sizeof(mise));
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			cin>>mise[i][j];
}
int judge()
{
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(mise[i][j]=='#'&&vis[i][j]==0)
				return 0;
		}
	}
	return 1;
}
int bfs(int x1,int y1,int x2,int y2)
{
	int ans=0;
	memset(vis,0,sizeof(vis));
	vis[x1][y1]=1;
	vis[x2][y2]=1;
	s.x=x1,s.y=y1;
	e.x=x2,e.y=y2;
	s.step=e.step=0;
     queue<N>q;
	 q.push(s);
	 q.push(e);
	 while(q.size())
	 {
        N now=q.front();
		q.pop();
		N next;
		if(ans<now.step)
			ans=now.step;
		for(int i=0;i<4;i++)
		{
            int a=now.x+d[i][0],b=now.y+d[i][1];
			if(vis[a][b]==0&&a>=1&&a<=n&&b>=1&&b<=m)
			{
				if(mise[a][b]=='#'){
			     next.x=a;
				 next.y=b;
				 next.step=now.step+1;
				 q.push(next);
				 vis[a][b]=1;}
			}
		}
	 }
	 return ans;
}
void solve()
{
	int ans=0x3f3f3f;
	int count=0;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(mise[i][j]=='#')
			{
				map[count].x=i;
				map[count].y=j;
				count++;
			}
		}
	}
	if(count<=2){
		cout<<"Case "<<cas<<": 0"<<endl;
	return ;}
	int temp;
    for(int i=0;i<count;i++)
	{
		for(int j=0;j<count;j++)
		{
			if(i==j) continue;
			 temp=bfs(map[i].x,map[i].y,map[j].x,map[j].y);
			if(judge())
			if(ans>temp)
			ans=temp;
		}
	}
	if(ans==0x3f3f3f)
		cout<<"Case "<<cas<<": -1"<<endl;
	else
		cout<<"Case "<<cas<<": "<<ans<<endl;
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		print();
		solve();
		cas++;
	}
	return 0;
}

#include "public.h" #include "key.h" #include "led_screen.h" u8 plane_pos[8][8]={ {1,1,1,0,0,0,0,0}, {0,1,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,1,0}};//zhu_jue zhuang_tai zuo_biao_wei_zhi yong_yu xian_shi u8 plane_pos_x=0,plane_pos_y=1;//fei_ji zhong_xin wei_zhi u8 hostile_pos_x=7,hostile_pos_y=6;//di_ren chu_shi wei_zhi u8 col_scan[8]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe}; //xiang_shang he xiang_xia shi x bian_hua void move_up(u8 (*pos)[8],u8 *x,u8 *y){ if (*y>1){ pos[*x][*y]=0; pos[(*x)+1][*y]=0; pos[*x][(*y)+1]=0; pos[*x][(*y)-1]=0; (*y)--; pos[*x][*y]=1; pos[(*x)+1][*y]=1; pos[*x][(*y)+1]=1; pos[*x][(*y)-1]=1; } } void move_down(u8 (*pos)[8],u8 *x,u8 *y){ if (*y<6){ pos[*x][*y]=0; pos[(*x)+1][*y]=0; pos[*x][(*y)+1]=0; pos[*x][(*y)-1]=0; (*y)++; pos[*x][*y]=1; pos[(*x)+1][*y]=1; pos[*x][(*y)+1]=1; pos[*x][(*y)-1]=1; } } //xiang_zuo he xiang_you shi y bian_hua void move_left(u8 (*pos)[8],u8 *x,u8 *y){ if (*x>0){ pos[*x][*y]=0; (*x)--; pos[*x][*y]=1; } } /* shu_ju he_bing hang_shu_ju shi an_lie cong_shang_dao_xia jin_xing lie_shu_ju bu_yong he_bing suo_you_de zhu_lie_sao_miao dou_shi yi_yang_de */ u8 pre_595write(u8 (*pos)[8],u8 num){ u8 dat=0; u8 i; for(i=0;i<8;i++){ dat |=(pos[num][7-i]<<i); } return dat; } //di_wo jiao_hu xing_wei han_shu void check_fire_effect(u8 (*pos)[8],u8 *plane_y,u8 *hostile_y,u8 *hostile_x){ if (*plane_y==*hostile_y){// kai_huo you_xiao pos[*hostile_x][*hostile_y]=0;//di_ji bei cui_hui //chong_xin sheng_cheng di_ji *hostile_y=6; *hostile_x=7; } } bool is_game_over(u8 *hostile_x){ if (*hostile_x<2){ return true; } else return false; } 报错User\main.c(83): error C129: missing ';' before 'is_game_over'
最新发布
08-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值