POJ - 1191 棋盘分割 记忆递归 搜索dp+数学

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

题意:中文题。

 题解:

1.关于切割的模拟,用递归 有这样的递归方程(dp方程):f(n,棋盘)=f(n-1,待割的棋盘)+f(1,割下的棋盘)

2.考虑如何计算方差,根据以下方差公式

我们只需算∑X 2的最小值//然后将它乘以n,减去总和的平方,除以n^2,再整体开根号就行了,化简一下的结果

3.关于棋盘的表示,我们用左上角坐标与右下角坐标,常规表示

4.关于计算优化,用sum二维前缀和。并且进行记忆化递归。

 技巧:1&引用 化简代码 2 二维前缀和的预处理

 

坑:我在poj上搜找这题,搜chess,rectangle,cut死活找不到,组后发现是到noi的中文题qrz。。。

  +1,-1 要注意

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <math.h>
#include <string.h>
#include <string>
#include <map>
#include<stack>
#include<set>
#include<string.h>
#include<iomanip>
#define pb push_back
#define _for(i, a, b) for (int i = (a); i<(b); ++i)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)

using namespace std;
const  int N =8+ 5;
//double num[N], price[N], ave[N];
int s[N][N];
int sum[N][N];
int res[15][N][N][N][N];
int calSum(int x1, int y1, int x2, int y2) {
    return sum[x2][y2] - sum[x2][y1-1] - sum[x1-1][y2] + sum[x1-1][y1-1];
}
int f(int n, int x1, int y1, int x2, int y2) {
    int t, a, b, c, e, mn = 1e7;
    int& ans = res[n][x1][y1][x2][y2];
    if (ans != -1) return ans;
    if (n == 1) {
        t = calSum(x1, y1, x2, y2);
        ans = t*t;
        return ans;
    }
    for (a = x1; a < x2; a++) {
        c = calSum(a + 1, y1, x2, y2);
        e = calSum(x1, y1, a, y2);
        t = min(c*c + f(n - 1, x1, y1, a, y2), e*e + f(n-1,a + 1, y1, x2, y2));
        if (mn > t)mn = t;
    }
    for (b = y1; b < y2; b++) {
        c = calSum(x1, b+1, x2, y2);
        e = calSum(x1, y1, x2, b);
        t = min(c*c + f(n-1,x1, y1, x2, b), e*e + f(n-1,x1, b + 1, x2, y2));
        if (mn > t)mn = t;
    }
    ans = mn;
    return ans;
}
int main() {
    memset(res, -1, sizeof(res));
    int n;
    cin >> n;
    
    _for(i,1,9)
        for(int j=1,rowsum=0;j<9;j++) {
        cin >> s[i][j];
        rowsum += s[i][j];
        sum[i][j] += sum[i - 1][j] + rowsum;
    }
    
    double result = n*f(n, 1, 1, 8, 8) - sum[8][8] * sum[8][8];
    cout << setiosflags(ios::fixed) << setprecision(3) << sqrt(result / (n*n)) << endl;
    
        
    system("pause");
}

 

posted @ 2018-04-02 21:39  SuuTTT  阅读(357)  评论(0编辑  收藏  举报