Sicily 2002. Feeding Time

2002. Feeding Time

Constraints

Time Limit: 1 secs, Memory Limit: 292.96875 MB

Description

It's Bessie's feeding time, and Farmer John is trying to decide where to put her. FJ has a farm that comprises W x H (1 <= W <= 750; 1 <= H <= 750) squares and is partitioned into one or more separate pastures by rocks both large and small. Every pasture contains some grass and some rocks. 
Bessie is a hungry little cow and just loves to eat, eat, eat her grass. She can move from any square to any other square that is horizontally, vertically, or diagonally adjacent. Bessie can't cross the rocks because they hurt her feet, and, of course, she can't leave the farm. Bessie wants to know the maximum number of squares of grass that she can eat. 
FJ has a map of his farm, where a '.' represents a square of grass, and a '*' represents a rock. Consider this 10x8 map and a detailed breakdown of the extent of each of its three pastures: 

      ...*....**  |  111*....**   ...*2222**    ...*....**

      ..**....**  |  11**....**   ..**2222**    ..**....**

      ...*....**  |  111*....**   ...*2222**    ...*....**

      ...**.*.**  |  111**.*.**   ...**2*2**    ...**.*.**

      ***.**.***  |  ***1**.***   ***.**2***    ***.**.***

      ...**.*.**  |  111**.*.**   ...**2*2**    ...**.*.**

      ...*.*****  |  111*.*****   ...*2*****    ...*.*****

      ...***..**  |  111***..**   ...***..**    ...***33**

Pasture 1 has 21 squares; pasture 2 has 18 squares; pasture 3 has 2 squares. Thus Bessie should choose pasture 1 with 21 squares to maximize the grass she can eat.

Input

* Line 1: Two space-separated integers: W and H 
* Lines 2..H+1: Line i+1 describes field row i with W characters (and no spaces), each either '.' or '*'

Output

* Line 1: A single integer that represents the maximum number of squares of grass that Bessie can eat.

Sample Input

10 8
...*....**
..**....**
...*....**
...**.*.**
***.**.***
...**.*.**
...*.*****
...***..**

Sample Output

21

Problem Source

2010中山大学新手赛-网络预选赛

队列dfs

#include <queue>
#include <iostream>
#include <stdio.h>
using namespace std;
#define MAX 755

bool map[MAX][MAX];//储存地图
bool vis[MAX][MAX];//储存是否已经访问过,注意刚开始默认是false的,上同
int w, h;

struct point {
    int ii, jj;
};

int dfs(int start_i, int start_j) {
    queue<point> q;//用队列来代替深搜,否则爆栈
    point temp_p, temp_p_last;
    temp_p.ii = start_i;
    temp_p.jj = start_j;
    vis[start_i][start_j] = true;
    q.push(temp_p);
    int counter = 0;
    
    while (!q.empty()) {
        temp_p = q.front();
        q.pop();
        map[temp_p.ii][temp_p.jj] = false;
        counter++;
        //以下是八个方向的搜索
        if (temp_p.ii > 0 && map[temp_p.ii - 1][temp_p.jj] && !vis[temp_p.ii - 1][temp_p.jj]) {
            temp_p_last.ii = temp_p.ii - 1;
            temp_p_last.jj = temp_p.jj;
            q.push(temp_p_last);
            vis[temp_p.ii - 1][temp_p.jj] = true;
        }
        
        if (temp_p.jj > 0 && map[temp_p.ii][temp_p.jj - 1] && !vis[temp_p.ii][temp_p.jj - 1]) {
            temp_p_last.ii = temp_p.ii;
            temp_p_last.jj = temp_p.jj - 1;
            q.push(temp_p_last);
            vis[temp_p.ii][temp_p.jj - 1] = true;
        }
        
        if (temp_p.ii < h - 1 && map[temp_p.ii + 1][temp_p.jj] && !vis[temp_p.ii + 1][temp_p.jj]) {
            temp_p_last.ii = temp_p.ii + 1;
            temp_p_last.jj = temp_p.jj;
            q.push(temp_p_last);
            vis[temp_p.ii + 1][temp_p.jj] = true;
        }
        
        if (temp_p.jj < w - 1 && map[temp_p.ii][temp_p.jj + 1] && !vis[temp_p.ii][temp_p.jj + 1]) {
            temp_p_last.ii = temp_p.ii;
            temp_p_last.jj = temp_p.jj + 1;
            q.push(temp_p_last);
            vis[temp_p.ii][temp_p.jj + 1] = true;
        }
        
        if (temp_p.ii > 0 && temp_p.jj > 0 && map[temp_p.ii - 1][temp_p.jj - 1] && !vis[temp_p.ii - 1][temp_p.jj - 1]) {
            temp_p_last.ii = temp_p.ii - 1;
            temp_p_last.jj = temp_p.jj - 1;
            q.push(temp_p_last);
            vis[temp_p.ii - 1][temp_p.jj - 1] = true;
        }
        
        if (temp_p.ii > 0 && temp_p.jj < w - 1 && map[temp_p.ii - 1][temp_p.jj + 1] && !vis[temp_p.ii - 1][temp_p.jj + 1]) {
            temp_p_last.ii = temp_p.ii - 1;
            temp_p_last.jj = temp_p.jj + 1;
            q.push(temp_p_last);
            vis[temp_p.ii - 1][temp_p.jj + 1] = true;
        }
        
        if (temp_p.ii < h - 1 && temp_p.jj < w - 1 && map[temp_p.ii + 1][temp_p.jj + 1] && !vis[temp_p.ii + 1][temp_p.jj + 1]) {
            temp_p_last.ii = temp_p.ii + 1;
            temp_p_last.jj = temp_p.jj + 1;
            q.push(temp_p_last);
            vis[temp_p.ii + 1][temp_p.jj + 1] = true;
        }
        
        if (temp_p.ii < h - 1 && temp_p.jj > 0 && map[temp_p.ii + 1][temp_p.jj - 1] && !vis[temp_p.ii + 1][temp_p.jj - 1]) {
            temp_p_last.ii = temp_p.ii + 1;
            temp_p_last.jj = temp_p.jj - 1;
            q.push(temp_p_last);
            vis[temp_p.ii + 1][temp_p.jj - 1] = true;
        }
    }
    
    return counter;
}
    
int main() {
    int i, j, max, temp_num;
    char temp[755];
    scanf("%d%d\n", &w, &h);
    for (i = 0; i < h; i++) {
        gets(temp);
        for (j = 0; j < w; j++) {
            if (temp[j] == '.') {
                map[i][j] = true;
            }
        }
    }
    for (i = 0, max = 0; i < h; i++) {
        for (j = 0; j < w; j++) {
            if (map[i][j]) {
                temp_num = dfs(i, j);
                max = temp_num > max ? temp_num : max;
            }
        }
    }
    printf("%d\n", max);
    return 0;
}           

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值