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 }
  • 相关阅读:
    域用户组策略禁用QQ等软件
    关于使用IDE制作样式表后不能正常显示的问题
    asp.net中使用ckfinder2选择图片并返回图片文件的路径的代码
    按规定长度显示指定绑定字段的内容
    ckeeditor和ckfinder在asp.net中的使用
    开发播放器中所学/用到的知识
    用户体验的网站首页设计的准则
    0410_gdgrid_checkbox_自己强加的多选,选中,批量删除
    0405_hxtx_主题游常用联系人功能代码实现
    0401flag
  • 原文地址:https://www.cnblogs.com/lvye/p/1995668.html
Copyright © 2011-2022 走看看