Bzoj1619/p2919 [usaco 08nov] guardian farm guarding the farm

Source: Internet
Author: User

P2919 [usaco 08nov] Guard farm guarding the farm

Similar questions: p3456 [poi2007] grz-ridges and valleys

Each time the BFS is at the same altitude, it determines whether it is a hill based on the size of the surrounding blocks.

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <queue> 5 # include <cctype> 6 # define re register 7 using namespace STD; 8 void read (Int & X) {9 char c = getchar (); X = 0; 10 while (! Isdigit (c) c = getchar (); 11 while (isdigit (c) x = (x <3) + (x <1) + (C ^ 48), c = getchar (); 12} 13 # define n 70214 const int D1 [8] = {, 0,-, 1,-1, -1}; 15 const int D2 [8] = {,-, 1,-,-1}; 16 struct data {int X, Y ;}; 17 int n, m, H [N] [N], ans; bool vis [N] [N]; 18 void BFS (INT F1, int F2) {19 queue <DATA> H; H. push (data) {F1, F2}); 20 vis [F1] [F2] = 1; int p = 1; 21 While (! H. empty () {22 data u = H. front (); H. pop (); 23 for (INT I = 0; I <8; ++ I) {24 int R1 = u. X + D1 [I], R2 = u. Y + D2 [I]; 25 if (R1 <1 | R1> N | R2 <1 | r2> m) continue; 26 P & = (H [R1] [R2] <= H [U. x] [U. y]); // whether it is Hill 27 if (vis [R1] [R2]) continue; 28 If (H [R1] [R2] = H [U. x] [U. y]) 29 H. push (data) {R1, R2}), vis [R1] [R2] = 1; 30} 31} ans + = P; 32} 33 int main () {34 read (n); read (m); 35 for (Re int I = 1; I <= N; ++ I) 36 for (Re Int J = 1; j <= m; ++ J) 37 read (H [I] [J]); 38 for (R E int I = 1; I <= N; ++ I) 39 for (Re Int J = 1; j <= m; ++ J) 40 if (! Vis [I] [J]) BFS (I, j); 41 printf ("% d", ANS); 42 return 0; 43}
View code

 

Bzoj1619/p2919 [usaco 08nov] guardian farm guarding the farm

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.