1619: [Usaco2008 nov]guarding the farm defends the ranch search

Source: Internet
Author: User

title : http://www.lydsy.com/JudgeOnline/problem.php?id=1619
1619: [Usaco2008 nov]guarding the farm defends the ranch
Time Limit:5 Sec Memory limit:64 MB
submit:694 solved:306
[Submit] [Status] [Discuss]

The farm has many hills upon which Farmer John would what to place guards to ensure the safety of his valuable milk-cow S. He wonders how many guards he would 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 <=) rows and m (1 < M <=) columns. Each member of the matrix was 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 O R elements with a lower (smaller) altitude. Different elements is adjacent if the magnitude of difference in their X coordinates is no greater than 1 and the mag Nitude of differences in their Y coordinates is also no greater than 1.

Farmer John's Farmer has many small hills where he wants to decorate some bodyguards (...). To defend his very valuable cows. He wanted to know how many bodyguards he would need to recruit if a bodyguard was placed on a small hill. He now has a map with a number matrix to represent the terrain. This matrix has N rows (1 < N < = 100) and M columns (1 < M < = 70). Each element in the matrix has a value of H_ij (0 < = H_ij < =10,000) to indicate the altitude of the area. Please help him to count the number of hills on the map. The definition of a hill is that if all the elements adjacent to an element in the map are smaller than the height of the element (or it is adjacent to the boundary of the map), then the element and all the elements around it are called a small hill. The meaning of the adjacency here is that if one element is not more than 1 from the other horizontal ordinate and its horizontal ordinate, then the two elements are said to be contiguous. Issue name: Guard input Format: first line: Two integers separated by spaces N and M the second line to line n+1: line i+1 describes the line I on the map, there are m spaces separated by the integer: H_ij. Input sample: (guard.in): 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 format: First line: The number of small Hills output sample: (guard.out): 3 Output Sample explanation: There are three hills on the map: The peaks of each hill are located in the upper-left corner (height is 4), the upper-right corner (height is 1) and the bottom (height is 2).


    • 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

Sample Input

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

Sample Output



Three hills are: the upper left corner of the height of 4 square, the upper right corner of the height of 1 square, and the last row of the height of 2 square.

Ideas :
Sort side after the search to determine the number of unicom blocks, unicom conditions:

Code with DFS

Code :

#include <iostream>#include <stdio.h>#include <algorithm>Using namespace Std;struct node{int x,y, H;}q[490015];int Ma[715][715];intNm;intxx[8]={0,0,1,1,1,-1,-1,-1},yy[8]={1,-1,1,0,-1,1,0,-1}; using namespace Std;intvis[715][715];intCMP (nodex, nodey){return x.h>y. h;} void Dfs (int x,int y){ for(intI=0;i<8; i++) {inttx=x+xx[i];intty=y+yy[i];if(tx>=1&&ty>=1&&tx<=n&&ty<=m&&!vis[tx][ty]&&Ma[TX] [ty]<=Ma[x][y]) {vis[tx][ty]=1;        DFS (Tx,ty); }    }}intMain () {scanf ("%d%d",&n,&m);inttot=0; for(intI=1; i<=n;i++) for(intj=1; j<=m; j + +) {scanf ("%d",&MaI [j]);Q[++tot].x=i;Q[tot].y=j;Q[tot]. h=MaI    [j]; }Sort(Q+1,Q+1+TOT,CMP);intans=0; for(intI=1; i<=tot;i++) {if(!vis[Q[i].x][Q[i].y]) {ans++; vis[Q[i].x][Q[i].y]=1; DfsQ[i].x,Q[i].y); }    }printf("%d", ans);}

1619: [Usaco2008 nov]guarding the farm defends the ranch search

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.