【DP】 POJ 1191 棋盘分割 记忆化搜索
思路:黑书
递归割x边或y边 取上或取下
四个选择
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#define cler(arr, val) memset(arr, val, sizeof(arr))
#define IN freopen ("in.txt" , "r" , stdin);
#define OUT freopen ("out.txt" , "w" , stdout);
typedef long long LL;
const int MAXN = 111;//点数的最大值
const int MAXM = 20006;//边数的最大值
const int INF = 11521204;
const int mod=1000000007;
int s[9][9][9][9],dp[17][9][9][9][9];
int find(int k,int x1,int y1,int x2,int y2)
{
if(dp[k][x1][y1][x2][y2]!=-1) return dp[k][x1][y1][x2][y2];
int ans = 999999999;
for(int i=x1; i<x2; i++)
for(int j=y1; j<y2; j++)
ans=min(ans,min(min(find(k-1,x1,y1,i,y2)+s[i+1][y1][x2][y2],find(k-1,i+1,y1,x2,y2)+s[x1][y1][i][y2]),min(find(k-1,x1,y1,x2,j)+s[x1][j+1][x2][y2],find(k-1,x1,j+1,x2,y2)+s[x1][y1][x2][j])));
return dp[k][x1][y1][x2][y2]=ans;
}
int main()
{
int n,m;
int mp[9][9];
//IN;
while(scanf("%d",&n)!=EOF)
{
double ans=0;
cler(dp,-1);
for(int i=1; i<=8; i++)
for(int j=1; j<=8; j++)
{
scanf("%d",&mp[i][j]);
ans+=mp[i][j];
}
for(int x1=1; x1<=8; x1++)
for(int y1=1; y1<=8; y1++)
for(int x2=x1; x2<=8; x2++)
for(int y2=y1; y2<=8; y2++)
{
for(int i=x1; i<=x2; i++)
for(int j=y1; j<=y2; j++)
s[x1][y1][x2][y2]+=mp[i][j];
s[x1][y1][x2][y2]*=s[x1][y1][x2][y2];
dp[1][x1][y1][x2][y2]=s[x1][y1][x2][y2];
}
find(n,1,1,8,8);
ans=dp[n][1][1][8][8]*1.0/n-ans*ans*1.0/n/n;
printf("%.3lf\n",sqrt(ans));
}
return 0;
}

浙公网安备 33010602011771号