当前位置:网站首页>321. Chessboard segmentation (2D interval DP)
321. Chessboard segmentation (2D interval DP)
2022-07-02 01:45:00 【seez】
321. Chessboard division - AcWing Question bank
The question :
- A matrix can be crosscut , Or side cutting , As shown in the figure
- For a chessboard cut into two parts , You can and can only select one to continue cutting down
Find the minimum variance , Due to mean square deviation X Fix ,n Fix , What we need to calculate is the area of the divided sub rectangle (xi-X)*(xi-X).
therefore , You can use the search method , Keep raising the minimum
analysis : Two dimensional interval dp, In fact, it is similar to the digital triangle model , Because segmentation may not have a linear recursive relationship , So we use the method of memory search .
- Crosscut i, In two parts [(x1,y1)~(i,y2)],[(i+1,y2)~(x2,y2)]
- Vertical cutting i, In two parts [(x1,y1)~(x2,i)],[(x2,i+1)~(x2,y2)]
initialization : All initialized to infinity , Due to the particularity of planned search , So first initialize to a negative number , Enter to reinitialize
State transition equation / State calculation :
According to the position of cross cutting and vertical cutting i Different , And which subset to continue to divide after division , It can be divided into multiple sets
Crosscut , It is divided into two subsets , Both subsets can be further divided , Another direct calculation
- dp[x1][y1][x2][y2][k]=max(dp[x1][i][x2][y2][k]+sum[x1~i][y1~y2]);
- dp[x1][y1][x2][y2][k]=max(dp[x1][y1][i+1][y2][k]+sum[i+1~]
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int N=16;
const int inf=1e9;
int s[N][N];
double dp[N][N][N][N][N];
double X;
double get(int x1,int y1,int x2,int y2)
{
int a=s[x2][y2]-s[x1-1][y2]-s[x2][y1-1]+s[x1-1][y1-1];
return (a-X)*(a-X);
}
double dfs(int x1,int y1,int x2,int y2,int k)
{
double &v=dp[x1][y1][x2][y2][k];// For simplicity , add v
if(v>=0) return v;
if(k==1) return get(x1,y1,x2,y2);
v=1e9; // For the minimum , Initialize to infinity
for(int i=x1;i<x2;i++)
{// Crosscutting subset
v=min(v,dfs(i+1,y1,x2,y2,k-1)+get(x1,y1,i,y2));
v=min(v,dfs(x1,y1,i,y2,k-1)+get(i+1,y1,x2,y2));
}
for(int i=y1;i<y2;i++)
{// Vertically cut subset
v=min(v,dfs(x1,i+1,x2,y2,k-1)+get(x1,y1,x2,i));
v=min(v,dfs(x1,y1,x2,i,k-1)+get(x1,i+1,x2,y2));
}
return v;
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=8;i++)
for(int j=1;j<=8;j++)
cin>>s[i][j];
for(int i=1;i<=8;i++)
for(int j=1;j<=8;j++)
s[i][j]+=s[i][j-1]+s[i-1][j]-s[i-1][j-1];
X=(double)s[8][8]/n;
memset(dp,-1,sizeof dp); // Because the integral may be 0, So take -1
double a=dfs(1,1,8,8,n)/n;
printf("%.3lf",sqrt(a));
return 0;
}
边栏推荐
- 浅浅了解Servlet
- Convolutional neural network (including code and corresponding diagram)
- Brief introduction to the development of mobile network
- Electronic Association C language level 1 33, odd even number judgment
- Discussion on the idea of platform construction
- Brief description of grafana of # yyds dry goods inventory # Prometheus
- This is the form of the K-line diagram (pithy formula)
- Luogu p1775 stone merger (weakened version)
- Basic concepts of machine learning
- Design and implementation of radio energy transmission system
猜你喜欢
MPLS experiment operation
6-2漏洞利用-ftp不可避免的问题
[rust web rokcet Series 1] Hello, world and get, post, put, delete
matlab 实现语音信号重采样和归一化,并播放比对效果
Unity AssetBundle subcontracting
【LeetCode 43】236. The nearest common ancestor of binary tree
New news, Wuhan Yangluo international port, filled with black technology, refreshes your understanding of the port
Learn about servlets
Have you stepped on the nine common pits in the e-commerce system?
Another programmer "deleted the library and ran away", deleted the code of the retail platform, and was sentenced to 10 months
随机推荐
SAP ui5 beginner tutorial 20 - explanation of expression binding usage of SAP ui5
[IVX junior engineer training course 10 papers to get certificates] 09 chat room production
Finally got byte offer, 25-year-old inexperienced experience in software testing, to share with you
1218 square or round
Laravel artisan 常用命令
np.where 和 torch.where 用法
Electronic Association C language level 1 33, odd even number judgment
Altium designer measure distance (ctrl+m)
Android: the kotlin language uses grendao3, a cross platform app development framework
What is AQS and its principle
开发那些事儿:如何利用Go单例模式保障流媒体高并发的安全性?
MATLAB realizes voice signal resampling and normalization, and plays the comparison effect
三分钟学会基础k线图知识
matlab 实现语音信号重采样和归一化,并播放比对效果
卷積神經網絡(包含代碼與相應圖解)
KS006基于SSM实现学生成绩管理系统
uTools
matlab 使用 audioread 、 sound 读取和播放 wav 文件
牛客网——华为题库(51~60)
Ks006 student achievement management system based on SSM