洛谷 P2919 [USACO08NOV]守護農場Guarding the Farm

題目描述

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.

?

?

搜索

屠龍寶刀點擊就送

#include <algorithm>
#include <cstdio>
#include <queue>
#define N 705using namespace std;
struct node
{int h,x,y;bool operator<(node a)const{return h>a.h;}
}sf[N*N];
bool vis[N][N];
int n,m,ans,tot,G[N][N],fx[8]={1,-1,0,0,1,1,-1,-1},fy[8]={0,0,-1,1,1,-1,-1,1};
struct Node
{int x,y;
};
void bfs(int x,int y)
{queue<Node>Q;Q.push((Node){x,y});vis[x][y]=1;for(Node now;!Q.empty();){now=Q.front();Q.pop();int H=G[now.x][now.y];for(int i=0;i<8;++i){int tx=now.x+fx[i],ty=now.y+fy[i];if(tx<1||tx>n||ty<1||ty>m||vis[tx][ty]||G[tx][ty]>H) continue;Q.push((Node){tx,ty});vis[tx][ty]=1;}}
}
int Main()
{scanf("%d%d",&n,&m);for(int i=1;i<=n;++i){for(int j=1;j<=m;++j){scanf("%d",&sf[++tot].h);sf[tot].x=i;sf[tot].y=j;G[i][j]=sf[tot].h;}}sort(sf+1,sf+1+tot); for(int i=1;i<=tot;++i){if(vis[sf[i].x][sf[i].y])continue;bfs(sf[i].x,sf[i].y);ans++;}printf("%d\n",ans);return 0;
}
int sb=Main();
int main(int argc,char *argv[]){;}

?

轉載于:https://www.cnblogs.com/ruojisun/p/7607929.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391263.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391263.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391263.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

iOS 開發一定要嘗試的 Texture(ASDK)

原文鏈接 - iOS 開發一定要嘗試的 Texture(ASDK)(排版正常, 包含視頻) 前言 本篇所涉及的性能問題我都將根據滑動的流暢性來評判, 包括掉幀情況和一些實際體驗 ASDK 已經改名為 Texture, 我習慣稱作 ASDK 編譯環境: MacOS 10.13.3, Xcode 9.2 參與測試機型: iPhone 6 10.3.3, i…

lisp語言是最好的語言_Lisp可能不是數據科學的最佳語言,但是我們仍然可以從中學到什么呢?...

lisp語言是最好的語言This article is in response to Emmet Boudreau’s article ‘Should We be Using Lisp for Data-Science’.本文是對 Emmet Boudreau的文章“我們應該將Lisp用于數據科學”的 回應 。 Below, unless otherwise stated, lisp refers to Common Lisp; in …

鏈接訪問后刷新顏色回到初始_如何使鏈接可訪問(提示:顏色不夠)

鏈接訪問后刷新顏色回到初始Link accessibility is one of the most important aspects of usability. However, designers often dont understand what it takes to make links accessible. Most frequently, they only distinguish links by color, which makes it hard for …

567

567 轉載于:https://www.cnblogs.com/Forever77/p/11519678.html

leetcode 403. 青蛙過河(dp)

一只青蛙想要過河。 假定河流被等分為若干個單元格&#xff0c;并且在每一個單元格內都有可能放有一塊石子&#xff08;也有可能沒有&#xff09;。 青蛙可以跳上石子&#xff0c;但是不可以跳入水中。 給你石子的位置列表 stones&#xff08;用單元格序號 升序 表示&#xff…

static、volatile、synchronize

原子性&#xff08;排他性&#xff09;&#xff1a;不論是多核還是單核&#xff0c;具有原子性的量&#xff0c;同一時刻只能有一個線程來對它進行操作&#xff01;可見性&#xff1a;多個線程對同一份數據操作&#xff0c;thread1改變了某個變量的值&#xff0c;要保證thread2…

tensorflow基本教程

轉載自 http://tensornews.cn/ 轉載于:https://www.cnblogs.com/Chris-01/p/11523316.html

1.10-linux三劍客之sed命令詳解及用法

內容:1.sed命令介紹2.語法格式,常用功能查詢 增加 替換 批量修改文件名第1章 sed是什么字符流編輯器 Stream Editor第2章 sed功能與版本處理出文本文件,日志,配置文件等增加,刪除,修改,查詢sed --versionsed -i 修改文件內容第3章 語法格式3.1 語法格式sed [選項] [sed指令…

python pca主成分_超越“經典” PCA:功能主成分分析(FPCA)應用于使用Python的時間序列...

python pca主成分FPCA is traditionally implemented with R but the “FDASRSF” package from J. Derek Tucker will achieve similar (and even greater) results in Python.FPCA傳統上是使用R實現的&#xff0c;但是J. Derek Tucker的“ FDASRSF ”軟件包將在Python中獲得相…

blender視圖縮放_如何使用主視圖類型縮放Elm視圖

blender視圖縮放A concept to help Elm Views scale as applications grow larger and more complicated.當應用程序變得更大和更復雜時&#xff0c;可幫助Elm Views擴展的概念。 In Elm, there are a lot of great ways to scale the Model, and update, but there is more c…

初探Golang(2)-常量和命名規范

1 命名規范 1.1 Go是一門區分大小寫的語言。 命名規則涉及變量、常量、全局函數、結構、接口、方法等的命名。 Go語言從語法層面進行了以下限定&#xff1a;任何需要對外暴露的名字必須以大寫字母開頭&#xff0c;不需要對外暴露的則應該以小寫字母開頭。 當命名&#xff08…

789

789 轉載于:https://www.cnblogs.com/Forever77/p/11524161.html

sql的split()函數

ALTER function [dbo].[StrToList_Test](Str varchar(max), fg NVARCHAR(200)) returns table table(value nvarchar(max) ) as begindeclare tempStr nvarchar(max),len INT LEN(fg); --去除前后分割符 while substring(Str,1,len)fg beginset Strsubstring(Str,len1,len(S…

大數據平臺構建_如何像產品一樣構建數據平臺

大數據平臺構建重點 (Top highlight)Over the past few years, many companies have embraced data platforms as an effective way to aggregate, handle, and utilize data at scale. Despite the data platform’s rising popularity, however, little literature exists on…

初探Golang(3)-數據類型

Go語言擁有兩大數據類型&#xff0c;基本數據類型和復合數據類型。 1. 數值類型 ##有符號整數 int8&#xff08;-128 -> 127&#xff09; int16&#xff08;-32768 -> 32767&#xff09; int32&#xff08;-2,147,483,648 -> 2,147,483,647&#xff09; int64&#x…

freecodecamp_freeCodeCamp的服務器到底發生了什么?

freecodecampUpdate at 17:00 California time: We have now fixed most of the problems. Were still working on a few known issues, but /learn is now fully operational.加利福尼亞時間17:00更新 &#xff1a;我們現在解決了大多數問題。 我們仍在處理一些已知問題&#…

為什么Linux下的環境變量要用大寫而不是小寫

境變量的名稱通常用大寫字母來定義。實際上用小寫字母來定義環境變量也不會報錯&#xff0c;只是習慣上都是用大寫字母來表示的。 首先說明一下&#xff0c;在Windows下是不區分大小寫的&#xff0c;所以在Windows下怎么寫都能獲取到值。 而Linux下不同&#xff0c;區分大小寫&…

python:連接Oracle數據庫后控制臺打印中文為??

打印查詢結果&#xff0c;中文顯示為了&#xff1f;&#xff1f;&#xff1f; [(72H FCR, 2.0), (?????, 8.0)] E:\Python35\Lib\site-packages中新增文件&#xff1a; sitecustomize.py import os os.environ[NLS_LANG] SIMPLIFIED CHINESE_CHINA.UTF8 轉載于:https://w…

時間序列預測 時間因果建模_時間序列建模以預測投資基金的回報

時間序列預測 時間因果建模Time series analysis, discussed ARIMA, auto ARIMA, auto correlation (ACF), partial auto correlation (PACF), stationarity and differencing.時間序列分析&#xff0c;討論了ARIMA&#xff0c;自動ARIMA&#xff0c;自動相關(ACF)&#xff0c;…

初探Golang(4)-map和流程控制語句

1.map map 是引用類型的&#xff0c;如果聲明沒有初始化值&#xff0c;默認是nil。空的切片是可以直接使用的&#xff0c;因為他有對應的底層數組,空的map不能直接使用。需要先make之后才能使用。 //1, 聲明map 默認值是nil var m1 map[key_data_type]value_data_type 聲明 …