[POJ 1191] 棋盘分割

[题目链接]

         http://poj.org/problem?id=1191

[算法]

        f[i][j][k][l][m] 表示进行了i次切割,当前矩形的左上角左标为(j,k),右下角坐标为(k,l) , sigma( (x - x') ^ 2 ) 的最小值

[代码]

       

#include <algorithm>  
#include <bitset>  
#include <cctype>  
#include <cerrno>  
#include <clocale>  
#include <cmath>  
#include <complex>  
#include <cstdio>  
#include <cstdlib>  
#include <cstring>  
#include <ctime>  
#include <deque>  
#include <exception>  
#include <fstream>  
#include <functional>  
#include <limits>  
#include <list>  
#include <map>  
#include <iomanip>  
#include <ios>  
#include <iosfwd>  
#include <iostream>  
#include <istream>  
#include <ostream>  
#include <queue>  
#include <set>  
#include <sstream>  
#include <stdexcept>  
#include <streambuf>  
#include <string>  
#include <utility>  
#include <vector>  
#include <cwchar>  
#include <cwctype>  
#include <stack>  
#include <limits.h> 
using namespace std;
#define MAXN 15
#define getsum(x1,y1,x2,y2) sum[x2][y2] - sum[x1 - 1][y2] - sum[x2][y1 - 1] + sum[x1 - 1][y1 - 1]
const int INF = 2e9;

long long i,j,k,x,y,t,n;
long long a[10][10];
double sum[10][10];
double cnt,ans;
double f[MAXN][10][10][10][10];

double min(double x,double y)
{
        return x < y ? x : y;
}
inline double sqr(double x)
{
        return x * x;
}

int main() 
{
        
        scanf("%lld",&n);
        for (i = 1; i <= 8; i++)
        {
                for (j = 1; j <= 8; j++)
                {
                        scanf("%lld",&a[i][j]);
                }
        }
        for (i = 1; i <= 8; i++)
        {
                for (j = 1; j <= 8; j++)
                {
                        sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j];
                }
        }
        cnt = sum[8][8] / n * 1.0;
        for (i = 1; i <= n; i++)
        {
                for (j = 1; j <= 8; j++)
                {
                        for (k = 1; k <= 8; k++)
                        {
                                for (x = 1; x <= 8; x++)
                                {
                                        for (y = 1; y <= 8; y++)
                                        {
                                                f[i][j][k][x][y] = INF;
                                        }
                                }
                        }
                }
        }
        for (i = 1; i <= 8; i++) f[1][1][1][i][8] = min(f[1][1][1][i][8],sqr(getsum(i + 1,1,8,8) - cnt));
        for (i = 1; i <= 8; i++) f[1][i][1][8][8] = min(f[1][i][1][8][8],sqr(getsum(1,1,i - 1,8) - cnt));
        for (i = 1; i <= 8; i++) f[1][1][1][8][i] = min(f[1][1][1][8][i],sqr(getsum(1,i + 1,8,8) - cnt));        
        for (i = 1; i <= 8; i++) f[1][1][i][8][8] = min(f[1][1][i][8][8],sqr(getsum(1,1,8,i - 1) - cnt));
        for (i = 1; i < n; i++)
        {
                for (j = 1; j <= 8; j++)
                {
                        for (k = 1; k <= 8; k++)
                        {
                                for (x = 1; x <= 8; x++)
                                {
                                        for (y = 1; y <= 8; y++)
                                        {
                                                for (t = k; t < y; t++) f[i + 1][j][k][x][t] = min(f[i + 1][j][k][x][t],f[i][j][k][x][y] + sqr(getsum(j,t + 1,x,y) - cnt));
                                                for (t = y; t > k; t--) f[i + 1][j][t][x][y] = min(f[i + 1][j][t][x][y],f[i][j][k][x][y] + sqr(getsum(j,k,x,t - 1) - cnt));
                                              for (t = j; t < x; t++) f[i + 1][j][k][t][y] = min(f[i + 1][j][k][t][y],f[i][j][k][x][y] + sqr(getsum(t + 1,k,x,y) - cnt));
                                              for (t = x; t > j; t--) f[i + 1][t][k][x][y] = min(f[i + 1][t][k][x][y],f[i][j][k][x][y] + sqr(getsum(j,k,t - 1,y) - cnt));
                                        }
                                }
                        }        
                }        
        }
        ans = INF;
        for (i = 1; i <= 8; i++)
        {
                for (j = 1; j <= 8; j++)
                {
                        for (x = 1; x <= 8; x++)
                        {
                                for (y = 1; y <= 8; y++)
                                {
                                        if (f[n - 1][i][j][x][y] != INF)
                                                ans = min(ans,f[n - 1][i][j][x][y] + sqr(getsum(i,j,x,y) - cnt));
                                }
                        }
                }
        }
        printf("%.3lf\n",1.0 * sqrt(ans / n));
        
        return 0;
    
}

 

转载于:https://www.cnblogs.com/evenbao/p/9366362.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值