Sicily 2002. Feeding Time

2002. Feeding TimeConstraints

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 10×8 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 Input10 8…*….**..**….**…*….**…**.*.*****.**.***…**.*.**…*.*****…***..**Sample Output21Problem Source

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

队列dfs

#include <queue>#include <iostream>#include <stdio.h>using namespace std;#define MAX 755bool 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;}

别小看任何人,越不起眼的人。往往会做些让人想不到的事。

Sicily 2002. Feeding Time

相关文章:

你感兴趣的文章:

标签云: