九度OJ 题目1532:棋盘寻宝扩展(动态规划)

简单棋盘动态规划 fhsfh 阅读详情

题目链接:http://ac.jobdu.com/problem.php?pid=1532

现在有一个8*8的棋盘,上面放着64个不同价值的礼物,每个小的棋盘上面放置一个礼物(礼物的价值大于0小于100),一个人初始位置在棋盘的左上角,每次他只能向下或向右移动一步,并拿走对应棋盘上的礼物,结束位置在棋盘的右下角。从棋盘的左上角移动到右下角的时候的,每次他只能向下或向右移动一步,并拿走对应棋盘上的礼物,但是拿到的所有的礼物的价值之和不大于一个限定值limit,请设计一个算法请实现,使其能够获得不超过限制值limit的最大价值的礼物。


思路是枚举到每一步时候的可以达到的分数值,具体见代码:


#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <queue>
#include <memory.h>
using namespace std;
 
int dp[9][9][1009];
int a[9][9];
 
int main( )
{
    int limit;
    while ( cin >> limit )
    {
        for (int i=0; i<8; i++)
            for (int j=0; j<8; j++)
                cin >> a[i][j];
 
        memset(dp, 0, sizeof(dp));
        if (a[0][0] <= limit)dp[0][0][a[0][0]] = 1;
        for (int i=0; i<9; i++)
            for (int j=0; j<9; j++)
            {
                if (j > 0)
                {
                    for (int k=limit; k>=a[i][j]; k--)
                    {
                        dp[i][j][k] |= dp[i][j-1][k-a[i][j]];
                    }
                }
                if (i > 0)
                {
                    for (int k=limit; k>=a[i][j]; k--)
                    {
                        dp[i][j][k] |= dp[i-1][j][k-a[i][j]];
                    }
                }
            }
 
        int res = -1;
        for (int i=limit; i>=0; i--)
        {
            if (dp[7][7][i])
            {
                res = i;
                break;
            }
        }
        cout << res << endl;
    }
 
    return 0;
}
/**************************************************************
    Problem: 1532
    User: frostcake
    Language: C++
    Result: Accepted
    Time:50 ms
    Memory:1840 kb
****************************************************************/


从棋盘左上角走到右下角有多少种走法 题目: 请编写一个函数(允许增加子函数),计算n x m的棋盘格子(n为横向的格子数,m为竖向的格子数)沿着各 自边缘线从左上角走到右下角,总共有多少种走法,要求不能走回头路,即:只能往右和往下走,不能往左 和往上走。 输入描述: 输入两个正整数 输出描述: 返回结果 示例1: 输入 2 2 输出 6 解题思路: 看这个简单的4×4表格 我们发现想到达第一横行 和 第一竖列的每一个点的走... 阅读详情

相关推荐

动态规划从入门到精通(二)-棋盘类题目

题目:Leetcode 62. Unique Paths A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below). The robot can only move either down or right at any point in time. The r...

食鱼酱的博客 3117

微策略2012年校园招聘笔试题两道-动态规划和DFS练习

一、棋盘寻宝 题目描述: 现在有一个8*8的棋盘,上面放着64个价值不等的礼物,每个小的棋盘上面放置一个礼物(礼物的价值大于0小于1000),一个人的初始位置在棋盘的左上角,每次他只能向下或向右移动一步,并拿走对应棋盘上的礼物,结束位置在棋盘的右下角,请设计一个算法使其能够获得最大价值的礼物。 输入: 输入包含多个测试用例,每个测试用例共有8行8列,第i行的...

thewebcode 296

Matlab实现箭头程序

matlab实现的箭头绘制程序,已经封装成函数接口,可用于向量,力场,磁场的表示

最新笔试题

1、8*8的棋盘上面放着64个不同价值的礼物,每个小的棋盘上面放置一个礼物(礼物的价值大于0),一个人初始位置在棋盘的左上角,每次他只能向下或向右移动一步,并拿走对应棋盘上的礼物,结束位置在棋盘的右下角,请设计一个算法使其能够获得最大价值的礼物。 //经典的动态规划 //dp[i][j] 表示到棋盘位置(i,j)上可以得到的最大礼物值 //dp[i][j] = max( dp[i][j-1]...

yuanlanjun 260

如何用YOLOv5改进无人机小目标检测?MFFSODNet实战教程(附代码)

本文详细介绍了如何利用YOLOv5框架结合MFFSODNet网络结构优化无人机航拍图像中的小目标检测。通过引入微小目标预测头、多尺度特征提取模块(MSFEM)和双向密集特征金字塔网络(BDFPN)等核心技术,显著提升了小目标检测精度。文章提供了完整的代码实现方案,并展示了在农业监测、电力巡检等实际场景中的应用效果。

sre5engineer的博客 739

多模态数据集对比:MicroLens、PixelRec和Amazon在电商推荐中的表现差异

本文深入对比了三大主流多模态数据集MicroLens、PixelRec和Amazon在电商推荐中的表现差异。通过实际测试数据,分析了这些数据集在冷启动处理、模态融合、稀疏性挑战等关键场景下的真实表现,为算法工程师和研究员提供实用指南。特别关注多模态数据集的选择对推荐系统效果的影响,帮助开发者优化模型性能。

weixin_28717939的博客 418

棋盘寻宝扩展(微策略2012年校园招聘笔试题)

题目1532:棋盘寻宝扩展 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:155 解决:72 题目描述: 现在有一个8*8的棋盘,上面放着64个不同价值的礼物,每个小的棋盘上面放置一个礼物(礼物的价值大于0小于100),一个人初始位置在棋盘的左上角,每次他只能向下或向右移动一步,并拿走对应棋盘上的礼物,结束位置在棋盘的右下角。

Achiberx 2292

K-GAS解读(08)

大众kgas标准,大众及其供应商kgas

m0_51113317的博客 598

8*8的棋盘-最大价值-java刷题

现在有一个8*8的棋盘,上面放着64个价值不等的礼物,每个小的棋盘上面放置一个礼物(礼物的价值大于0小于1000),一个人的 初始位置在棋盘的左上角,每次他只能向下或向右移动一步,并拿走对应棋盘上的礼物,结束位置在棋盘的右下角,请设计一个算 法使其能够获得最大价值的礼物。 输入: 输入包含多个测试用例,每个测试用例共有8行8列,第i行的第j列的数字代表了该处棋盘上的礼物的价值,每两个数之间

NJUPT_T的博客 2328

国产数据库PolarDB-X实战:5分钟快速搭建本地开发环境(附Docker一键部署脚本)

本文提供了一份详尽的国产分布式数据库PolarDB-X本地开发环境搭建指南。通过Docker Compose一键部署脚本,开发者可在5分钟内快速启动一个功能完整的PolarDB-X实例,涵盖环境准备、脚本解析、连接验证及日常维护,极大简化了国产数据库的入门和体验流程。

weixin_29246195的博客 358

九度OJ 1532 棋盘寻宝扩展 -- 动态规划【背包问题】

题目地址:http://ac.jobdu.com/problem.php?pid=1532 题目描述: 现在有一个8*8的棋盘,上面放着64个不同价值的礼物,每个小的棋盘上面放置一个礼物(礼物的价值大于0小于100),一个人初始位置在棋盘的左上角,每次他只能向下或向右移动一步,并拿走对应棋盘上的礼物,结束位置在棋盘的右下角。从棋盘的左上角移动到右下角的时候的,每次他只能向下或向右

liushaobo的专栏 2063

Android Settings 设置Preference置灰显示

使Preference置灰显示,不能点击。先调用Preference的setEnabled(false),再调用setShouldDisableView(true); 如果使置灰项恢复正常可点击,可调用setEnabled(true)。

随风而动 1048

【算法笔记】动态规划——走棋盘

题目描述 一个棋盘有n行m列,从棋盘的左上角走到右下角,每次只能往右走一步,或者往下走一步,总共有多少种走法?

... 4422

MCP开发实战:基于openEuler系统MCP插件的开发、测试与发布全流程

MCP开发实战:基于openEuler系统MCP插件的开发、测试与发布全流程

jks212454的博客 555

软考中级-软件设计师 Python基础

软考中级-软件设计师 Python知识点整理

片言可决讼,半纸能缚龙 3643

棋盘型动态规划

过河卒 传纸条 骑士游历 数字三角形

D_HSCheng的博客 1108

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

算法之给定一个n*m的格子或棋盘,如何计算从左上角走到右下角的走法总数?(Java)

文章目录给定一个M*N的格子或棋盘,求走法总数1.题目2.例子3.解析4.递归求法,时间复杂度O(2^n)5.非递归求法,时间复杂度O(n) 给定一个M*N的格子或棋盘,求走法总数 1.题目 给定一个n*m的格子或棋盘,问从左上角走到右下角的走法总数(每次只能向右或向下移动一个方格边长的距离。 2.例子 输入:7 5 输出:792 3.解析 不管怎么走,最终都是要走 m+n 步。 4.递...

RunFromHere的博客 1万+

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

从矩阵左上角到右下角的最大值

动态规划方法实现: 动态规划三要素:最优子结构、边界、状态转移矩阵 def maxRoute(array,m,n): for _ in range(1,n): array[0][_]=array[0][_-1]+array[0][_] for _ in range(1,m): array[_][0]=array[_-1][0]+array[0][_]...

haoshan4783的博客 919

深度学习工具包 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")

棋盘-动态规划

题目 请编写一个函数(允许增加子函数),计算n x m的棋盘格子(n为横向的格子数,m为竖向的格子数)沿着各自边缘线从右下角走到左上角,总共有多少种走法,要求不能走回头路,即:只能往左和往上走,不能往右和往下走。 思路 将原文题化解为子问题,若每次只能走一步,即从第(M * N)个格走一步,只有两种走法,(N - 1, M)和(N, M - 1)。由此,可将原始问题拆解成子问题。 边界条件...

刘明的博客 1756

给定一个m*n的格子或棋盘,问从左上角走到右下角的走法总数(每次只能向右或向下移动一个方格边长的距离。

比如一个2*3的矩阵, 1 2 3 4 5 6 从1出发走到6,则可能的走法为:1 2 3 6, 1 2 5 6, 1 4 5 6共有三种。 这道题可以看成是深度优先遍历一颗树。解法为: public class MatrixTraversal { public static int getTraversal(int p, int q) { int num = 0; ...

qq_26462167的博客 1万+

给出了一个n*n的矩形,编程求从左上角到右下角的路径数(n > =2)

原文链接:点击打开链接给出了一个n*n的矩形,编程求从左上角到右下角的路径数(n &gt; =2),限制只能向右或向下移动,不能回退。例如当n=2时,有6条路径。解:一是利用数学知识,从左上角到右下角总共要走2n步,其中横向要走n步,所以总共就是C2n~n。二是利用递归实现int getTotalPath(int m, int n) { if(m == 1) ...

akenseren的博客 2738

MATLAB实现Q-Learning一维寻宝

代码参考莫烦python main.m clear clc N_STATE = 6;%开始的距离即状态 ACTIONS = {'left','right'}; EPSILON = 0.9;%贪婪策略 ALPHA = 0.1;%学习率 GAMMA = 0.9;%衰减因子 MAX_EPISODES = 13;%最大学习次数 q_table = build_q_table(N_STATE, ACTIONS); for episode = 1 : MAX_EPISODES step_counter =

qq_36534594的博客 1423

从方格左上角到右下角的走法

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bo...

ZWB626的博客 2205

【动态规划】棋盘分割问题

描述 将一个8*8的棋盘进行如下分割:将原棋盘割下一块矩形棋盘并使剩下部分也是矩形,再将剩下的部分继续如此分割,这样割了(n-1)次后,连同最后剩下的矩形棋盘共有n块矩形棋盘。(每次切割都只能沿着棋盘格子的边进行)  原棋盘上每一格有一个分值,一块矩形棋盘的总分为其所含各格分值之和。现在需要把棋盘按上述规则分割成n块矩形棋盘,并使各矩形棋盘总分的均方差最小。  均方差,其中平均值,x

MooMLu的博客 3292
上一篇: mysql 时间转换函数的用法
下一篇: 九度OJ 题目1534:数组中第K小的数字(二分解)
Jackie_Zhu
博客等级 码龄18年 343粉丝 32原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值