zoukankan      html  css  js  c++  java
  • Sicily 2002. Feeding Time (广搜) 解题报告

    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
    解题报告:
      这是一道典型的搜索题,可以使用广搜实现。题目意思大致就是找出一片相邻的最大面积。使用广搜基本方法即可,无需任何处理技巧。
    代码如下:
    1 #include<iostream>
    2 #include<queue>
    3 using namespace std;
    4 const int MAX=760;
    5
    6 struct node
    7 {
    8 int x,y;
    9 char gr;
    10 };
    11
    12 int counts;
    13 node nod[MAX][MAX];
    14 int visit[MAX][MAX];
    15 int bfs(int a,int b)
    16 {
    17 queue<node>que;
    18 node nod2;
    19 int i,j;
    20 que.push(nod[a][b]);
    21 visit[a][b]=1;
    22 counts++;
    23 while(!que.empty())
    24 {
    25 nod2=que.front();
    26 que.pop();
    27 i=nod2.x;
    28 j=nod2.y;
    29 if(nod[i-1][j].gr=='.' && visit[i-1][j]==0)
    30 {
    31 que.push(nod[i-1][j]);
    32 visit[i-1][j]=1;
    33 counts++;
    34 }
    35 if(nod[i+1][j].gr=='.' && visit[i+1][j]==0)
    36 {
    37 que.push(nod[i+1][j]);
    38 visit[i+1][j]=1;
    39 counts++;
    40 }
    41 if(nod[i][j-1].gr=='.' && visit[i][j-1]==0)
    42 {
    43 que.push(nod[i][j-1]);
    44 visit[i][j-1]=1;
    45 counts++;
    46 }
    47 if(nod[i][j+1].gr=='.' && visit[i][j+1]==0)
    48 {
    49 que.push(nod[i][j+1]);
    50 visit[i][j+1]=1;
    51 counts++;
    52 }
    53 if(nod[i-1][j-1].gr=='.' && visit[i-1][j-1]==0)
    54 {
    55 que.push(nod[i-1][j-1]);
    56 visit[i-1][j-1]=1;
    57 counts++;
    58 }
    59 if(nod[i+1][j+1].gr=='.' && visit[i+1][j+1]==0)
    60 {
    61 que.push(nod[i+1][j+1]);
    62 visit[i+1][j+1]=1;
    63 counts++;
    64 }
    65 if(nod[i-1][j+1].gr=='.' && visit[i-1][j+1]==0)
    66 {
    67 que.push(nod[i-1][j+1]);
    68 visit[i-1][j+1]=1;
    69 counts++;
    70 }
    71 if(nod[i+1][j-1].gr=='.' && visit[i+1][j-1]==0)
    72 {
    73 que.push(nod[i+1][j-1]);
    74 visit[i+1][j-1]=1;
    75 counts++;
    76 }
    77 }
    78 return counts;
    79 }
    80
    81 int main()
    82 {
    83 int i,j;
    84 int w,h;
    85 int large=0;
    86 cin>>w>>h;
    87 for(i=1;i<=h;i++)
    88 for(j=1;j<=w;j++)
    89 {
    90 cin>>nod[i][j].gr;
    91 nod[i][j].x=i;
    92 nod[i][j].y=j;
    93 visit[i][j]=0;
    94 }
    95 for(i=1;i<=h;i++)
    96 for(j=1;j<=w;j++)
    97 {
    98 if(nod[i][j].gr=='.' && visit[i][j]==0)
    99 {
    100 counts=0;
    101 counts=bfs(i,j);
    102 if(counts>large)large=counts;
    103 }
    104 }
    105 cout<<large<<endl;
    106 return 0;
    107 }
  • 相关阅读:
    DateTime类型的一个Bug
    无痛苦的软件维护——被遗忘的需求
    完全命令行.NET开发
    无痛苦的软件维护——文档和代码
    .NET初学者架构设计指南(一)Hello world的时代
    NGOSS的一点简单概念
    软件的逻辑层次
    VSTS for Testers学习笔记目录
    How Google Tests Software (出书,停止更新)
    推荐——《浪潮之巅》(据传稍后会出书,停止更新)
  • 原文地址:https://www.cnblogs.com/lvye/p/1995668.html
Copyright © 2011-2022 走看看