山峰和山谷(信息学奥赛一本通-T1454)

【题目描述】

给定一个 n×n 的网格状地图,每个方格 (i,j)有一个高度 wij​​ 。如果两个方格有公共顶点,则它们是相邻的。

定义山峰和山谷如下:

均由地图上的一个连通块组成;

所有方格高度都相同;

周围的方格(即不属于山峰或山谷但与山峰或山谷相邻的格子)高度均大于山谷的高度,或小于山峰的高度。

求地图内山峰和山谷的数量。特别地,如果整个地图方格的高度均相同,则整个地图既是一个山谷,也是一个山峰。

【输入】

第一行一个整数n(2≤n≤1000),表示地图的大小。

接下来 n 行每行 n 个整数表示地图。第 i 行有 n 个整数 wi1,wi2,…,win(0≤wij≤1000000000),表示地图第 i 行格子的高度。

【输出】

输出一行两个整数,分别表示山峰和山谷的数量。

【输入样例】

输入样例1

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:

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

【输出样例】

输出样例1:

2 1

样例解释:

输出样例2:

3 3

样例解释:

思路:基础的 BFS,只要按照高度排序后,分别 BFS 山峰、山谷即可

【源程序】

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL multMod(LL a,LL b,LL mod){ a%=mod; b%=mod; LL res=0; while(b){if(b&1)res=(res+a)%mod; a=(a<<=1)%mod; b>>=1; } return res%mod;}
LL quickMultPowMod(LL a, LL b,LL mod){ LL res=1,k=a; while(b){if((b&1))res=multMod(res,k,mod)%mod; k=multMod(k,k,mod)%mod; b>>=1;} return res%mod;}
LL quickPowMod(LL a,LL b,LL mod){ LL res=1; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1; } return res; }
LL getInv(LL a,LL mod){ return quickPowMod(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-6;
const int MOD = 1000000000+7;
const int N = 1000+5;
const int dx[] = {0,0,1,-1,1,1,-1,-1};
const int dy[] = {1,-1,0,0,1,-1,1,-1};
using namespace std;
struct Node {
    int x, y;
    int val;
    Node() {}
    Node(int x, int y) : x(x), y(y) {}
    Node(int x, int y, int val) : x(x), y(y), val(val) {}
    bool operator<(const Node &rhs) const { return val < rhs.val; }
} node[N * N];
int n;
int G[N][N];
bool vis[N][N];
void downBFS(int x, int y) {
    queue<Node> Q;
    Q.push(Node(x, y));
    vis[x][y] = true;
    while (!Q.empty()) {
        Node now = Q.front();
        Q.pop();
        int x = now.x, y = now.y;
        for (int i = 0; i < 8; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (nx <= 0 || ny <= 0 || nx > n || ny > n)
                continue;
            if (!vis[nx][ny] && G[x][y] <= G[nx][ny]) {
                vis[nx][ny] = true;
                Q.push(Node(nx, ny));
            }
        }
    }
}
void upBFS(int x, int y) {
    queue<Node> Q;
    Q.push(Node(x, y));
    vis[x][y] = true;
    while (!Q.empty()) {
        Node now = Q.front();
        Q.pop();
        int x = now.x, y = now.y;
        for (int i = 0; i < 8; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (nx <= 0 || ny <= 0 || nx > n || ny > n)
                continue;
            if (!vis[nx][ny] && G[x][y] >= G[nx][ny]) {
                vis[nx][ny] = true;
                Q.push(Node(nx, ny));
            }
        }
    }
}
int main() {
    while (scanf("%d", &n) != EOF) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                scanf("%d", &G[i][j]);
                int pos = (i - 1) * n + j;
                node[pos] = Node(i, j, G[i][j]);
            }
        }
        sort(node + 1, node + 1 + n * n);

        int down = 0;
        memset(vis, false, sizeof(vis));
        for (int i = 1; i <= n * n; i++) {
            int x = node[i].x;
            int y = node[i].y;
            if (!vis[x][y]) {
                downBFS(x, y);
                down++;
            }
        }
        int up = 0;
        memset(vis, false, sizeof(vis));
        for (int i = n * n; i >= 1; i--) {
            int x = node[i].x;
            int y = node[i].y;
            if (!vis[x][y]) {
                upBFS(x, y);
                up++;
            }
        }
        printf("%d %d\n", up, down);
    }
    return 0;
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值