feeding time

来源:互联网 发布:如何作曲编曲软件 编辑:程序博客网 时间:2024/04/30 08:18

Feeding Time

Accepted : 16 Submit : 148Time Limit : 2000 MS Memory Limit : 1048576 KB
It's Bessie's feeding time, and Farmer John is trying to decidewhere 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 moreseparate pastures by rocks both large and small. Every pasturecontains some grass and some rocks.Bessie is a hungry little cow and just loves to eat, eat, eat hergrass. She can move from any square to any other square that ishorizontally, vertically, or diagonally adjacent. Bessie can't crossthe rocks because they hurt her feet, and, of course, she can'tleave the farm. Bessie wants to know the maximum number of squaresof 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 detailedbreakdown 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 has2 squares. Thus Bessie should choose pasture 1 with 21 squares tomaximize 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\n", nBest);    }    return 0;}


原创粉丝点击