FZU 2150 Fire Game

来源:互联网 发布:java实现排序算法 编辑:程序博客网 时间:2024/04/28 16:41
Problem 2150 Fire Game

Accept: 1344    Submit: 4753
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

43 3.#.###.#.3 3.#.#.#.#.3 3...#.#...3 3###..##.#

Sample Output

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

/*这道题真的快把自己写吐了,自己最开始写了快200行的代码,我对FZUoj不太了解,提交了好多次都是CE自己真的快疯了,但是后来看到自己codeblock上面报的错我就知道问题了我将Node{x,y,z},改成了用构造函数写成的Node(x,y,z),然后就过了。而且用visual C++ 改成 GUN C++耗时还变短了,不知道为啥。*/#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 15;int n,m,flag;bool vis[maxn][maxn];char maze[maxn][maxn];int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};struct Node{    int x,y,t;    Node() {}    Node(int a,int b,int c):x(a),y(b),t(c){}};Node node[maxn*maxn];       //用来保存图中草的位置int bfs(int x1, int y1, int x2, int y2){    queue <Node> que;    Node pre;    que.push(Node(x1,y1,0));    que.push(Node(x2,y2,0));    vis[x1][y1] = true;    vis[x2][y2] = true;    int temp = 0;    while(!que.empty())    {        pre = que.front();        que.pop();        temp = max(temp, pre.t);        for(int i = 0; i < 4; i++)        {            int xx = pre.x + dir[i][0];            int yy = pre.y + dir[i][1];            if(xx < 0 || xx >= n || yy < 0 || yy >= m || maze[xx][yy] != '#' || vis[xx][yy])                continue;            vis[xx][yy] = true;            que.push(Node(xx, yy, pre.t+1));        }    }    return temp;}bool check(void){    for (int i = 0; i < flag; i++)    {        int x = node[i].x,y = node[i].y;        if (!vis[x][y]) return false;    }    return true;}int main(void){    int T;    scanf("%d", &T);    for(int kase = 1; kase <= T; kase++)    {        scanf("%d %d", &n, &m);        flag = 0;        for(int i = 0; i < n; i++)        {            scanf("%s", maze[i]);            for(int j = 0; j < m; j++)                if(maze[i][j] == '#')          //保存草的位置                {                    node[flag].x = i;                     node[flag++].y = j;                }        }        if(flag <= 2) printf("Case %d: 0\n", kase); //如果题目中的草少于2个,那么一下子就烧完了,需要的时间为0        else        {            int ans = INF;            for(int i = 0; i < flag; i++)     //枚举所有的草                  for(int j = i+1; j < flag; j++)                {                    memset(vis, false, sizeof(vis));                    int temp = bfs(node[i].x, node[i].y, node[j].x, node[j].y);   //bfs同时加入2个点,开始我用了2遍bfs,发现自己好蠢                    if(check()) ans = min(ans, temp);   //注意判断是不是所有的草都烧完了                }            if(ans < INF) printf("Case %d: %d\n", kase, ans);  //说明还有草没烧完            else printf("Case %d: -1\n", kase);        }    }    return 0;}

0 0
原创粉丝点击