zoukankan      html  css  js  c++  java
  • POJ1191 棋盘分割

    Time Limit: 1000MS              Memory Limit: 10000K
    Total Submissions: 16587        Accepted: 5951

    题目链接:

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

    解题思路:

    关键是方程式的化简,思路清晰,整个题的难度不大。

    AC代码(课程PPT)

    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<cmath> 
    #include<iomanip>
    
    using namespace std;
    
    int s[9][9];    //每个格子的分数 
    int sum[9][9];    //(1,1)到(i,j)的矩形的分数之和 
    int res[15][9][9][9][9];    //fun的记录表 
    
    int calSum(int x1, int y1, int x2, int y2)//(x1,y1)到(x2,y2)的矩形的分数之和 
    {  
        return sum[x2][y2]-sum[x2][y1-1]-sum[x1-1][y2]+sum[x1-1][y1-1]; 
    }   
    
    int fun(int n, int x1, int y1, int x2, int y2) 
    {
        int t, a, b, c, e, MIN = 10000000;     
        if (res[n][x1][y1][x2][y2] != -1)           
            return res[n][x1][y1][x2][y2];  
        if (n == 1) 
        {
            t = calSum(x1, y1, x2, y2);           
            res[n][x1][y1][x2][y2] = t * t;           
            return t * t; 
        }
        for (a = x1; a < x2; a++) 
        {
            c = calSum(a + 1, y1, x2, y2);       
            e = calSum(x1, y1, a, y2);       
            t = min(fun(n - 1, x1, y1, a, y2) + c * c, fun(n - 1, a + 1, y1, x2, y2) + e * e);       
            if (MIN > t) MIN = t; 
        }      
        for (b = y1; b < y2; b++) 
        { 
            c = calSum(x1, b + 1, x2, y2);       
            e = calSum(x1, y1, x2, b);       
            t = min(fun(n - 1, x1, y1, x2, b) + c * c, fun(n - 1, x1, b + 1, x2, y2) + e * e);       
            if (MIN > t) MIN = t; 
        }      
        res[n][x1][y1][x2][y2] = MIN;      
        return MIN;
    }
    int main() {
        memset(sum, 0, sizeof(sum));     
        memset(res, -1, sizeof(res));   //初始化记录表  
        int n;     
        cin>>n;     
        for (int i=1; i<9; i++)         
            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*fun(n,1,1,8,8)-sum[8][8]*sum[8][8];     
        cout<<setiosflags(ios::fixed)<<setprecision(3)<<sqrt(result/(n*n))<<endl;
        return 0; 
    } 
  • 相关阅读:
    Algorithm Gossip (48) 上三角、下三角、对称矩阵
    .Algorithm Gossip (47) 多维矩阵转一维矩阵
    Algorithm Gossip (46) 稀疏矩阵存储
    Algorithm Gossip (45) 费氏搜寻法
    Algorithm Gossip (44) 插补搜寻法
    Algorithm Gossip (43) 二分搜寻法
    Algorithm Gossip (42) 循序搜寻法(使用卫兵)
    Algorithm Gossip (41) 基数排序法
    Algorithm Gossip (40) 合并排序法
    AlgorithmGossip (39) 快速排序法 ( 三 )
  • 原文地址:https://www.cnblogs.com/yun-an/p/10919946.html
Copyright © 2011-2022 走看看