题解:
如果距离中没有T个箱子
那么就是可以的
代码:
#include<bits/stdc++.h> using namespace std; const int N=10005; const int qx[4]={0,0,1,-1}; const int qy[4]={1,-1,0,0}; int a[N],n,m,t,tot,id[350][305],b[N],Map[N][4],dis[1200][1200]; double ans=0; int pan(int x,int y) { if (x<1||x>n||y<1||y>m) return false; return true; } int main() { scanf("%d%d%d",&n,&m,&t); char c; tot=0; for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) { c='\0'; while (c!='0'&&c!='1') scanf("%c",&c); id[i][j]=++tot; if (c=='1') a[tot]=1; } memset(Map,0,sizeof(Map)); for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) for (int l=0;l<4;l++) if (pan(i+qx[l],j+qy[l])) Map[id[i][j]][l]=id[i+qx[l]][j+qy[l]]; memset(dis,0x3f3f3f3f,sizeof(dis)); for (int i=1;i<=tot;i++) dis[i][i]=a[i]; queue<int> q; for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) { while (!q.empty()) q.pop(); int from=id[i][j]; memset(b,false,sizeof(b)); b[from]=true; q.push(from); while (!q.empty()) { int u=q.front(); q.pop(); for (int i=0;i<4;i++) if (Map[u][i]) { int v=Map[u][i]; if (dis[from][u]+a[v]<dis[from][v]) { dis[from][v]=dis[from][u]+a[v]; if (!b[v]) { b[v]=true; q.push(v); } } } b[u]=false; } } for (int i=1;i<tot;i++) for (int j=i+1;j<=tot;j++) if (dis[i][j]<=t) { int x=i/m+1,y=i%m; if (i%m==0) x--,y=m; int xx=j/m+1,yy=j%m; if (j%m==0) xx--,yy=m; ans=max(ans,sqrt((x-xx)*(x-xx)*1.0+(y-yy)*(y-yy)*1.0)); } printf("%.6f",ans); return 0; }