1.poj  1579

题目链接:

http://poj.org/problem?id=1579

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=1e4+5;
int a,b,c;
int vis[20][20][20];
int dp[20][20][20];
int solve(int a,int b,int c)
{if(a<=0||b<=0||c<=0){return 1;}if(a>20||b>20||c>20){return solve(20,20,20);}if(a<b&&b<c){if(!vis[a][b][c]){dp[a][b][c]=solve(a,b,c-1)+solve(a,b-1,c-1)-solve(a,b-1,c);vis[a][b][c]=1;}return dp[a][b][c];}else{if(!vis[a][b][c]){dp[a][b][c]=solve(a-1,b,c)+solve(a-1,b-1,c)+solve(a-1,b,c-1)-solve(a-1,b-1,c-1);vis[a][b][c]=1;}return dp[a][b][c];}
}
int main()
{memset (vis,0,sizeof(vis));while(scanf("%d%d%d",&a,&b,&c)){if(a==-1&&b==-1&&c==-1) break;printf("w(%d, %d, %d) = %d\n",a,b,c,solve(a,b,c));}return 0;
}

2.hdu 1078 FatMouse and Cheese

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1078

dp[i][j]表示在i,j位置的最大数目.

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=105;
int n,k;
int a[maxn][maxn];
int vis[maxn][maxn];
int dp[maxn][maxn];
int pos[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int ans;
int Max;
int Mlocx,Mlocy;
int dfs (int locx,int locy)
{if(locx==Mlocx&&locy==Mlocy){vis[Mlocx][Mlocy]=1;return 0;}if(vis[locx][locy]){return dp[locx][locy];}for (int j=1;j<=k;j++){for (int i=0;i<4;i++){int tlocx=locx+j*pos[i][0];int tlocy=locy+j*pos[i][1];if(tlocx>=1&&tlocx<=n&&tlocy>=1&&tlocy<=n){if(a[tlocx][tlocy]>a[locx][locy]){dp[locx][locy]=max(dp[locx][locy],a[tlocx][tlocy]+dfs(tlocx,tlocy));}}}}vis[locx][locy]=1;return dp[locx][locy];
}
int main()
{while(scanf("%d%d",&n,&k)){if(n==-1||k==-1) break;ans=0;Max=0;memset(dp,0,sizeof(dp));memset (vis,0,sizeof(vis));for (int i=1;i<=n;i++){for (int j=1;j<=n;j++){scanf("%d",&a[i][j]);if(Max<a[i][j]){Max=a[i][j];Mlocx=i;Mlocy=j;}}}dfs(1,1);printf("%d\n",dp[1][1]+a[1][1]);}return 0;
}

3. poj 2111 Millenium Leapcow

题目链接:

http://poj.org/problem?id=2111

思路:

设记忆化数组dp[i][j],表示在dp[i][j]位置之后的步数,显然最大的数肯定是0,以它为终止搜索的条件。

然后进行记忆化搜索,在找到满足条件的节点的同时记下它的编号作为后继节点。

然后根据后继节点输出答案即可。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=470;
int a[maxn][maxn];
int dp[maxn][maxn];
int vis[maxn][maxn];
int Mlocx,Mlocy;
int n;
int pos[10][2]={{1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2}};
int prex[maxn][maxn];
int prey[maxn][maxn];
int dfs (int locx,int locy)
{if(vis[locx][locy]){return dp[locx][locy];}if(locx==Mlocx&&locy==Mlocy){vis[locx][locy]=1;dp[locx][locy]=0;return 0;}for (int i=0;i<8;i++){int tlocx=locx+pos[i][0];int tlocy=locy+pos[i][1];if(tlocx<=0||tlocx>n||tlocy<=0||tlocy>n) continue;if(a[tlocx][tlocy]<=a[locx][locy]) continue;dfs(tlocx,tlocy);if(dp[locx][locy]<1+dp[tlocx][tlocy]){prex[locx][locy]=tlocx;prey[locx][locy]=tlocy;dp[locx][locy]=1+dp[tlocx][tlocy];}else if(dp[locx][locy]==1+dp[tlocx][tlocy]&&prex[locx][locy]!=-1&&prey[locx][locy]!=-1&&a[prex[locx][locy]][prey[locx][locy]]>a[tlocx][tlocy]){prex[locx][locy]=tlocx;prey[locx][locy]=tlocy;}}vis[locx][locy]=1;return dp[locx][locy];
}
int main()
{while(scanf("%d",&n)!=EOF){int Max=0;memset (vis,0,sizeof(vis));memset (dp,0,sizeof(dp));memset (prex,-1,sizeof(prex));memset (prey,-1,sizeof(prey));for (int i=1;i<=n;i++){for (int j=1;j<=n;j++){scanf("%d",&a[i][j]);if(Max<a[i][j]){Max=a[i][j];Mlocx=i,Mlocy=j;}}}for (int i=1;i<=n;i++){for (int j=1;j<=n;j++){dfs(i,j);}}int Maxstep=0,stx=1,sty=1;for (int i=1;i<=n;i++){for (int j=1;j<=n;j++){if(dp[i][j]>Maxstep){Maxstep=dp[i][j];stx=i,sty=j;}else if(dp[i][j]==Maxstep){if(a[i][j]<a[stx][sty]){stx=i,sty=j;}}}}printf("%d\n",dp[stx][sty]+1);int tx=stx,ty=sty;while(tx!=-1||ty!=-1){printf("%d\n",a[tx][ty]);int ttx=tx,tty=ty;tx=prex[ttx][tty],ty=prey[ttx][tty];}}return 0;
}

4.HDU 2848 Number Cutting Game

题目:

Problem Description

Two boys boyA and boyB are playing a simple game called Number Cutting Game. Now I will introduce it to you, my friend.

At the beginning of the game, a machine will generate two numbers, N(10 < N < 10 ^ 19) and K(2 <= K <= length(N)). Then boyA and boyB are take turns to play it:

When one player receives two numbers N and K, his task is to cut N into K nonempty pieces. If length(N) < K, that is to say he can not able to cut, he loses the game. Otherwise, he adds these K pieces together and get the sum SUM. Then let N = SUM and throw N and K to another player.

We assume boyA receives the two numbers from the machine firstly and they are smart enough to choose the best strategy for himself.
Now please help boyA to calculate weather he can win if he receives N and K firstly.

Here is a sample for you N = 103, K = 2.
Fisrtly, boyA receives N = 103, K = 2. He has two ways to cut like: 1|03 => 1 + 03 = 4, or 10|3 => 10 + 3 = 13.
Then, boyB receives from boyA. If boyB receives N = 4, K = 2, he loses the game. If boyB receives N = 13 and K = 2, he has only one way to cut: 1|3 = 1 + 3 = 4, then he will win the game.
So you can see boyA will win if he choose the best strategy.

Input

No more than 1000 cases. Each contains two line, first line an integer N(10 < N < 10 ^ 19), second line K(2 <= K <= length(N)).

Output

If boyA can win, print 1, otherwise 0.

Sample Input

 

103 2

999999999999999999 2

Sample Output

 

1

0

思路:

如果选择的一方没有一种办法能够组合成题目要求的情况,则判选择的一方输,否则则继续搜索下去。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef unsigned long long ull;
const int maxn=470;
ull p10[30];
ull n,k;
int re;
int dfs (ull x,ull sum,int ci)
{if(ci<k-1&&x==0) return 0;if(ci==k-1){if(dfs(sum+x,0,0)) return 0;return 1;}for (int i=1;p10[i]<=x;i++){if(dfs(x/p10[i],sum+(x%p10[i]),ci+1)) return 1;}return 0;
}
int main()
{p10[0]=1;for (int i=1;i<=19;i++){p10[i]=p10[i-1]*10;}while(cin>>n>>k){re=-1;re=dfs(n,0,0);printf("%d\n",re);}return 0;
}

记忆化搜索例题 记忆化搜索相关推荐

  1. 深度优先搜索之记忆化dfs

    文章目录 前言 朴素dfs的求解思路 记忆化dfs的求解思路 将整数按权重排序 题目描述 解题思路 朴素dfs 记忆化dfs 小结 矩阵中的最长递增路径 题目描述 解题思路 朴素dfs 记忆化dfs ...

  2. 58 同城 iOS 客户端搜索模块组件化实践

    [编者按]58 同城 App 自从 1.0 版本开始,便已经提供了搜索功能.随着版本的迭代.业务的复杂,搜索框架也在不断受到挑战.诸如代码不能复用.耦合度高.业务功能接入成本高等问题日积月累,成为需要 ...

  3. 网页静态化和网页伪静态化之间的区别与选择

    网页静态化和网页伪静态化,如果我们是一个不懂网站的人估计听到这些词可能会比较头晕,王晟璟在刚开始接触并尝试建设自己的个人博客网站的时候也是如此,看了很多关于这方面的资料也还是云里雾里的,不过后面终于整 ...

  4. 【相关性搜索】 多字段搜索的两种方式——词中心与字段中心

    背景 首先看个例子,有两个 doc,一条是 albino elephant,一条是 elephant elephant PUT test_elephant/_doc/1 {"title_te ...

  5. Python爬虫(七)_非结构化数据与结构化数据

    页面解析与数据提取 实际上爬虫一共就四个主要步骤: 定(要知道你准备在哪个范围或者网站去搜索) 爬(将所有的网站的内容全部爬下来) 取(分析数据,去掉对我们没用处的数据) 存(按照我们想要的方式存储和 ...

  6. 【Android 插件化】Hook 插件化框架总结 ( 插件包管理 | Hook Activity 启动流程 | Hook 插件包资源加载 ) ★★★

    Android 插件化系列文章目录 [Android 插件化]插件化简介 ( 组件化与插件化 ) [Android 插件化]插件化原理 ( JVM 内存数据 | 类加载流程 ) [Android 插件 ...

  7. 【Android 插件化】Hook 插件化框架 ( Hook Activity 启动流程 | Hook 点分析 )

    Android 插件化系列文章目录 [Android 插件化]插件化简介 ( 组件化与插件化 ) [Android 插件化]插件化原理 ( JVM 内存数据 | 类加载流程 ) [Android 插件 ...

  8. 【Android 插件化】Hook 插件化框架 ( 合并 “插件包“ 与 “宿主“ 中的 Element[] dexElements | 设置合并后的 Element[] 数组 )

    Android 插件化系列文章目录 [Android 插件化]插件化简介 ( 组件化与插件化 ) [Android 插件化]插件化原理 ( JVM 内存数据 | 类加载流程 ) [Android 插件 ...

  9. 【Android 插件化】Hook 插件化框架 ( 通过反射获取 “宿主“ 应用中的 Element[] dexElements )

    Android 插件化系列文章目录 [Android 插件化]插件化简介 ( 组件化与插件化 ) [Android 插件化]插件化原理 ( JVM 内存数据 | 类加载流程 ) [Android 插件 ...

最新文章

  1. Java多线程,Thread,Runnable,Callable Task,Future<Task>,CompletionService
  2. ADO.NET Entity Framework Beta2(五)/快速入门(实体框架)
  3. ACM《数据结构》顺序表
  4. 高等数学上-赵立军-北京大学出版社-题解-练习2.3
  5. 记录工作中常用的CSS3
  6. oracle11 分配表权限,Oracle11g权限--角色
  7. linux学习笔记:磁盘挂载与卸载命令
  8. java resultSet获取总行数
  9. 《模拟电子技术基础》-(童诗白)笔记
  10. java各种异常总结
  11. 移动硬盘/U盘上装Windows 7旗舰版(VHD版)
  12. 两个PDF比较标出差异_怎样核对两份word文档内容差异?我用2小时,同事仅用2分钟搞定...
  13. 澳国立计算机录取分数,澳洲国立大学最近录取分数线及注意事项
  14. Event Loop、宏任务和微任务(动态演示)
  15. Ubuntu 挂在硬盘、内存
  16. JAVA经典算法40题(供面试所用)
  17. P2P技术软件Murder分发大文件
  18. Arduino IDE+_Attiny13/85实践(一) IED环境配置
  19. 2022年危险化学品生产单位安全生产管理人员操作证考试题库及答案
  20. FlowNet2.0 win10系统疑难问题解决

热门文章

  1. 【超分辨率】(DRN)Closed-loop Matters: Dual Regression Networks for Single Image Super-Resolution
  2. Qt 一个简单的word文档编辑器
  3. 生产者消费者模型问题
  4. libGDX的启动类和配置
  5. 设备驱动中的并发控制
  6. Keras和TensorFlow的安装配置
  7. idea如何给main函数中的args[] 字符串数组赋值
  8. 谈谈Http长连接和Keep-Alive以及Tcp的Keepalive
  9. OpenCascade学习笔记-创建一个简单的OpenCascade单文档
  10. 多元正态分布的后验采样