标签:范围 ret mat 整数 一个 lan names 技术分享 小数点
题目传送门:http://poj.org/problem?id=1191
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 16150 | Accepted: 5768 |
Sample Input
3 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 3
Sample Output
1.633
Source
1 ///POJ 1191 棋盘分割 (记忆化搜索经典) 2 #include <cstdio> 3 #include <iostream> 4 #include <algorithm> 5 #include <cstring> 6 #include <cmath> 7 #define INF 0x3f3f3f3f 8 #define ll long long int 9 #define mod 1000000007 10 using namespace std; 11 12 const int MAXN = 15; 13 const int MAXM = 10; 14 double d[MAXN][MAXM][MAXM][MAXM][MAXM]; 15 double record[MAXM][MAXM][MAXM][MAXM]; 16 double mmp[MAXM][MAXM]; 17 double sum, ave; 18 int N; 19 20 double get_sum(int x1, int y1, int x2, int y2) 21 { 22 if(record[x1][y1][x2][y2]>=0) return record[x1][y1][x2][y2]; 23 double re = 0; 24 for(int i = x1; i <= x2; i++) 25 for(int j = y1; j <= y2; j++) 26 re+=mmp[i][j]; 27 record[x1][y1][x2][y2] = re*re; 28 return record[x1][y1][x2][y2]; 29 } 30 31 double dfs(int x1, int y1, int x2, int y2, int cnt) 32 { 33 if(d[cnt][x1][y1][x2][y2]>=0) return d[cnt][x1][y1][x2][y2]; 34 if(cnt == N) 35 { 36 return get_sum(x1, y1, x2, y2); 37 } 38 double min_sum = 99999999; 39 double tp = 0; 40 for(int i = x1; i < x2; i++) 41 { 42 tp = get_sum(x1, y1, i, y2) + dfs(i+1, y1, x2, y2, cnt+1); 43 if(min_sum > tp) min_sum = tp; 44 tp = get_sum(i+1, y1, x2, y2) + dfs(x1, y1, i, y2, cnt+1); 45 if(min_sum > tp) min_sum = tp; 46 } 47 for(int j = y1; j < y2; j++) 48 { 49 tp = get_sum(x1, y1, x2, j) + dfs(x1, j+1, x2, y2, cnt+1); 50 if(min_sum > tp) min_sum = tp; 51 tp = get_sum(x1, j+1, x2, y2) + dfs(x1, y1, x2, j, cnt+1); 52 if(min_sum > tp) min_sum = tp; 53 } 54 d[cnt][x1][y1][x2][y2] = min_sum; 55 return min_sum; 56 } 57 58 int main() 59 { 60 scanf("%d", &N); 61 memset(d, -1, sizeof(d)); 62 memset(record, -1, sizeof(record)); 63 for(int i = 1; i <= 8; i++) 64 for(int j = 1; j <= 8; j++) 65 { 66 scanf("%lf", &mmp[i][j]); 67 sum+=mmp[i][j]; 68 } 69 ave = sum/(N*1.0); 70 ave*=ave; 71 double res = dfs(1, 1, 8, 8, 1); 72 double ans = sqrt(res/N-ave); 73 printf("%.3f\n", ans); 74 return 0; 75 }
标签:范围 ret mat 整数 一个 lan names 技术分享 小数点
原文地址:https://www.cnblogs.com/ymzjj/p/9496694.html