FZU 2150Fire game-BFS

K - Fire Game
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
题目

题目大意:有一块空地上有些草堆‘#’,两个小伙伴想要在这片地上玩耍,然后就要烧掉这些草堆,相邻的草堆可以相互引燃,两个人可以分别选择一块草堆点燃,问说最少需要多少时间才能烧光草堆(每人只能点一次)


解题思路:枚举两个位置同时做为起点,然后BFS,注意有一个坑点就是说草堆的个数少于2的时候,枚举不到两个起点,但是是可以烧光草堆的。

#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>

using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 20;
const int dir[4][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0}};

struct point {
	int x, y;
	point() {}
	point(int a, int b) { x = a; y = b; }
};
int l, r, d[N][N], tmp;
char g[N][N];

void init() {
	memset(g, 0, sizeof(g));
	tmp = 0;

	scanf("%d%d%*c", &r, &l);
	
	for (int i = 0; i < r; i++) {
		gets(g[i]);
		for (int j = 0; j < l; j++) if (g[i][j] == '#') tmp++;
	}
}

int bfs(int x1, int y1, int x2, int y2) {
	memset(d, INF, sizeof(d));

	d[x1][y1] = d[x2][y2] = 0;

	point k, c;

	queue<point> q;
	q.push(point(x1, y1));
	q.push(point(x2, y2));

	while ( !q.empty() ) {
		k = q.front(); q.pop();

		for (int i = 0; i < 4; i++) {
			c.x = k.x + dir[i][0]; c.y = k.y + dir[i][1];
			if (c.x < 0 || c.x >= r || c.y < 0 || c.y >= l) continue;
			if (g[c.x][c.y] != '#') continue;

			if (d[c.x][c.y] > d[k.x][k.y] + 1) {
				d[c.x][c.y] = d[k.x][k.y] + 1;
				q.push(c);
			}
		}
	}

	int ans = 0;
	for (int i = 0; i < r; i++) {
		for (int j = 0; j < l; j++) if (g[i][j] == '#') {
			ans = max(ans, d[i][j]);
		}
	}
	return ans;
}


int solve() {
	if (tmp <= 2) return 0;
	int ans = INF;
	for (int i = 0; i < r; i++) {
		for (int j = 0; j < l; j++) if (g[i][j] == '#') {
			for (int k = 0; k < r; k++) {
				for (int t = 0; t < l; t++) {
					if (k == i && t <= j) continue;
					if (g[k][t] == '#') 
						ans = min(ans, bfs(i, j, k, t));
				}
			}
		}
	}
	return ans == INF ? -1 : ans;
}

int main () {
	int cas;
	scanf("%d", &cas);
	for (int i = 1; i <= cas; i++) {
		init();
		printf("Case %d: %d\n", i, solve());
	}
	return 0;
}

一般比较喜欢的做题风格
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#define Mod 1000000007
using namespace std;

struct Point
{
    int x,y;
    int step;
}S;
char mp[13][13];
int vis[13][13];
int n,m;
int lef;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
queue<Point> que;

bool OK(int nx,int ny)
{
    if(nx < n && nx >= 0 && ny < m && ny >= 0)
        return true;
    return false;
}

int BFS()
{
    int maxi = 0;
    while(!que.empty())  //同步搜索
    {
        int SIZE = que.size();
        while(SIZE--)
        {
            Point tmp = que.front();
            que.pop();
            int nx = tmp.x;
            int ny = tmp.y;
            int step = tmp.step;
            maxi = max(maxi,step);
            Point now;
            for(int k=0;k<4;k++)
            {
                int kx = nx + dx[k];
                int ky = ny + dy[k];
                if(!OK(kx,ky) || vis[kx][ky])
                    continue;
                if(mp[kx][ky] == '#')
                {
                    vis[kx][ky] = 1;
                    now.x = kx;
                    now.y = ky;
                    now.step = step+1;
                    que.push(now);
                    lef--;
                }
            }
        }
    }
    if(lef == 0)
        return maxi;
    else
        return -1;
}

int main()
{
    int t,cs = 1,i,j,k,h;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        int cnt = 0;
        for(i=0;i<n;i++)
        {
            scanf("%s",mp[i]);
            for(j=0;j<m;j++)
                if(mp[i][j] == '#')
                    cnt++;
        }
        int flag = 0;
        int tag = Mod;
        for(i=0;i<n;i++)   //第一个点
        {
            for(j=0;j<m;j++)
            {
                if(mp[i][j] == '#')
                {
                    for(k=i;k<n;k++)   //第二个点
                    {
                        for(h=(k==i?j:0);h<m;h++)
                        {
                            if(mp[k][h] == '#')
                            {
                                memset(vis,0,sizeof(vis));
                                S.x = i,S.y = j,S.step = 0;
                                que.push(S);
                                lef = cnt-1;
                                vis[i][j] = 1;
                                if(!(i==k&&j==h))
                                {
                                    S.x = k,S.y = h,S.step = 0;
                                    que.push(S);
                                    vis[k][h] = 1;
                                    lef--;
                                }
                                int res = BFS();
                                if(res != -1)
                                {
                                    flag = 1;
                                    tag = min(tag,res);
                                }
                            }
                        }
                    }
                }
            }
        }
        if(flag)
            printf("Case %d: %d\n",cs++,tag);
        else
            printf("Case %d: -1\n",cs++);
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值