ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

FZU-2150 Fire Game(BFS)

2021-03-06 02:02:49  阅读:200  来源: 互联网

标签:fire grass Fire BFS int Game grid board include


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

这个题一开始想麻烦了,一开始想的是先判断连通块的个数,再在每一个连通块里寻找最长链,于是去想了很久的最长链求
法,然后发现如果是一坨都是草的话就会出问题,看了一眼标程,竟是最朴素的暴力算法,然后去查了一下原来1s可以运行
的次数是5*1e8次,是我孤陋寡闻了一直以为是千万级。
像这种有关最短路问题的搜索一般都是用bfs去做。
程序为了避免卡常还是做了一点操作的qwq
 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <queue>
 6 #include <stack>
 7 #include <vector>
 8 #include <iostream>
 9 #include "algorithm"
10 using namespace std;
11 const int MAX=15;
12 int t,cas,n,m,ans;
13 char s[MAX][MAX];
14 bool vis[MAX][MAX];
15 int dx[]={1,0,-1,0};
16 int dy[]={0,1,0,-1};
17 struct Poi{
18     int x,y,ste;
19 };
20 queue <Poi> q;
21 inline int mx(int x,int y){return x>y?x:y;}
22 inline int mn(int x,int y){return x<y?x:y;}
23 void get_pos(int nu,int *x,int *y){
24     *x=nu/m+1;if (nu%m==0) (*x)--;
25     *y=nu%m;if (nu%m==0) (*y)=m;
26 }
27 void bfs(int cx,int cy,int vx,int vy){
28     int i,j,cc,vv,an=0;Poi a,b;
29     while (!q.empty()) q.pop();
30     memset(vis,false,sizeof(vis));
31     a.x=cx;a.y=cy,a.ste=0;
32     b.x=vx;b.y=vy,b.ste=0;
33     q.push(a);q.push(b);
34     vis[cx][cy]=vis[vx][vy]=true;
35     while (!q.empty()){
36         a=q.front();q.pop();an=mx(an,a.ste);
37         for (i=0;i<4;i++){
38             cc=a.x+dx[i];
39             vv=a.y+dy[i];
40             if (s[cc][vv]=='#' && !vis[cc][vv]){
41                 b.x=cc,b.y=vv,b.ste=a.ste+1;
42                 vis[cc][vv]=true;
43                 q.push(b);
44             }
45         }
46     }
47     for (i=1;i<=n;i++)
48         for (j=1;j<=m;j++)
49             if (s[i][j]=='#' && !vis[i][j])
50                 return;
51     ans=mn(ans,an);
52 }
53 int main(){
54     freopen ("fire.in","r",stdin);
55     freopen ("fire.out","w",stdout);
56     int i,j,cx,cy,vx,vy;
57     scanf("%d",&t);
58     while (t--){
59         scanf("%d%d\n",&n,&m);
60         ans=1e8;
61         for (i=1;i<=n;i++) scanf("%s",s[i]+1);
62         for (i=1;i<=n*m;i++){
63             get_pos(i,&cx,&cy);
64             if (s[cx][cy]=='#')
65                 for (j=i+1;j<=n*m;j++){
66                     get_pos(j,&vx,&vy);
67                     if (s[vx][vy]=='#')
68                         bfs(cx,cy,vx,vy);
69                 }
70         }
71         printf("Case %d: ",++cas);
72         if (ans==1e8) printf("-1\n");
73         else printf("%d\n",ans);
74     }
75     return 0;
76 }

 


标签:fire,grass,Fire,BFS,int,Game,grid,board,include
来源: https://www.cnblogs.com/keximeiruguo/p/14489276.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有