2002. Feeding Time Constraints
Time limit:1 secs, Memory limit:292.96875 MB
Description
It's Bessie ' s feeding time, and Farmer John was trying to decide where to put her. FJ has a farm, comprises W x H (1 <= w <=, 1 <= H <=) squares and is partitioned to 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 she grass. She can move from all square to any other square, which 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 in grass that she can eat.
FJ has a map of the 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 all 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 squares; Pasture 2 has squares; Pasture 3 has 2 squares. Thus Bessie should choose pasture 1 with + 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
21st
Problem Source
2010 Sun Yat-sen University Novice tournament-Network qualifiers
Queue Dfs
#include <queue> #include <iostream> #include <stdio.h>using namespace std; #define MAX 755bool Map[max ][max];//Storage map BOOL vis[max][max];//Storage has been accessed, note that the default is initially false, on the same int w, h;struct point {int II, JJ;}; int dfs (int start_i, int start_j) {queue<point> q;//use queue instead of deep search, otherwise burst 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++; The following is a search in eight directions 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.J J]) {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] &&A MP;!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] &am p;&!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] &&A MP;!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