当前位置:网站首页>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;
}
边栏推荐
- Laravel artisan common commands
- Learning note 24 - multi sensor post fusion technology
- New news, Wuhan Yangluo international port, filled with black technology, refreshes your understanding of the port
- Tencent cloud techo youth dream campus trip into Wuhan University
- SAP ui5 beginner tutorial 20 - explanation of expression binding usage of SAP ui5
- 【LeetCode 43】236. The nearest common ancestor of binary tree
- Four basic strategies for migrating cloud computing workloads
- Réseau neuronal convolutif (y compris le Code et l'illustration correspondante)
- uTools
- Feature extraction and detection 16 brisk feature detection and matching
猜你喜欢

PR second training
![[Floyd] post disaster reconstruction](/img/7a/f72c7781ef148212c870a56fb9a607.jpg)
[Floyd] post disaster reconstruction

The smart Park "ZhongGuanCun No.1" subverts your understanding of the park

Raspberry pie 4B learning notes - IO communication (1-wire)

TSINGSEE青犀平台如何实现同一节点同时播放多个视频?

Should enterprises choose server free computing?

ES6 new method of string

Introduction to ffmpeg Lib

Memorabilia of domestic database in June 2022

现货黄金分析的技巧有什么呢?
随机推荐
Niuke - Huawei question bank (51~60)
Study note 2 -- definition and value of high-precision map
Finally got byte offer, 25-year-old inexperienced experience in software testing, to share with you
matlab 使用 resample 完成重采样
uTools
Ubuntu20.04 PostgreSQL 14 installation configuration record
Based on configured schedule, the given trigger will never fire
10 minutes to get started quickly composition API (setup syntax sugar writing method)
MATLAB realizes voice signal resampling and normalization, and plays the comparison effect
Automatically browse pinduoduo products
Convolutional neural network (including code and corresponding diagram)
Introduction to ffmpeg Lib
Android: the kotlin language uses grendao3, a cross platform app development framework
[IVX junior engineer training course 10 papers] 05 canvas and aircraft war game production
自动浏览拼多多商品
城市选择器组件实现原理
SQLite 3 of embedded database
What are the affordable Bluetooth headsets? Student party parity Bluetooth headset recommendation
[Chongqing Guangdong education] Tianshui Normal University universe exploration reference
GL Studio 5 installation and experience