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;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
java에서 InputStream, String, File 간의 상호 전환 비교InputStream, String, File 상호 전환 1. String --> InputStream 2. InputStream --> String 오늘 인터넷에서 또 다른 방법을 보았는데, 특별히 가지고 와서 공...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.