# |BZOJ 1619|搜索|[Usaco2008 Nov]Guarding the Farm 保卫牧场

bzoj传送门
Luogu免权限地址

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define ms(i,j) memset(i,j,sizeof i)
using namespace std;
const int MAXN = 700 + 5;
struct po
{
int x, y, h;
bool operator < (const po &b) const
{
return h > b.h;
}
}p[MAXN*MAXN];
const int dx[8] = {1,0,-1,0,1,-1,1,-1},
dy[8] = {0,1,0,-1,1,-1,-1,1};
int map[MAXN][MAXN];
int vi[MAXN][MAXN];
int n,m;
int cnt = 0;
int ans = 0;
int dfs(int x, int y)
{
for (int i=0;i<8;i++)
{
int tx = x + dx[i], ty = y + dy[i];
if (tx>0&&ty>0&&tx<=n&&ty<=m)
{
if (!vi[tx][ty])
if (map[tx][ty]<=map[x][y])
{
vi[tx][ty] = true;
dfs(tx, ty);
}
}
}
}
int main()
{
scanf("%d%d", &n, &m);
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
{
scanf("%d", &map[i][j]);
p[++cnt].x = i;
p[  cnt].y = j;
p[  cnt].h = map[i][j];
}
sort(p+1, p+1+cnt); ms(vi, false);
for (int i=1;i<=cnt;i++)
if (!vi[p[i].x][p[i].y])
{
vi[p[i].x][p[i].y] = true;
dfs(p[i].x, p[i].y); ans++;
}
printf("%d\n", ans);
return 0;
}