Sicily 2002. Feeding Time 解题报告

本文介绍了一种解决Bessie在限定区域内寻找最大草地面积的问题。通过使用队列实现宽度优先搜索,避免了递归导致的栈溢出问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

Description

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.

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

21

 

纠结了很久的一道题。以前用了一个比较简单的递归实现宽度优先搜索,总是爆栈RUNTIME ERROR,无奈今天上网找了一个用队列的实现,而且改为循环,终于解决了爆栈问题。

思路也比较简单,将farm以string数组(二维数组)形式读入,然后遍历一遍所有的点,如果是草地的则寻找该点附近连通并且是草地的点。注意查找过的点要置为岩石防止重复。

 

 

 

 1 #include <iostream>
 2 #include <queue>
 3 using namespace std;
 4 
 5 #define MAX 750
 6 
 7 struct Point{
 8     int x,y;
 9     Point(int i,int j){
10         x=i;
11         y=j;
12     }
13 };
14 
15 void findAnArea(int &grass,queue<Point>connection,string farmMap[MAX+2]);
16 
17 int main(){
18     queue<Point>connection;//用来压进某一点开始连通的点
19     int width,height,max=0,grass;
20     string farmMap[MAX+2];//加上上下左右4行防止搜寻临近区域时越界
21     cin>>width>>height;
22     farmMap[0]=string(width+2,'*'); //地图初始化
23     farmMap[MAX+1]=string(width+2,'*');
24     for(int i=1;i<=height;i++){
25         cin>>farmMap[i];
26         farmMap[i]="*"+farmMap[i]+"*";
27     }
28     for(int i=1;i<=height;i++){
29         for(int j=1;j<=width;j++){
30             if(farmMap[i][j]=='.'){
31                 grass=1;
32                 farmMap[i][j]='*';//将该点草地置为岩石后搜寻附近的区域
33                 connection.push(Point(i,j));
34                 findAnArea(grass,connection,farmMap);
35                 if(grass>max)
36                     max=grass;
37             }
38         }
39     }
40     cout<<max<<endl;
41     return 0;
42 }
43 void findAnArea(int &grass,queue<Point>connection,string farmMap[MAX+2]){
44     int i,j;
45     while(!connection.empty()){
46         i=connection.front().x;
47         j=connection.front().y;
48         connection.pop();
49         if(farmMap[i-1][j-1]=='.'){
50             farmMap[i-1][j-1]='*';
51             connection.push(Point(i-1,j-1));
52             grass++;
53         }
54         if(farmMap[i-1][j]=='.'){
55             farmMap[i-1][j]='*';
56             connection.push(Point(i-1,j));
57             grass++;
58         }
59         if(farmMap[i-1][j+1]=='.'){
60             farmMap[i-1][j+1]='*';
61             connection.push(Point(i-1,j+1));
62             grass++;
63         }
64         if(farmMap[i][j-1]=='.'){
65             farmMap[i][j-1]='*';
66             connection.push(Point(i,j-1));
67             grass++;
68         }
69         if(farmMap[i][j+1]=='.'){
70             farmMap[i][j+1]='*';
71             connection.push(Point(i,j+1));
72             grass++;
73         }
74         if(farmMap[i+1][j-1]=='.'){
75             farmMap[i+1][j-1]='*';
76             connection.push(Point(i+1,j-1));
77             grass++;
78         }
79         if(farmMap[i+1][j]=='.'){
80             farmMap[i+1][j]='*';
81             connection.push(Point(i+1,j));
82             grass++;
83         }
84         if(farmMap[i+1][j+1]=='.'){
85             farmMap[i+1][j+1]='*';
86             connection.push(Point(i+1,j+1));
87             grass++;
88         }
89     }
90 }

 

 

 

 

 

转载于:https://www.cnblogs.com/jolin123/p/3326186.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值