POJ 1191 棋盘分割 【DFS记忆化搜索经典】
题目传送门:http://poj.org/problem?id=1191
棋盘分割
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 16150 | Accepted: 5768 |
Description

原棋盘上每一格有一个分值,一块矩形棋盘的总分为其所含各格分值之和。现在需要把棋盘按上述规则分割成n块矩形棋盘,并使各矩形棋盘总分的均方差最小。
均方差


xi为第i块矩形棋盘的总分。
请编程对给出的棋盘及n,求出O'的最小值。
Input
第2行至第9行每行为8个小于100的非负整数,表示棋盘上相应格子的分值。每行相邻两数之间用一个空格分隔。
Output
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
题意概括:
解题思路:
///POJ 1191 棋盘分割 (记忆化搜索经典)
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#define INF 0x3f3f3f3f
#define ll long long int
#define mod 1000000007
using namespace std; const int MAXN = ;
const int MAXM = ;
double d[MAXN][MAXM][MAXM][MAXM][MAXM];
double record[MAXM][MAXM][MAXM][MAXM];
double mmp[MAXM][MAXM];
double sum, ave;
int N; double get_sum(int x1, int y1, int x2, int y2)
{
if(record[x1][y1][x2][y2]>=) return record[x1][y1][x2][y2];
double re = ;
for(int i = x1; i <= x2; i++)
for(int j = y1; j <= y2; j++)
re+=mmp[i][j];
record[x1][y1][x2][y2] = re*re;
return record[x1][y1][x2][y2];
} double dfs(int x1, int y1, int x2, int y2, int cnt)
{
if(d[cnt][x1][y1][x2][y2]>=) return d[cnt][x1][y1][x2][y2];
if(cnt == N)
{
return get_sum(x1, y1, x2, y2);
}
double min_sum = ;
double tp = ;
for(int i = x1; i < x2; i++)
{
tp = get_sum(x1, y1, i, y2) + dfs(i+, y1, x2, y2, cnt+);
if(min_sum > tp) min_sum = tp;
tp = get_sum(i+, y1, x2, y2) + dfs(x1, y1, i, y2, cnt+);
if(min_sum > tp) min_sum = tp;
}
for(int j = y1; j < y2; j++)
{
tp = get_sum(x1, y1, x2, j) + dfs(x1, j+, x2, y2, cnt+);
if(min_sum > tp) min_sum = tp;
tp = get_sum(x1, j+, x2, y2) + dfs(x1, y1, x2, j, cnt+);
if(min_sum > tp) min_sum = tp;
}
d[cnt][x1][y1][x2][y2] = min_sum;
return min_sum;
} int main()
{
scanf("%d", &N);
memset(d, -, sizeof(d));
memset(record, -, sizeof(record));
for(int i = ; i <= ; i++)
for(int j = ; j <= ; j++)
{
scanf("%lf", &mmp[i][j]);
sum+=mmp[i][j];
}
ave = sum/(N*1.0);
ave*=ave;
double res = dfs(, , , , );
double ans = sqrt(res/N-ave);
printf("%.3f\n", ans);
return ;
}
POJ 1191 棋盘分割 【DFS记忆化搜索经典】的更多相关文章
- poj 1191 棋盘分割(dp + 记忆化搜索)
题目:http://poj.org/problem?id=1191 黑书116页的例题 将方差公式化简之后就是 每一块和的平方 相加/n , 减去平均值的平方. 可以看出来 方差只与 每一块的和的平方 ...
- HDU 2517 / POJ 1191 棋盘分割 区间DP / 记忆化搜索
题目链接: 黑书 P116 HDU 2157 棋盘分割 POJ 1191 棋盘分割 分析: 枚举所有可能的切割方法. 但如果用递归的方法要加上记忆搜索, 不能会超时... 代码: #include& ...
- poj 3249(bfs+dp或者记忆化搜索)
题目链接:http://poj.org/problem?id=3249 思路:dp[i]表示到点i的最大收益,初始化为-inf,然后从入度为0点开始bfs就可以了,一开始一直TLE,然后优化了好久才4 ...
- 不要62 hdu 2089 dfs记忆化搜索
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意: 给你两个数作为一个闭区间的端点,求出该区间中不包含数字4和62的数的个数 思路: 数位dp中 ...
- dfs+记忆化搜索,求任意两点之间的最长路径
C.Coolest Ski Route 题意:n个点,m条边组成的有向图,求任意两点之间的最长路径 dfs记忆化搜索 #include<iostream> #include<stri ...
- POJ 2704 Pascal's Travels 【DFS记忆化搜索】
题目传送门:http://poj.org/problem?id=2704 Pascal's Travels Time Limit: 1000MS Memory Limit: 65536K Tota ...
- POJ 1088 滑雪 DFS 记忆化搜索
http://poj.org/problem?id=1088 校运会放假继续来水一发^ ^ 不过又要各种复习,功课拉下了许多 QAQ. 还有呀,就是昨天被一个学姐教育了一番,太感谢了,嘻嘻^ ^ 好了 ...
- poj1088-滑雪 【dfs 记忆化搜索】
http://poj.org/problem?id=1088 滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 79806 ...
- BZOJ1048:[HAOI2007]分割矩阵(记忆化搜索DP)
Description 将一个a*b的数字矩阵进行如下分割:将原矩阵沿某一条直线分割成两个矩阵,再将生成的两个矩阵继续如此分割(当然也可以只分割其中的一个), 这样分割了(n-1)次后,原矩阵被分割成 ...
随机推荐
- python 爬虫系列07-天气爬虫
看天气 import requests from bs4 import BeautifulSoup ALL_DATA = [] def parse_page(url): headers = { 'Us ...
- 4GLTE@NB-IOT
参考:https://www.cnblogs.com/pangguoming/p/9755916.html NB-IOT特点:在4G基础上发展而来,覆盖广,海量接入,成本低低功耗:不适合应用情况:大数 ...
- innosetup区分正常状态和静默安装状态(通过传递的参数)
命令行运行程序,如: myprogram.exe /abc /bcd 如果我们想获取其中的参数,“/abc”.“/bcd” 1. 直接使用innosetup自带的方法, GetCmdTail() ...
- Become a Better Programmer: 5 Essential Methods at a Glance--reference
http://www.git-tower.com/blog/become-a-better-programmer-5-essentials/ Become a Better Programmer: 5 ...
- js消息提示框插件-----toastr用法
(本文系转载) 因为个人项目中有一个提交表单成功弹出框的需求,从网上找了一些资料,发现toastr这个插件的样式还是不错的.所以也给大家推荐下,但是网上的使用资料不是很详细,所以整理了一下,希望能给 ...
- ul+js模拟select
html css .select_box{ float: left; } .select_box input{ width: 160px; height: 30px; text-align: ce ...
- UML建模—EA的使用起步
Enterprise Architect(EA) 是一个功能比较强悍的建模工具. 对于一个软件设计者来说,从需求分析到业务设计.类模型设计.数据库设计到测试.发布.部署等一系列软件设计必须的操作都可以 ...
- Windows Composition API 指南 - 认识 Composition API
微软在 Windows 10中 面向通用 Windows 应用 (Universal Windows Apps, UWA) 新引入了一套用于用户界面合成的 API:Composition API.Co ...
- C#自定义控件 在 Toolbox显示不了的问题
1) Close your solution2) Tools->Options->"Windows Form Designer" - find AutoToolboxP ...
- 【Linux】安装配置JDK1.8
第一步:下载Linux环境下的jdk1.8,请去(官网)中下载jdk的安装文件: https://www.oracle.com/technetwork/java/javase/downloads/in ...