feeding time

Feeding Time


Accepted : 16
 
Submit : 148
Time Limit : 2000 MS
 
Memory Limit : 1048576 KB
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.
THE PROBLEM CONTAINS MULTIPLE CASES.KEEP PROCESSING THE INPUT TILL EOF.  
INPUT FORMAT:
* 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 '*'
SAMPLE INPUT (file feedtime.in):
10 8
...*....**
..**....**
...*....**
...**.*.**
***.**.***
...**.*.**
...*.*****
...***..**
OUTPUT FORMAT:
* Line 1: A single integer that represents the maximum number of
        squares of grass that Bessie can eat.
SAMPLE OUTPUT (file feedtime.out):
21

Source

USACO FEB10
널리 퍼진 비귀화...설명하지 않다
#include <stdio.h>
#include <stack>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <string.h>
using namespace std;
char grid[755][755];
int sign[755][755];
int W, H,dx[] = { 1, -1, 0, 0, 1, -1, 1, -1 },dy[] = { 0, 0, 1, -1, 1, 1, -1, -1 };
stack<int> stx,sty;
int main() {
    int x, y, nBest = 0, square,zx,zy,cx,cy;
    int sum;
    while(scanf("%d%d", &W, &H)!=EOF)
    {
    nBest=0;
    memset(sign,0,sizeof(sign));
    for (y = 0; y < H; y++)
        scanf("%s", grid[y]);
    for (y = 0; y < H; y++)
    {
        for (x = 0; x < W; x++)
         {
             square=0;
             if(grid[y][x]!='*'&&sign[y][x]!=1)
             {
                 stx.push(x);
                 sty.push(y);
                 sign[y][x]=1;
                 while(!stx.empty()&&!sty.empty())
                {
                    cx=stx.top();
                    cy=sty.top();
                    stx.pop();
                    sty.pop();
                    square++;
                for (int i = 0; i < 8; i++)
                    {
                    zx=cx+dx[i];
                    zy=cy+dy[i];
                    if (!(zx < 0 || zx >= W || zy < 0 ||zy >= H || grid[zy][zx] == '*'||sign[zy][zx]!=0))
                        {
                        stx.push(zx);
                        sty.push(zy);
                        sign[zy][zx]=1;
                        }
                    }
                }
            }
            if (square > nBest)
                nBest = square;
        }
    }
    printf("%d
", nBest); } return 0; }

좋은 웹페이지 즐겨찾기