1. 程式人生 > >FZU 2150 Fire Game (高姿勢bfs--兩個起點)

FZU 2150 Fire Game (高姿勢bfs--兩個起點)

con 暴力 roc 大於 efi while 否則 back sample

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

4
3 3
.#.
###
.#.
3 3
.#.
#.#
.#.
3 3
...
#.#
...
3 3
###
..#
#.#

Sample Output

Case 1: 1
Case 2: -1
Case 3: 0
Case 4: 2

給你塊地,有空地,也有草堆,讓你選兩個草堆進行點火,燃燒的草堆會引燃上下左右的相鄰草堆,每一次引燃花費1s時間,問你最少花多長時間把草堆都點著,如果做不到輸出-1.
這個題一開始姿勢不對,想錯了,先bfs下找連通塊,如果連通塊個數大於3直接GG,否則再在已知連通塊內求個深度........283行代碼直接掛掉了。
然而正確思路是醬紫的:及時有只一個連通塊,我們也可以選擇兩個點火點來減少時間。
所以直接暴力枚舉每兩個草堆,把這兩個點加入bfs隊列,兩起點bfs,看看此時能點多少點多少的bfs的時間(就是q中最後一個被pop出的元素的depth),再判斷下選這兩個點是否能把草堆全點著。
PS:這題最後半小時不過是因為沒有初始化,以後養成好習慣每次都在每個測例運行前加一行init()......
代碼如下:
  1 #include <iostream>
  2 #include <algorithm>
  3 #include <string>
  4 #include <cstring>
  5 #include <cmath>
  6 #include <cstdio>
  7 #include <queue>
  8 #include <vector>
  9 using namespace std;
 10 #define inf 0x3f3f3f3f
 11 int n,m;
 12 bool vis[15][15];
 13 char grid[15][15];
 14 int casee=0;
 15 int ans=inf;
 16 struct node
 17 {
 18     int x,y,depth;
 19 };
 20 vector <node>grass;
 21 bool check (int x,int y)
 22 {
 23     if (!vis[x][y]&&grid[x][y]==‘#‘&&x>=0&&x<n&&y>=0&&y<m)
 24     return true;
 25     else
 26     return false;
 27 }
 28 bool judge ()
 29 {
 30     for (int i=0;i<n;++i){
 31         for (int j=0;j<m;++j){
 32             if (grid[i][j]==‘#‘&&!vis[i][j])
 33             return false;
 34         }
 35     }
 36     return true;
 37 }
 38 void init()
 39 {
 40     grass.clear();
 41     memset(vis,false,sizeof vis);
 42 }
 43 int bfs (node n1,node n2)
 44 {
 45     queue <node> q;
 46     memset(vis,false,sizeof vis);
 47     while (!q.empty()) q.pop();
 48     q.push(n1);
 49     q.push(n2);
 50     int depthest=0;
 51     while (!q.empty())
 52     {
 53         node now=q.front();
 54         q.pop();
 55         if (vis[now.x][now.y])
 56         continue;
 57         vis[now.x][now.y]=true;
 58         depthest=now.depth;
 59         if (check(now.x-1,now.y))
 60         {
 61             node nxt=now;
 62             nxt.x--;
 63             nxt.depth++;
 64             q.push(nxt);
 65         }
 66         if (check(now.x+1,now.y))
 67         {
 68             node nxt=now;
 69             nxt.x++;
 70             nxt.depth++;
 71             q.push(nxt);
 72         }
 73         if (check(now.x,now.y-1))
 74         {
 75             node nxt=now;
 76             nxt.y--;
 77             nxt.depth++;
 78             q.push(nxt);
 79         }
 80         if (check(now.x,now.y+1))
 81         {
 82             node nxt=now;
 83             nxt.y++;
 84             nxt.depth++;
 85             q.push(nxt);
 86         }
 87     }
 88     return depthest;
 89 }
 90 int main()
 91 {
 92     //freopen("de.txt","r",stdin);
 93     int t;
 94     scanf("%d",&t);
 95     while (t--)
 96     {
 97         init();
 98         ans=inf;
 99         scanf("%d%d",&n,&m);
100         for (int i=0;i<n;++i)
101         scanf("%s",grid[i]);
102         for (int i=0;i<n;++i){
103             for (int j=0;j<m;++j){
104                 if (grid[i][j]==‘#‘){
105                     node g;
106                     g.x=i;
107                     g.y=j;
108                     g.depth=0;
109                     grass.push_back(g);
110                 }
111             }
112         }
113         for (int i=0;i<grass.size();++i)
114         {
115             for (int j=i;j<grass.size();++j)
116             {
117                 grass[i].depth=0;
118                 grass[j].depth=0;
119                 int temp=min(bfs(grass[i],grass[j]),ans);
120                 if (judge())
121                 ans=min(ans,temp);
122             }
123         }
124         printf("Case %d: ",++casee);
125         if (ans==inf)
126         printf("-1\n");
127         else
128         printf("%d\n",ans);
129     }
130     return 0;
131 }

FZU 2150 Fire Game (高姿勢bfs--兩個起點)