题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1048
题解:搞清题意之后来个记忆化爆搜就行了。
代码:
#include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<algorithm> #include<iostream> #include<vector> #include<map> #include<set> #include<queue> #include<string> #define inf 1000000000 #define maxn 11 #define maxm 200000+5 #define eps 1e-10 #define ll long long #define pa pair<int,int> #define for0(i,n) for(int i=0;i<=(n);i++) #define for1(i,n) for(int i=1;i<=(n);i++) #define for2(i,x,y) for(int i=(x);i<=(y);i++) #define for3(i,x,y) for(int i=(x);i>=(y);i--) #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go) #define for5(n,m) for(int i=1;i<=n;i++)for(int j=1;j<=m;j++) #define mod 1000000007
#define sqr(x) (x)*(x) using namespace std; inline int read() { int x=,f=;char ch=getchar(); while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();} while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();} return x*f; }
int n,m,k,s[maxn][maxn];
double ave,f[maxn][maxn][maxn][maxn][maxn];
inline double dp(int x1,int y1,int x2,int y2,int z)
{
double &t=f[x1][y1][x2][y2][z];
if(t<inf)return t;
if(!z)return t=sqr(s[x2][y2]-s[x1-][y2]-s[x2][y1-]+s[x1-][y1-]-ave);
t=inf;
for0(i,z-)
{
for2(j,x1,x2-)t=min(t,dp(x1,y1,j,y2,i)+dp(j+,y1,x2,y2,z--i));
for2(j,y1,y2-)t=min(t,dp(x1,y1,x2,j,i)+dp(x1,j+,x2,y2,z--i));
}
//cout<<x1<<' '<<y1<<' '<<x2<<' '<<y2<<' '<<z<<' '<<t<<endl;
return t;
} int main() { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); n=read();m=read();k=read();
for1(i,n)for1(j,m)s[i][j]=s[i-][j]+s[i][j-]-s[i-][j-]+read();
ave=(double)s[n][m]/(double)k;
for1(i1,n)for1(i2,m)for1(i3,n)for1(i4,m)for0(i5,k)f[i1][i2][i3][i4][i5]=inf;
dp(,,n,m,k-);
printf("%.2f\n",sqrt((double)f[][][n][m][k-]/(double)k)); return ; }