feeding time

4 篇文章 0 订阅

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\n", nBest);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值