• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

FOJ Problem 2150 Fire Game

Problem 2150 Fire Game

Accept: 22    Submit: 46 Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem 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
 1 #pragma comment(linker,"/STACK:102400000,102400000")
 2 #include <cstdio>
 3 #include <vector>
 4 #include <cmath>
 5 #include <queue>
 6 #include <cstring>
 7 #include <iostream>
 8 #include <algorithm>
 9 using namespace std;
10 #define INF 0x7fffffff
11 #define mod 1000000007
12 #define ll long long
13 #define maxn 1025
14 #define pi acos(-1.0)
15 int n, m, x,y,now, all, v,ans,step;
16 int t;
17 char s[11][11];
18 int vis[11][11];
19 int dx [] = { 1, 0, -1, 0 };
20 int dy [] = { 0, 1, 0, -1 };
21 struct point{
22     int x, y, k;
23     point(int x = 0, int y = 0,int k=0) :x(x), y(y),k(k){}
24 };
25 int ta[maxn], tb[maxn];
26 int bfs(point a, point b){
27     step = 0;
28     queue<point>q;
29     q.push(a); q.push(b);
30     while (!q.empty()){
31         point t = q.front(); q.pop();
32         if (vis[t.x][t.y])continue;
33         vis[t.x][t.y] = 1;
34         step = max(step, t.k);
35         for (int i = 0; i < 4; i++){
36             int nx = t.x + dx[i];
37             int ny = t.y + dy[i];
38             if (!vis[nx][ny]&&s[nx][ny]=='#'&&nx>=0&&ny>=0&&nx<n&&ny<m){
39                 q.push(point(nx, ny,t.k+1));
40             }
41         }
42     }
43     return step;
44 }
45 int main(){
46     int cas = 1;
47     scanf("%d", &t);
48     while (t--){
49         scanf("%d%d", &n, &m);
50         memset(vis, 0, sizeof vis);
51         int k = 0;
52         for (int i = 0; i < n; i++)scanf("%s", s[i]);
53         for (int i = 0; i < n; i++)
54         for (int j = 0; j < m; j++)
55             if (s[i][j] == '#')ta[k] = i, tb[k++] = j;
56         printf("Case %d: ", cas++);
57         ans = INF;
58         for (int i = 0; i < k;i++)
59         for (int j = i; j < k; j++){
60             memset(vis, 0, sizeof vis);
61             int step=bfs(point(ta[i], tb[i], 0), point(ta[j], tb[j], 0));
62             int flag = 1;
63             for (int x = 0; x < n; x++)
64             for (int y = 0; y < m; y++)
65                 if (s[x][y] == '#'&&!vis[x][y])flag = 0, x = n, y = m;
66             if (flag)ans = min(ans,step);
67         }
68         if (ans >= INF)printf("-1\n");
69         else printf("%d\n", ans);
70     }
71     return 0;
72 }
View Code
posted @ 2013-12-23 06:49  HaibaraAi  阅读(252)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3