UVALive 3977

时间:2023-03-09 00:35:40
UVALive 3977

直接搜索,简单题;

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define maxn 505
#define ll long long
using namespace std; int map[maxn][maxn];
int dir[][]={{,},{,},{,-},{-,}};
int vis[maxn][maxn]; struct node
{
int x,y,d;
node(int x=,int y=,int d=):x(x),y(y),d(d){}
bool operator<(const node &t)const
{
return d>t.d;
}
}arr[maxn*maxn]; queue<node>q; int main()
{
int t,n,m,d;
scanf("%d",&t);
while(t--)
{
int cnt=;
scanf("%d%d%d",&n,&m,&d);
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
scanf("%d",&map[i][j]);
arr[cnt++]=node(i,j,map[i][j]);
}
} memset(vis,-,sizeof vis);
int ans=;
sort(arr,arr+cnt);
while(!q.empty())q.pop(); for(int i=;i<cnt;i++)
{
node v=arr[i];
if(vis[v.x][v.y]!=-)continue;
bool flag=;
int bound=v.d-d;
int peak=v.d;
q.push(v);
int cot=; while(!q.empty())
{
node u=q.front();
q.pop();
vis[u.x][u.y]=peak;
for(int i=;i<;i++)
{
node tmp;
tmp.x=u.x+dir[i][];
tmp.y=u.y+dir[i][];
if(tmp.x<||tmp.x>n||tmp.y<||tmp.y>m)continue;
tmp.d=map[tmp.x][tmp.y];
if(tmp.d<=bound)continue;
if(vis[tmp.x][tmp.y]!=-)
{
if(vis[tmp.x][tmp.y]!=peak)
flag=;
continue;
}
vis[tmp.x][tmp.y]=peak;
if(tmp.d==peak)cot++;
q.push(tmp);
}
}
if(flag)
ans+=cot;
}
printf("%d\n",ans);
}
return ;
}