Guarding the Farm Guardian Farm

Translation of the meaning of problems

Farmer John's farm, there are many small hill, where he wanted to arrange some of his bodyguards to defend those very valuable cows.

He wanted to know if arranged on a small hill, then a bodyguard, he had at least a total of how many bodyguards recruitment. He is now on hand to represent a digital terrain map matrix. The matrix has N rows (1 <N≤700) and M columns (1 <M≤ 700). Matrix each element has a value H_ij (0≤H_ij≤10000) to represent the altitude of the region.

Small hill is defined: if all of the map elements adjacent to the element a height smaller than that element (or adjacent to it is the boundary map), and the elements therearound according to said set of all elements arranged in this order a small hill. Here adjacent sense: if the horizontal and vertical and horizontal and vertical coordinates of a position to another position coordinates differ by no more than 1, called two adjacent elements, such as the location of a boundary point 8 non-adjacent points: on up, down, left, right, upper left, upper right, lower left, lower right.

Please help him and at least as high as the statistics of the number of small hill on the map.

Title Description

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.

Input Format

* 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

Output Format

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

Sample input and output

Input # 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 
Output # 1
3 

Description / Tips

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.

 

We can get highly ranked from high in the end what sequence, and then descending order to expand, so will save a lot of problems; final tally answer just fine;

 

#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;
}

 

Guess you like

Origin www.cnblogs.com/hrj1/p/11257309.html