一道简单的染色题(需要排序)
Posted ${color{Cyan}{talk;is;cheap;;s
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一道简单的染色题(需要排序)相关的知识,希望对你有一定的参考价值。
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; }
以上是关于一道简单的染色题(需要排序)的主要内容,如果未能解决你的问题,请参考以下文章