分析
BFS出到每一个点的(距离Limit
)最短路径
将收到的宝藏sort+unique
然后再暴力max一下,即可
代码
//Newcoder 18 C #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <queue> #include <cstring> #define LL long long #define Lowbit(X) (X&(-X)) #define Lson (X<<1) #define Rson (X<<1|1) #define Cl(X,Y) memset((X),(Y),sizeof(X)) #define FOR(i,A,B) for(LL i=A;i<=B;i++) #define BOR(i,A,B) for(LL i=A;i>=B;i--) #define FOR_SIDE(i,A) for(LL i=Head[A];i;i=Next[i]) #define INF 0x3f3f3f3f3f3f3f3f using namespace std; const LL MAXN=1e3+10; LL Row,Line,Ans,Test; LL Cube[MAXN][MAXN],Stax,Stay,Dist,Limit; bool Vis[MAXN][MAXN],Jud[MAXN][MAXN]; LL Num[1000010],Cnt,Dep[MAXN][MAXN]; LL Dx[5]={0,0,-1,0,1},Dy[5]={0,1,0,-1,0}; struct Node { LL E,F; }; queue<Node>Temp; inline void File() { freopen(".in","r",stdin); freopen(".out","w",stdout); } inline void BFS() { Temp.push(Node{ Stax,Stay }); Vis[Stax][Stay]=true; while(!Temp.empty()) { Node New=Temp.front(); Temp.pop(); if(Cube[New.E][New.F]>0) Num[++Cnt]=Cube[New.E][New.F]; if(Dep[New.E][New.F]>=Dist) continue; FOR(i,1,4) { LL Nx=New.E+Dx[i],Ny=New.F+Dy[i]; if(Nx>Row || Nx<1 || Ny>Line || Ny<1 || Vis[Nx][Ny]) continue; Dep[Nx][Ny]=Dep[New.E][New.F]+1; Vis[Nx][Ny]=true; Temp.push(Node{ Nx,Ny }); } } } inline void Solve() { sort(Num+1,Num+Cnt+1); LL Size=unique(Num+1,Num+Cnt+1)-(Num+1); if(Size<Limit) { printf("no\n"); } else { FOR(i,1,Size-Limit+1) Ans=min(Ans,Num[i+Limit-1]-Num[i]); printf("%lld\n",Ans); } } int main() { //File(); scanf("%lld",&Test); while(Test--) { Cl(Num,0); Cnt=0; Cl(Vis,false); Cl(Cube,0); Ans=INF; Cl(Dep,0); Cl(Jud,false); while(!Temp.empty()) Temp.pop(); scanf("%lld %lld %lld %lld %lld %lld",&Row,&Line,&Stax,&Stay,&Dist,&Limit); FOR(i,1,Row) FOR(j,1,Line) { scanf("%lld",&Cube[i][j]); if(Cube[i][j]==-1) Vis[i][j]=true; } if(Cube[Stax][Stay]==-1) { cout<<"no"<<endl; continue; } BFS(); Solve(); } //fclose(stdin); fclose(stdout); // system("pause"); return 0; }