zoukankan      html  css  js  c++  java
  • 洛谷—— P2919 [USACO08NOV]守护农场Guarding the Farm

    https://www.luogu.org/problem/show?pid=2919

    题目描述

    The farm has many hills upon which Farmer John would like to place guards to ensure the safety of his valuable milk-cows.

    He wonders how many guards he will need if he wishes to put one on top of each hill. He has a map supplied as a matrix of integers; the matrix has N (1 < N <= 700) rows and M (1 < M <= 700) columns. Each member of the matrix is an altitude H_ij (0 <= H_ij <= 10,000). Help him determine the number of hilltops on the map.

    A hilltop is one or more adjacent matrix elements of the same value surrounded exclusively by either the edge of the map or elements with a lower (smaller) altitude. Two different elements are adjacent if the magnitude of difference in their X coordinates is no greater than 1 and the magnitude of differences in their Y coordinates is also no greater than 1.

    农场里有许多山丘,在山丘上约翰要设置哨岗来保卫他的价值连城的奶牛.

    约翰不知道有多少山丘,也就不知道要设置多少哨岗.他有一张地图,用整数矩阵的方式描 述了农场N(1 <= N<=700)行M(1 < M<=700)列块土地的海拔高度好 H_ij (0 <= H_ij <= 10,000).请帮他 计算山丘的数量.

    一个山丘是指某一个方格,与之相邻的方格的海拔高度均严格小于它.当然,与它相邻的方 格可以是上下左右的那四个,也可以是对角线上相邻的四个.

    输入输出格式

    输入格式:

    • Line 1: Two space-separated integers: N and M

    • Lines 2..N+1: Line i+1 describes row i of the matrix with M

    space-separated integers: H_ij

    输出格式:

    • Line 1: A single integer that specifies the number of hilltops

    输入输出样例

    输入样例#1:
    8 7 
    4 3 2 2 1 0 1 
    3 3 3 2 1 0 1 
    2 2 2 2 1 0 0 
    2 1 1 1 1 0 0 
    1 1 0 0 0 1 0 
    0 0 0 1 1 1 0 
    0 1 2 2 1 1 0 
    0 1 1 1 2 1 0 
    
    输出样例#1:
    3 
    

    说明

    There are three peaks: The one with height 4 on the left top, one of the points with height 2 at the bottom part, and one of the points with height 1 on the right top corner.

    DFS

    每次从最高点向四周扩展、

     1 #include <algorithm>
     2 #include <cstdio>
     3 
     4 #define max(a,b) (a>b?a:b)
     5 inline void read(int &x)
     6 {
     7     x=0; register char ch=getchar();
     8     for(; ch>'9'||ch<'0'; ) ch=getchar();
     9     for(; ch>='0'&&ch<='9'; ch=getchar()) x=x*10+ch-'0';
    10 }
    11 const int N(705);
    12 int fx[8]={-1,0,1,-1,1,-1,0,1};
    13 int fy[8]={-1,-1,-1,0,0,1,1,1};
    14 int n,m,H,ans,cnt,h[N][N];
    15 struct Pos {
    16     int x,y,h;
    17     bool operator < (const Pos a)const
    18     {
    19         return h>a.h;
    20     }
    21 }pos[N*N];
    22 bool vis[N][N];
    23 void DFS(int x,int y)
    24 {
    25     vis[x][y]=1;
    26     for(int tx,ty,i=0; i<8; ++i)
    27     {
    28         tx=x+fx[i]; ty=y+fy[i];
    29         if(tx<1||ty<1||tx>n||ty>m) continue;
    30         if(!vis[tx][ty]&&h[x][y]>=h[tx][ty]) DFS(tx,ty);
    31     }
    32 }
    33 
    34 int Presist()
    35 {
    36     read(n),read(m);
    37     for(int i=1; i<=n; ++i)
    38       for(int j=1; j<=m; ++j)
    39         read(pos[++cnt].h),pos[cnt].x=i,pos[cnt].y=j,h[i][j]=pos[cnt].h;
    40     std::sort(pos+1,pos+cnt+1);
    41     for(int i=1; i<=cnt; ++i)
    42         if(!vis[pos[i].x][pos[i].y]) DFS(pos[i].x,pos[i].y),ans++;
    43     printf("%d
    ",ans);
    44     return 0;
    45 }
    46 
    47 int Aptal=Presist();
    48 int main(int argc,char*argv[]){;}
    ——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
  • 相关阅读:
    jQuery-1.样式篇---属性与样式
    jQuery-1.样式篇---选择器
    jQuery-1.样式篇
    随机数
    UIButton
    UILabel
    webView
    气泡聊天
    下拉和上拉刷新
    LimitDemo
  • 原文地址:https://www.cnblogs.com/Shy-key/p/7591706.html
Copyright © 2011-2022 走看看