POJ 1191 DP+DFS棋盘分割问题
题目大意:
Description
原棋盘上每一格有一个分值,一块矩形棋盘的总分为其所含各格分值之和。现在需要把棋盘按上述规则分割成n块矩形棋盘,并使各矩形棋盘总分的均方差最小。 均方差运用动态规划,状态为int d(int k, int x1, int y1, int x2, int y2),表示左上角为(x1, y1),右下角为(x2, y2)的矩阵被切割成n块时可以达到的最小平方和。
状态转移方程为
nowstate=min( nowstate, dfs(k-1, x1, y1, a ,y2)+s[a+1, y1, x2, y2])
nowstate=min( nowstate, dfs(k-1, a+1, y1, x2, y2)+s[x1, y1, a, y2])
nowstate=min( nowstate, dfs(k-1, x1, b+1, x2, y2)+s[x1, y1, x2, b])
nowstate=min( nowstate, dfs(k-1, x1, y1, x2, b)+s[x1, b+1, x2, y2])
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
int dp[][][][][],val[][],n; int sum(int x1,int y1,int x2,int y2)
{
int ans=val[x2][y2]-val[x1-][y2]-val[x2][y1-]+val[x1-][y1-];
return ans*ans;
} int dfs(int k,int x1,int y1,int x2,int y2)
{
if(dp[k][x1][y1][x2][y2]) return dp[k][x1][y1][x2][y2];
if(k==||x1==x2||y1==y2){
dp[k][x1][y1][x2][y2]= sum(x1,y1,x2,y2);
return dp[k][x1][y1][x2][y2];
}
int nowstate=INF;
for(int i=x1;i<x2;i++){
nowstate=min(nowstate,dfs(k-,i+,y1,x2,y2)+sum(x1,y1,i,y2));
nowstate=min(nowstate,dfs(k-,x1,y1,i,y2)+sum(i+,y1,x2,y2));
}
for(int i=y1;i<y2;i++){
nowstate=min(nowstate,dfs(k-,x1,i+,x2,y2)+sum(x1,y1,x2,i));
nowstate=min(nowstate,dfs(k-,x1,y1,x2,i)+sum(x1,i+,x2,y2));
}
dp[k][x1][y1][x2][y2]=nowstate;
return nowstate;
} void change()
{
for(int i=;i<=;i++)
val[i][]=,val[][i]=; for(int i=;i<=;i++)
for(int j=;j<=;j++)
val[i][j]+=val[i-][j]+val[i][j-]-val[i-][j-];
} int main()
{
while(~scanf("%d",&n)){ memset(dp,,sizeof(dp)); for(int i=;i<=;i++)
for(int j=;j<=;j++)
scanf("%d",&val[i][j]); change(); double res;
double ave=1.0*val[][]/n;
res=sqrt((double)dfs(n,,,,)/n-ave*ave);
printf("%.3f\n", res);
}
return ;
}
POJ 1191 DP+DFS棋盘分割问题的更多相关文章
- [POJ] 1191 [LUOGU] P1436 棋盘分割
那个均方差,可以通过展开.合并Σ,发现最终只有Xi^2会对答案造成影响,其他都是定值,所以求出最小的和的平方就行. 其实这才是这题最难的部分,以下都是码农部分. f[x1][y1][x2][y2][k ...
- poj 1321 (简单DFS) 棋盘问题
题目:http://poj.org/problem?id=1321 最近状态有点down, 练练手 #include<cstdio> using namespace std; ][]; ] ...
- HDU 2517 / POJ 1191 棋盘分割 区间DP / 记忆化搜索
题目链接: 黑书 P116 HDU 2157 棋盘分割 POJ 1191 棋盘分割 分析: 枚举所有可能的切割方法. 但如果用递归的方法要加上记忆搜索, 不能会超时... 代码: #include& ...
- POJ 1191 棋盘分割 【DFS记忆化搜索经典】
题目传送门:http://poj.org/problem?id=1191 棋盘分割 Time Limit: 1000MS Memory Limit: 10000K Total Submission ...
- poj 1191 棋盘分割 动态规划
棋盘分割 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11457 Accepted: 4032 Description ...
- POJ 1191 棋盘分割
棋盘分割 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11213 Accepted: 3951 Description 将一个 ...
- POJ 1191棋盘分割问题
棋盘分割问题 题目大意,将一个棋盘分割成k-1个矩形,每个矩形都对应一个权值,让所有的权值最小求分法 很像区间DP,但是也不能说就是 我们只要想好了一个怎么变成两个,剩下的就好了,但是怎么变,就是变化 ...
- [NOI1999] 棋盘分割(推式子+dp)
http://poj.org/problem?id=1191 棋盘分割 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 156 ...
- poj1191 棋盘分割【区间DP】【记忆化搜索】
棋盘分割 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16263 Accepted: 5812 Description ...
随机推荐
- 题解报告:hdu 1176 免费馅饼(递推dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1176 Problem Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小 ...
- oracle (DBaaS) 服务介绍
转 https://oracle-base.com/articles/vm/oracle-cloud-database-as-a-service-dbaas-create-service?utm_so ...
- 安装Kube
安装Docker yum install -y docker 加速Docker DOCKER_MIRRORS="https://5md0553g.mirror.aliyuncs.com&qu ...
- 未来十年Python的前景会怎样?
转自:一位非常优秀的Python倡导者 作者:alex链接:https://www.zhihu.com/question/22112542/answer/166053516来源:知乎著作权归作者所有. ...
- AJPFX关于IO流的简单总结
IO流的分类:1.根据流的数据对象来分:高端流:所有的内存中的流都是高端流,比如:InputStreamReader 低端流:所有的外界设备中的流都是低端流,比如InputStream,Output ...
- Spring数据访问2 - 通过JDBC访问数据库
因为原生的jdbc操作太复杂,几乎都是建立连接.关闭连接和处理例外等模板式的代码,Spring对此进行了抽象——使用模板来消除样板式代码 ,JdbcTemplate承担了简化数据库访问这块的任务. 利 ...
- CF949B A Leapfrog in the Array
思路: 最终的时候,对于位置p,若p是奇数,则该位置的元素是(p + 1) / 2:若p是偶数,需要从p开始不断地迭代寻找上一次跳跃所处的位置(p = p + n - p / 2),直到p是奇数为止. ...
- addslashes,stripslashes
官方介绍: (PHP 4, PHP 5) addslashes — 使用反斜线引用字符串 返回字符串,该字符串为了数据库查询语句等的需要在某些字符前加上了反斜线.这些字符是单引号(’).双引号(”). ...
- apropos - 在 whatis 数据库中查找字符串
总览 (SYNOPSIS) apropos keyword ... 描述 (DESCRIPTION) apropos 命令在一些特定的包含系统命令的简短描述的数据库文件里查找关键字, 然后把结果送到标 ...
- 异常详细信息: System.ComponentModel.Win32Exception: 信号灯超时时间已到