POJ 1191

poj 1191 棋盘分割(记忆化搜索/动态规划) 程序设计实习递归练习 棋盘分割 阅读详情

这哪里是动规 明明是暴搜。

万恶的黑皮书。。。。。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=16;
const int M=8;
const int inf=1<<30;
int ans[maxn][maxn][maxn][maxn][maxn];
bool flag[maxn][maxn][maxn][maxn][maxn];
int s[maxn][maxn][maxn][maxn];
int ar[maxn][maxn];

int doubp(int a)
{
	return a*a;
}

int dp(int k,int x1,int y1,int x2,int y2)
{
	if(flag[k][x1][y1][x2][y2])
	{
		return ans[k][x1][y1][x2][y2];
	}
	int i,sum=inf;
	for(i=x1;i<x2;i++)
	{
		sum=min(sum,min(dp(k-1,x1,y1,i,y2)+doubp(s[i+1][y1][x2][y2]), dp(k-1,i+1,y1,x2,y2)+doubp(s[x1][y1][i][y2])));
	}
	for(i=y1;i<y2;i++)
	{
		sum=min(sum,min(dp(k-1,x1,y1,x2,i)+doubp(s[x1][i+1][x2][y2]), dp(k-1,x1,i+1,x2,y2)+doubp(s[x1][y1][x2][i])));
	}
	ans[k][x1][y1][x2][y2]=sum;
	flag[k][x1][y1][x2][y2]=true;
	return sum;
}

int main()
{
	int n;
	while(~scanf("%d",&n))
	{
		memset(s,0,sizeof(s));
		memset(ans,-1,sizeof(ans));
		memset(flag,0,sizeof(flag));
		int i,j,k,l,p,sum=0;
		for(i=1;i<=M;i++)
		{
			for(j=1;j<=M;j++)
			{
				scanf("%d",&ar[i][j]);
				s[i][j][i][j]=ar[i][j];
				sum+=ar[i][j];
			}
		}
		for(i=1;i<=M;i++)
		{
			for(j=1;j<=M;j++)
			{
				for(k=i;k<=M;k++)
				{
					for(l=j;l<=M;l++)
					{
						s[i][j][k][l]=s[i][j][k-1][l]+s[i][j][k][l-1]-s[i][j][k-1][l-1]
						               +s[k][l][k][l];
						ans[1][i][j][k][l]=doubp(s[i][j][k][l]);
						flag[1][i][j][k][l]=true;
					}
				}
			}
		}
		dp(n,1,1,M,M);
		double dn=n;
		printf("%.3lf\n",sqrt(ans[n][1][1][M][M]/dn-(sum/dn)*(sum/dn)));
	}
	return 0;
}



C++ 递归:小游戏(POJ 2802),棋盘分割(POJ 1191) 递归 小游戏 #include<iostream> #include<algorithm> #include<cstring> #include<stdio.h> using namespace std; #define maxin 75 char board[maxin+2][maxin+2]; bool mark[maxin+2][maxin+2... 阅读详情

相关推荐

51单片机也能玩转信号发生器?手把手教你用DAC0832和LM358输出四种标准波形

本文详细介绍了如何使用51单片机和DAC0832芯片搭配LM358运放,自制低成本信号发生器,实现正弦波、矩形波、三角波和锯齿波四种标准波形的输出。从硬件选型、电路设计到编程实现,手把手教你完成这一实用电子项目,总成本不到50元,适合电子爱好者和学生实践。

weixin_30530939的博客 553

POJ1191棋盘分割 NYOJ87

另外分享朋友们一起开发的oj:该题地址:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=87/*POJ1191棋盘分隔,刘汝佳黑书面有解析,大概思路如下: * 根据方差公式推倒出如果需要方差最小,则需要每个被减数的平方最小,即每块棋盘的总分平方和最小 * 考虑左上角坐标为(x1,y1),右下角坐标为(x2,y2)的棋盘,设它的总和为sum[x1,y1,x2,y2]切割k次以后得到 * k+1块矩形的总分平方和最小值为d[k,x1,y1,x2

三江小渡的专栏 656

探索niri:让你的Linux桌面布局无拘无束

niri是一款基于Wayland的滚动平铺合成器,采用创新的窗口管理方式。它将窗口排列为向右无限延伸的条目,支持独立显示器布局和动态垂直工作区,确保多任务操作流畅性。核心功能包括概要视图、截图界面、屏幕投射、手势操作、窗口分组等,并支持自定义布局和动画效果。niri具备良好的稳定性,兼容xwayland-satellite工具,灵感来自PaperWM但突破了GNOME限制。该项目为追求高效桌面管理的用户提供了全新解决方案,欢迎加入社区交流体验。

weixin_44626085的博客 1818

POJ 1191 棋盘分割【区间类DP】

http://poj.org/problem?id=1191 1>根据公式化简: 其中后者是一个已知数。求均方差的最小值就是求出个个棋盘内各数值的平方和最小值。 2>棋盘分割分四种情况: 竖切(左不动),竖切(右不动),横切(上不动),横切(下不动); 3>状态转移方程: f(i,x1,y1,x2,y2)表示以(x1,y1),(x2,y2)为...

dianxuanzhi1019的博客 135

Excel中如何快速地将成绩按比例来划分为等级?

问题介绍: 记录说明:班级里共有40个人,按最终总平均成绩从高到低划分等级,A等级30%,B等级50%,C等级20%。 部分成绩如下表所示: Excel中相关函数介绍: IF函数 IF函数有三个参数,语法如下: =IF(条件判断, 结果为真返回值, 结果为假返回值) 第一参数是条件判断,比如说“A1=">37”这种,判断TRUE或FALSE。如果判断是TRUE(结果为真)那么IF函数返回值是第二参数,否则(结果为假)返回第三参数。 如果省略了第三参数,则当结果为假时,会返回FALSE。 如果公式

Wendy的博客 1万+

均匀分布产生高斯分布

均匀分布产生高斯分布 文章目录均匀分布产生高斯分布简介方法和证明代码效果引用 简介 方法和证明 代码 效果 引用

gy_Rick的博客 5317

POJ 1191 棋盘分割

题目连接:http://poj.org/problem?id=1191 解题思路: 棋盘分割一块之后,剩余的棋盘的分割和原问题类似,只是规模变小了。棋盘分割问题可以采用动态规划方法解决。 对方差公式进行化解,得到σ^2=1/n∑xi^2 - x^2 可知,要使方差最小,只需使∑xi^2最小即可,即各块分值平方和最小。平均值 x是个固定的数,跟分割的方式没有关系, 首先定义状态:dp[k]

路漫漫其修远兮,吾将上下而求索 4531

Virtualbox “Kernel driver not installed (rc=-1908)”问题全面解析与修复指南

本文全面解析了Virtualbox出现'Kernel driver not installed (rc=-1908)'错误的原因及修复方法。详细介绍了如何通过重新安装virtualbox-dkms、处理Secure Boot特殊情况等步骤解决该问题,并提供了预防措施与最佳实践,帮助用户避免类似错误再次发生。

weixin_30352645的博客 531

poj 1191 普通dp

<br /><br /> <br /> <br />poj 1191 开始想的时候没有思路,主要是想有没有什么除了枚举的方法,结果觉得除了暴力好像其他的都特别困难,后来突然想到暴力还是可以的,其实这也是一种dp,就是按照横边,和纵边来枚举,不过有一点没有想到的就是 dp是三维的 要记录在剩下矩形里面可以切k刀的的最小方差是多少。自己没有想到dp 那个k刀 的状态是不一样的。这里错了好久。自认为表示 剩下矩形可以构成的最小方差值就可以了。

mindmb的专栏 521

【基于 CasADi 的欠驱动无人船实时非线性模型预测控制(NMPC)实现 Visual Studio 2022 C++】

本文介绍了基于CasADi和IPOPT在Visual Studio 2022 C++环境下实现的欠驱动无人船实时非线性模型预测控制(NMPC)方案。文章详细阐述了NMPC基本原理、欠驱动无人船动力学模型(包含状态变量和控制变量定义),并给出了C++实现的核心代码框架。该方案通过显式引入系统动力学模型和约束条件,在每个采样周期滚动优化控制序列,有效解决了欠驱动无人船的轨迹跟踪与避碰问题。文中还提供了VS2022配置CasADi环境的详细步骤,包括预编译包下载、工程属性设置等关键环节,为工程实现提供了完整的技术

vor234的博客 654

基于百度地图API在AI Studio上的卫星地图块图像处理与分类

本项目基于百度地图API获取了不同的瓦片地图并进行合并等处理,可用于遥感和抽象地图的地图块的图像分类、分割、检测等数据的制作。瓦片地图金字塔模型是一种多分辨率层次模型,从瓦片金字塔的底层到顶层,分辨率越来越低,但表示的地理范围不变。首先确定地图服务平台所要提供的缩放级别的数量N,把缩放级别最高、地图比例尺最大的地图图片作为金字塔的底层,即第0层,并对其进行分块,从地图图片的左上角开始,从左至右、从上到下进行切割,分割成相同大小(比如256x256像素)的正方形地图瓦片,形成第0层瓦片矩阵;

斌擎科技 747

poj 1191棋盘分割

题目地址:http://poj.org/problem?id=1191 要求最小的方差,先用平方差公式:σ ^2 = ∑(xi)^ -n*x .  x为平均值.这样,求最小的平分和就行了. 用记忆化搜索搞定,有个down和left搞反了害我调试了半天.后来一段一段代码注释掉才发现问题. #include #include #include #include #define

yunfanfighting的专栏 578

A-Frame引擎开发:A-Frame基础入门_(11).A-Frame物理引擎集成

在上一节中,我们学习了如何在A-Frame中创建基本的3D场景和交互元素。然而,为了使虚拟现实游戏更具真实感和互动性,物理引擎的集成是必不可少的。物理引擎可以模拟现实世界中的物理行为,如重力、碰撞、摩擦等,从而增强用户的沉浸感。物理引擎在虚拟现实游戏中的重要性不言而喻。它不仅能够模拟物体的运动和相互作用,还能为游戏提供更真实的反馈和体验。例如,当玩家在虚拟世界中推动一个物体时,物理引擎可以确保物体按照预期的方式移动,而不是简单地穿墙而过或突然消失。重力是物理引擎中最基本的特性之一。在A-Frame中,我们可

chenlz2007的博客 798

棋盘分割 (POJ - 1191,二维区间 DP)

一.题目链接: POJ-1191 二.题目大意: 中文题... 三.分析: 卧艹,这尼玛也太暴力了! 大佬讲解1 大佬讲解2 一开始没想明白状态表示,是我太菜. dp[i][x1][y1][x2][y2] 表示 将区域 (x1, y1, x2, y2) 切 i 次后的子矩形平方和最小值. 搞清状态表示后,状态转移就很好写啦. 详见代码(没错,我就是懒得讲) 四.代码实现:...

The___Flash的博客 420

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

POJ1191棋盘分割

题目:http://poj.org/problem?id=1191 1.分析式子!!!   发现xba是定值,σ的大小仅和∑ xi^2 有关。故dp条件是平方和最小。 2.分出一块就像割掉一条,只需枚举从四个方向割+割多宽。 (赋初值之小优美) #include<iostream> #include<cstdio> #include<cstring&...

weixin_33950035的博客 135

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

poj-1191-棋盘分割

题目地址http://poj.org/problem?id=1191题目大意 解题思路 Code #include <stdio.h> #include <iostream> #include <stdlib.h> #include <string.h> #include <queue> #include <map> #include <vector> #include <math.h> #

todo 405

poj 1191 棋盘分割

http://poj.org/problem?id=1191 DP 一不小心开了个五维数组 double ans[k][x1][y1][x2][y2] 表示(x1,y1)到(x2,y2)可以割k次时与平均数最小平方和 代码: #include<iostream> #include<cstdio> #include<cstring> #incl...

weixin_30390075的博客 72

POJ 1191 棋盘分割 【DFS记忆化搜索经典】

题目传送门:http://poj.org/problem?id=1191 棋盘分割 Time Limit:1000MS Memory Limit:10000K Total Submissions:16150 Accepted:5768 Description 将一个8*8的棋盘进行如下分割:将原棋盘割下一块矩形棋盘并使剩下部...

weixin_30559481的博客 145

poj1191(記憶化搜索)

題目鏈接:http://poj.org/problem?id=1191 題意:中文題誒~ 思路:這道題有幾個關鍵點需要想通,不然會比較難做... 首先,題目給出的標準差公式並不是很好計算,需要給它變下形: ans = sqrt(sum(xi-x)^2/n) (其中x爲平均面積,xi爲某個矩形面積... ans^2 = sum(xi-x)^2/n    = sum(xi^2)...

weixin_30527323的博客 118

poj1191 2010.4.14

poj1191 2010.4.14 详解见黑书 #include #include #include #include using namespace std; #define MAXN 9 #define MAXT 16 #define M 8 #define MAX 9999999 int sum[MAXN][MAXN]; //sum[i][j],记录以 i行j列 为

Batman_的博客 358

poj1191 棋盘分割。 dp

连接:http://poj.org/problem?id=1191 思路:额,其实就是直接搞记录一下就可以了。 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <algorithm> 5 #include <st...

weixin_30672295的博客 113

poj 1191 棋盘分割(dp,黑书dp例二)

poj 1191 棋盘分割 状态转移方程来自lrjhei'shu

yjCola 612

【动态规划+递归】:poj1191,棋盘分割

http://poj.org/problem?id=1191 明白几点: 1)最终的均方差可以转变成:sqrt( sigma[ i:1-m ]( xi*xi ) / n - ( sum(all) / n )*( sum(all) / n ) ),不懂这个转换的需要补一下数学基础;所以最终决定最小值的是sigma[ i:1-m ]( xi*xi )。 2)动归含义: dp

mmc2015的专栏 700

【poj 1191】棋盘分割 题解&代码(C++)

poj dp

曾经追梦的happy时光 1112
上一篇: POJ1141 动态规划
下一篇: POJ 1390 方块消除
MakingMaker
博客等级 码龄15年 6粉丝 108原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值