https://www.luogu.org/problemnew/show/P2919
#include <bits/stdc++.h> #define read read() #define up(i,l,r) for(int i = l; i<= r; i++) using namespace std; const int N = 705; struct point{ int x,y,h; }p[N * N]; int read { int x = 0;char ch = getchar(); while(ch < 48 || ch > 57) ch = getchar(); while(ch >=48 && ch <= 57) {x = 10 * x + ch - 48; ch = getchar();} return x; } bool cmp(point a,point b) {return a.h > b.h;} int n,m,vis[N][N],ans,height[N][N]; int dx[] = {0,-1,-1,-1, 0, 0, 1, 1, 1}; int dy[] = {0,-1, 0, 1,-1, 1,-1, 0, 1}; void dfs(int x,int y) { up(i,1,8) { int nx = dx[i] + x, ny = dy[i] + y; if(vis[nx][ny] || height[nx][ny] > height[x][y]) continue; if( nx <= 0 || ny <= 0 || nx > n || ny > m ) continue; vis[nx][ny] = 1,dfs(nx,ny); } } int main() { n = read; m = read; int cnt = 0; up(i,1,n) up(j,1,m) { p[++cnt].x = i; p[cnt].y = j; p[cnt].h = read; height[i][j] = p[cnt].h; } sort(p + 1,p + cnt + 1,cmp); up(i,1,cnt) { if(!vis[p[i].x][p[i].y]) { vis[p[i].x][p[i].y] = 1; dfs(p[i].x,p[i].y);//染色 ans++; } } printf("%d",ans); return 0; }