FGD小朋友特别喜欢爬山,在爬山的时候他就在研究山峰和山谷。为了能够对旅程有一个安排,他想知道山峰和山谷的数量。给定一个地图,为FGD想要旅行的区域,地图被分为 n×n的网格,每个格子 (i,j) 的高度 w(i,j)是给定的。若两个格子有公共顶点,那么它们就是相邻的格子,如与 (i,j)邻的格子有(i−1,j−1),(i−1,j),(i−1,j+1),(i,j−1),(i,j+1),(i+1,j−1),(i+1,j),(i+1,j+1)。我们定义一个格子的集合 S为山峰(山谷)当且仅当:

第一行包含一个正整数 n,表示地图的大小。接下来一个 n×n的矩阵,表示地图上每个格子的高度 w。
共一行,包含两个整数,表示山峰和山谷的数量。
数据范围:1≤n≤1000,0≤w≤109
- 5
- 8 8 8 7 7
- 7 7 8 8 7
- 7 7 7 7 7
- 7 8 8 7 8
- 7 8 8 8 8
2 1
- 5
- 5 7 8 3 1
- 5 5 7 6 6
- 6 6 6 2 8
- 5 7 2 5 8
- 7 1 0 1 7
3 3
样例解释


- #include
- #define endl '\n'
- #define int long long
- #define Bug cout<<"---------------------"<
- using namespace std;
- const int N = 1010;
- int mp[N][N];
- bool vis[N][N];
- int n;
- int dx[8] = { 0,0,1,-1,1,-1,-1,1 };
- int dy[8] = { 1,-1,0,0,-1,-1,1,1 };
- bool in(int x, int y) {
- return x >= 0 && x < n&& y >= 0 && y < n;
- }
- int flag;
- int dfs(int x, int y) {
- int now = mp[x][y];
- vis[x][y] = true;
- for (int i = 0;i < 8;i++) {
- int tx = x + dx[i];
- int ty = y + dy[i];
- //第一次对周围的判断,如果有大的,则标记为山谷。有小的则是山峰
- if (in(tx, ty)&&mp[tx][ty] > now&&flag==-1) {
- flag = 1;
- }
- if (in(tx, ty) && mp[tx][ty] < now&&flag==-1) {
- flag = 2;
- }
- //后续如果有违反规律的情况,就return-1;
- if (in(tx, ty) && flag == 2 && mp[tx][ty] > now) {
- return -1;
- }
- if ( in(tx, ty) && flag == 1 && mp[tx][ty] < now) {
- return -1;
- }
- //探索周围的所有情况;如果相等,就探索。
- if (!vis[tx][ty] && in(tx, ty) && mp[tx][ty] == now) {
- dfs(tx, ty);
- }
- }
- return flag;
- }
- signed main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0); cout.tie(0);
- cin >> n;
- for (int i = 0;i < n;i++) {
- for (int j = 0;j < n;j++) {
- cin >> mp[i][j];
- }
- }
- int mon = 0;int gu = 0;
- for (int i = 0;i < n;i++) {
- for (int j = 0;j < n;j++) {
- //memset(vis, false, sizeof(vis));
- flag = -1;
- if (!vis[i][j]) {
- flag = dfs(i, j);
- }
- if(flag==1){
- gu++;
- }
- else if (flag == 2) {
- mon++;
- }
- else if (flag == -1) {
- continue;
- }
- }
- }
- cout << mon << ' ' << gu << endl;
- return 0;
- }
- //山谷和山峰
- #include
- #include
- using namespace std;
- const int N=1005;
- int n;
- bool f[N][N];
- int mp[N][N];
- bool has_higher,has_lower;
- void dfs(int x,int y,bool& has_higher,bool& has_lower){
- f[x][y]=true;
- for(int i=-1;i<=1;i++){
- for(int j=-1;j<=1;j++){
- int nx=x+i;
- int ny=y+j;
- if(nx<0 || nx>=n || ny<0 || ny>=n){
- continue;
- }
- if(mp[x][y]!=mp[nx][ny]){//高度不相等
- if(mp[x][y] < mp[nx][ny]){
- has_higher=true;
- }
- if(mp[x][y] > mp[nx][ny]){
- has_lower=true;
- }
- }else{//高度相等
- //has_higher=true;
- //has_lower=true;
- if(f[nx][ny]){
- //已经访问过
- continue;
- }
- f[nx][ny]=true;
- dfs(nx,ny,has_higher,has_lower);
- }
- }
- }
- }
- int main(){
- int vally=0,peak=0;
- cin>>n;
- set<int>v;
- for(int i=0;i
- for(int j=0;j
- cin>>mp[i][j];
- v.insert(mp[i][j]);
- }
- }
- if(v.size()==1){
- cout<<1<<' '<<1<
- return 0;
- }
- for(int i=0;i
- for(int j=0;j
- if(!f[i][j]){
- has_higher=false;
- has_lower=false;
- dfs(i,j,has_higher,has_lower);
- if(has_higher && has_lower){
- continue;
- }
- if(has_higher){
- vally++;
- }
- if(has_lower){
- peak++;
- }
- }
- }
- }
- cout<
' '< - return 0;
- }
BFS-AC代码
- #include
- #define endl '\n'
- #define int long long
- using namespace std;
- constexpr int N = 1010;
- typedef pair<int, int>pii;
- int n;
- int mp[N][N];
- bitset
vis[N]; - int dx[10] = { 1,-1,0,0,1,1,-1,-1 };
- int dy[10] = { 0,0,1,-1,1,-1,-1,1 };
- int mon, vall;
- bool in(int x, int y) {
- return x >= 0 && x < n&& y >= 0 && y < n;
- }
- void bfs(int x,int y,bool&higher,bool&lower) {
- queue
q; - vis[x][y] = true;
- q.push(make_pair(x, y));
- while (!q.empty()) {
- pii now = q.front();
- q.pop();
- for (int i = 0;i < 8;i++) {
- int tx = now.first + dx[i];
- int ty = now.second + dy[i];
- if (in(tx, ty) && !vis[tx][ty] && mp[tx][ty] == mp[now.first][now.second]) {
- vis[tx][ty] = true;
- q.push(make_pair(tx,ty));
- }
- else if (in(tx, ty) && mp[tx][ty] > mp[now.first][now.second]) {
- higher = true;
- }
- else if (in(tx, ty) && mp[tx][ty] < mp[now.first][now.second]) {
- lower = true;
- }
- }
- }
- }
- signed main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0); cout.tie(0);
- cin >> n;
- for (int i = 0;i < n;i++) {
- for (int j = 0;j < n;j++) {
- cin >> mp[i][j];
- }
- }
- for (int i = 0;i < n;i++) {
- for (int j = 0;j < n;j++) {
- if (!vis[i][j]) {
- bool higher = false;bool lower = false;
- bfs(i, j,higher,lower);
- if (!higher && !lower) {
- mon++, vall++;
- }
- else if (!higher&&lower) {
- mon++;
- }
- else if (!lower&&higher) {
- vall++;
- }
- }
- }
- }
- cout << mon << ' ' << vall << endl;
- return 0;
- }
—
-
相关阅读:
Linux系统之部署Dailynotes个人笔记管理工具登录到Dailynotes首页,输入内容无法保存,如何解决?
数字化新零售营销模式如何落地?数字化新零售营销功能推荐
Tomcat总体架构,启动流程与处理请求流程
Socks5代理IP:保障跨境电商的网络安全
DGIOT国内首家轻量级物联网开源平台——MQTT接入实战教程
【C++】设计模式之观察者模式
leetcode每天5题-Day53-贪心2
如何在Vim中使用内置的Make命令来编译程序?
植入“人工心脏”助患者重获“心”生
【CMU15-445 Part-8】Tree Indexes ii
-
原文地址:https://blog.csdn.net/m0_72522122/article/details/127637385