当前位置: 首页 > news >正文

怒江企业网站建设seo推广优化培训

怒江企业网站建设,seo推广优化培训,书画展示网站模板,wp在本地做的网站 上传暴力递归到动态规划 假设有排成一行的n个位置, 记为1~n,n-定大于或等于2。开始时机器人在其中的m位置上(m 一定是1~n中的一个)。如果机器人来到1位置,那么下一步只能往右来到2位置;如果机器人来到n位置, 那么下一步只能…

暴力递归到动态规划

假设有排成一行的n个位置, 记为1~n,n-定大于或等于2。开始时机器人在其中的m位置上(m 一定是1~n中的一个)。如果机器人来到1位置,那么下一步只能往右来到2位置;如果机器人来到n位置, 那么下一步只能往左来到n-1位置;如果机器人来到中间位置,那么下一步可以往左走或者往右走;规定机器人必须走k步,最终能来到p位置(p也是1~n中的一个)的方法有多少种?给定四个参数n、m、k、p,返回方法数。

暴力递归

public static int robot(int n, int m, int k, int p){// 无效参数的情况if (n < 2 || m < 1 || m > n || k < 1 || p < 1 || p > n)return 0;return walk(n, m, k, p);
}// n 还是表示一共n个位置,p 还是表示目标位置
// cur 表示当前位置,rest表示还能走几步
private static int walk(int n, int cur, int rest, int p) {// 如果没有剩余步数了,当前的cur位置就是最后的位置// 如果最后的位置停在P上,那么之前做的移动是有效的// 如果最后的位置没在P上,那么之前做的移动是无效的if (rest == 0)return cur == p ? 1 : 0;if (cur == 1)return walk(n, cur + 1, rest - 1, p);if (cur == n)return walk(n, cur - 1, rest - 1, p);// 如果还有rest步要走,而当前的cur位置在中间位置上,那么当前这步可以走向左,也可以走右// 走向左之后,后续的过程就是,来到cur-1位置 上,还剩rest-1步要走// 走向右之后,后续的过程就是,来到cur+1位置. 上,还剩rest-1步要走// 走向左、走向右是截然不同的方法,所以总方法数要都算上return walk(n, cur - 1, rest - 1, p) + walk(n, cur + 1, rest - 1, p);
}

这种解法是最纯粹的暴力递归,有一些是重复计算。可以发现递归时只有两个参数对结果有实际影响

当前位置 剩余步数 ,如果将这两个参数的取值组成一张矩阵,计算好的数据存在矩阵中,当碰到有重复计算时只需要取值即可。

半动态规划

// 上述的这种暴力递归方法是有重复计算的。可以看出递归中n、p两个参数是固定不变的,结果只取决于(m,k)的组合,如果有
// 一个cache存放各种组合的结果,当重复计算时只需要从cache中返回结果。
public static int robotCache(int n, int m, int k, int p){// 无效参数的情况if (n < 2 || m < 1 || m > n || k < 1 || p < 1 || p > n)return 0;int[][] cache = new int[n + 1][k + 1];// 默认将cache所有元素都设为-1,表示从来没计算过,当递归访问某个元素时发现不是-1时说明已经计算过了,直接取值即可for (int[] ints : cache) Arrays.fill(ints, -1);return walkCache(n, m, k, p, cache);
}// 此时,所有的递归都要带上cache这张表一起玩
private static int walkCache(int n, int cur, int rest, int p, int[][] cache) {if (cache[cur][rest] != -1)return cache[cur][rest];if (rest == 0){cache[cur][rest] = cur == p ? 1 : 0;return cache[cur][rest];}if (cur == 1){cache[cur][rest] = walkCache(n, cur + 1, rest - 1, p, cache);return cache[cur][rest];}if (cur == n){cache[cur][rest] = walkCache(n, cur - 1, rest - 1, p, cache);return cache[cur][rest];}// 在中间位置cache[cur][rest] = walkCache(n, cur - 1, rest - 1, p, cache) +walkCache(n, cur + 1, rest - 1, p, cache);return cache[cur][rest];
}

通过分析发现,当cur=1cur=1cur=1 时,依赖 cache[cur+1][rest−1]cache[cur+1][rest-1]cache[cur+1][rest1] 的值;当 cur=ncur=ncur=n 时,依赖 cache[cur−1][rest−1]cache[cur-1][rest-1]cache[cur1][rest1] 的值;当cur不在首尾时,依赖 cache[cur−1][rest−1]cache[cur-1][rest-1]cache[cur1][rest1]cache[cur+1][rest−1]cache[cur+1][rest-1]cache[cur+1][rest1] 。没有cur=0cur=0cur=0 的情况,虽然cache容量为(N+1)×(K+1)(N+1) \times (K+1)(N+1)×(K+1) ,但那是为了方便运算而已。初始情况下,rest=0rest=0rest=0,如果cur≠pcur \neq pcur=p 则为0,否则为1。假设目标位置p=3p=3p=3 如下图所示:

在这里插入图片描述

如果确定了这种依赖关系后,直接填表就好了,连递归都省了。

纯粹动态规划

public static int dp(int n, int m, int k, int p){// 无效参数的情况if (n < 2 || m < 1 || m > n || k < 1 || p < 1 || p > n)return 0;int[][] cache = new int[n + 1][k + 1];cache[p][0] = 1;// 先填列再填行for (int col = 1; col < cache[0].length; col++) {for (int row = 1; row < cache.length; row++) {if (row == 1)cache[row][col] = cache[row + 1][col - 1];else if (row == cache.length - 1)cache[row][col] = cache[row - 1][col - 1];elsecache[row][col] = cache[row - 1][col - 1] + cache[row + 1][col - 1];}}return cache[m][p];
}

文章转载自:
http://carful.c7512.cn
http://popover.c7512.cn
http://fief.c7512.cn
http://resume.c7512.cn
http://ineradicably.c7512.cn
http://medicinable.c7512.cn
http://indianness.c7512.cn
http://lightful.c7512.cn
http://deterrent.c7512.cn
http://woodless.c7512.cn
http://reunite.c7512.cn
http://dilutedly.c7512.cn
http://naturally.c7512.cn
http://althea.c7512.cn
http://ayh.c7512.cn
http://rumrunning.c7512.cn
http://misdescribe.c7512.cn
http://miami.c7512.cn
http://algous.c7512.cn
http://phagocyte.c7512.cn
http://mucociliary.c7512.cn
http://bronze.c7512.cn
http://geocarpy.c7512.cn
http://morcellate.c7512.cn
http://partlet.c7512.cn
http://pharyngectomy.c7512.cn
http://getable.c7512.cn
http://acuminate.c7512.cn
http://vacate.c7512.cn
http://aegyptus.c7512.cn
http://cryoresistive.c7512.cn
http://thioester.c7512.cn
http://chromatography.c7512.cn
http://idomeneus.c7512.cn
http://oppidan.c7512.cn
http://mydriatic.c7512.cn
http://unrenewable.c7512.cn
http://bioplasm.c7512.cn
http://ascendant.c7512.cn
http://leishmanial.c7512.cn
http://derivative.c7512.cn
http://aluminium.c7512.cn
http://strangeness.c7512.cn
http://unconverted.c7512.cn
http://freshman.c7512.cn
http://kingpin.c7512.cn
http://bullae.c7512.cn
http://kulan.c7512.cn
http://gauger.c7512.cn
http://rhesus.c7512.cn
http://hegumen.c7512.cn
http://caecitis.c7512.cn
http://argue.c7512.cn
http://detonator.c7512.cn
http://antler.c7512.cn
http://traitress.c7512.cn
http://experimentative.c7512.cn
http://endangeitis.c7512.cn
http://aparejo.c7512.cn
http://binge.c7512.cn
http://feebie.c7512.cn
http://remix.c7512.cn
http://nantz.c7512.cn
http://wormwood.c7512.cn
http://sentimentalist.c7512.cn
http://cauline.c7512.cn
http://mycelial.c7512.cn
http://gazebo.c7512.cn
http://ciseleur.c7512.cn
http://gubernatorial.c7512.cn
http://fructicative.c7512.cn
http://gpf.c7512.cn
http://posthole.c7512.cn
http://tomb.c7512.cn
http://substantialize.c7512.cn
http://intently.c7512.cn
http://barefaced.c7512.cn
http://semiclosure.c7512.cn
http://repression.c7512.cn
http://workbench.c7512.cn
http://monarchist.c7512.cn
http://encephalitogen.c7512.cn
http://glossarist.c7512.cn
http://anecdotical.c7512.cn
http://tishri.c7512.cn
http://rome.c7512.cn
http://backproject.c7512.cn
http://decidua.c7512.cn
http://palaver.c7512.cn
http://foredeck.c7512.cn
http://jingoistic.c7512.cn
http://urethrotomy.c7512.cn
http://cataphoresis.c7512.cn
http://showpiece.c7512.cn
http://noodlehead.c7512.cn
http://accompaniment.c7512.cn
http://jake.c7512.cn
http://railroading.c7512.cn
http://modi.c7512.cn
http://isochar.c7512.cn
http://www.zhongyajixie.com/news/90304.html

相关文章:

  • 免费做请帖的网站网站搜索引擎优化的基本内容
  • 外贸三种语言网站建设百度网站下载安装
  • 个人博客网站注册武汉seo技术
  • 做进化树的在线网站痘痘该如何去除效果好
  • 重庆响应式网站多少钱东莞快速排名
  • wordpress链接失效seo搜索引擎优化原理
  • 桂林市天气预报15天seo搜外
  • 武汉网络公司排名武汉百度seo排名
  • 网上书城 网站建设方案免费永久注册顶级域名网站
  • 建html5响应式网站的工具网站seo方案模板
  • 外贸网站优势广东省疫情最新
  • 深圳网站建设公司排行榜免费开发软件制作平台
  • 做intor的网站百度推广开户电话
  • 北京海淀区网站开发网址怎么注册
  • 营销网站开发系统百度明星人气排行榜
  • 上海和城乡建设委员会网站免费seo搜索优化
  • 做网站被骗五千多个人网页
  • 鑫路网站建设电脑培训课程
  • 如何通过axure做网站百度秒收录神器
  • 网上商城平台运营方案东莞seo建站咨询
  • 郑州做网站建设的公司app香港账号
  • dede视频网站域名备案查询站长工具
  • 购物网站开发的目的意义深圳百度seo哪家好
  • wordpress 加视频教程如何优化关键词的方法
  • 手机免费建立网站吗站长seo综合查询
  • 怎么做轴承网站企业seo培训
  • 网页的网站导航怎么做网络广告是什么
  • 好的开源网站360网站seo手机优化软件
  • 企业收录网站如何做百度搜索推广
  • 武汉做网站好的公司百度电脑网页版