其他分享
首页 > 其他分享> > 守护农场Guarding the Farm

守护农场Guarding the Farm

作者:互联网

题意翻译

农夫John的农场里有很多小山丘,他想要在那里布置一些保镖去保卫他的那些相当值钱的奶牛们。

他想知道如果在一座小山丘上布置一名保镖的话,他最少总共需要招聘多少名保镖。他现在手头有一个用数字矩阵来表示地形的地图。这个矩阵有N行(1<N≤700)和M列( 1<M≤ 700) 。矩阵中的每个元素都有一个值H_ij(0≤H_ij≤10000)来表示该地区的海拔高度。

小山丘的定义是:若地图中一个元素所邻接的所有元素都比这个元素高度要小(或它邻接的是地图的边界),则该元素和其周围所有按照这样顺序排列的元素的集合称为一个小山丘。这里邻接的意义是:若一个位置的横纵坐标与另一个位置的横纵坐标相差不超过1,则称这两个元素邻接,比如某个非边界点的位置有8个相邻点:上、下、左、右、左上、右上、左下、右下。

请你帮助他统计出地图上最少且尽量高的小山丘数量。

题目描述

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.

输入格式

* 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.

 

我们可以把获取的高度从高到底排一下序,然后从高到低依次去扩展,这样的话就省了很多问题;最后统计答案就好了;

 

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#define mod 100000007
#define inf 336860180
#define PI 3.1415926
#define ll long long
using namespace std;
int n,m,ans,H,tot,h[702][702];
struct po{int x,y,h;}a[703*703];
queue<int>qx,qy;
int fx,fy,rx,ry;
bool v[1000][1000];
bool cmp(po xx,po yy){return xx.h>yy.h;}
int X[8]={0,0,1,-1,1,-1,1,-1};
int Y[8]={1,-1,0,0,1,1,-1,-1};
void bfs(int x,int y){
    qx.push(x);qy.push(y);
    v[x][y]=1;
    while(!qx.empty()){
      fx=qx.front();qx.pop();
      fy=qy.front();qy.pop();
      H=h[fx][fy];
      for(int i=0;i<=7;i++){
        rx=X[i]+fx;ry=Y[i]+fy;
        if(rx<1 || rx >n || ry<1 || ry>m){
            continue;
        }
        if(v[rx][ry]){
            continue;
        }
        if(h[rx][ry]<=H){
          qx.push(rx);qy.push(ry);
          v[rx][ry]=1;    
        }
      }    
    }
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            tot++;
            scanf("%d",&a[tot].h);
            h[i][j]=a[tot].h;
            a[tot].x=i;a[tot].y=j;
        }
    }
    sort(a+1,a+tot+1,cmp);
    memset(v,0,sizeof(v));
    for(int i=1;i<=tot;i++){
        int xx=a[i].x,yy=a[i].y;
        if(v[xx][yy]){
            continue;
        }
        bfs(xx,yy);
        ans++;
    }
    printf("%d",ans);
    return 0;
}

 

标签:Farm,qx,元素,Guarding,int,qy,include,守护,define
来源: https://www.cnblogs.com/hrj1/p/11257309.html