HDU 1208 Pascal's Travels 经典 跳格子的方案数 (dp或者记忆化搜索)
Description
Consider the 4 x 4 board shown in Figure 1, where the solid circle identifies the start position and the dashed circle identifies the target. Figure 2 shows the three paths from the start to the target, with the irrelevant numbers in each removed.

Figure 1

Figure 2
Input
Output
Sample Input
2331
1213
1231
3110
4
3332
1213
1232
2120
5
11101
01111
11111
11101
11101
-1
Sample Output
0
7
dp[i+a[i][j]][j]+=dp[i][j];
if(j+a[i][j]<=n)
dp[i][j+a[i][j]]+=dp[i][j];
#include <iostream>
#include <cstdio>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<memory>
#include<queue>
#include<vector>
using namespace std;
#define max_v 40
__int64 dp[max_v][max_v];//dp[i][j] 从1,1到i,j的方案数
int a[max_v][max_v];
char s[max_v];
int main()
{
int n;
while(~scanf("%d",&n))
{
if(n==-)
break;
for(int i=;i<=n;i++)
{
scanf("%s",s);
int l=strlen(s);
int k=;
for(int j=;j<l;j++)
{
a[i][k++]=s[j]-'';
}
}
memset(dp,,sizeof(dp));
dp[][]=;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(a[i][j]==)
continue;
if(i+a[i][j]<=n)
dp[i+a[i][j]][j]+=dp[i][j];
if(j+a[i][j]<=n)
dp[i][j+a[i][j]]+=dp[i][j];
}
}
printf("%I64d\n",dp[n][n]);
}
return ;
}
思路二:
记忆化搜索
注意这里dp的含义和上面dp的含义不同
这里的dp:dp[i][j]代表从i,j出发的方案数
#include <iostream>
#include <cstdio>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<memory>
#include<queue>
#include<vector>
using namespace std;
#define max_v 40
__int64 dp[max_v][max_v];//dp[i][j] 从i,j出发的方案数
int a[max_v][max_v];
char s[max_v];
int n;
__int64 dfs(int x,int y)
{
if(dp[x][y]>)//记忆化搜索
return dp[x][y];
if(x==n&&y==n)//搜到终点
return ;
int xx,yy;
if(a[x][y]==)//不能跳的点
return ;
for(int k=;k<=;k++)//两个方向,下或右
{
if(k==)
{
xx=x+a[x][y];
yy=y;
}else
{
xx=x;
yy=y+a[x][y];
}
if(xx<=n&&yy<=n)//避免越界
dp[x][y]+=dfs(xx,yy);
}
return dp[x][y];
}
int main()
{
while(~scanf("%d",&n))
{
if(n==-)
break;
for(int i=;i<=n;i++)
{
scanf("%s",s);
int l=strlen(s);
int k=;
for(int j=;j<l;j++)
{
a[i][k++]=s[j]-'';
}
}
memset(dp,,sizeof(dp));
printf("%I64d\n",dfs(,));//从1,1开始搜
}
return ;
}
HDU 1208 Pascal's Travels 经典 跳格子的方案数 (dp或者记忆化搜索)的更多相关文章
- HDU 1513 Palindrome:LCS(最长公共子序列)or 记忆化搜索
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 题意: 给你一个字符串s,你可以在s中的任意位置添加任意字符,问你将s变成一个回文串最少需要添加 ...
- hdu 1208 Pascal's Travels
http://acm.hdu.edu.cn/showproblem.php?pid=1208 #include <cstdio> #include <cstring> #inc ...
- HDU 4597 Play Game(区间DP(记忆化搜索))
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4597 题目大意: 有两行卡片,每个卡片都有各自的权值. 两个人轮流取卡片,每次只能从任一行的左端或右端 ...
- HDU 4597 Play Game (DP,记忆化搜索,博弈)
题意:Alice和Bob玩一个游戏,有两个长度为N的正整数数字序列,每次他们两个,只能从其中一个序列,选择两端中的一个拿走.他们都希望可以拿到尽量大的数字之和, 并且他们都足够聪明,每次都选择最优策略 ...
- HDU 4597 Play Game (DP,记忆化搜索)
Play Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total S ...
- HDU 2089 不要62(数位DP·记忆化搜索)
题意 中文 最基础的数位DP 这题好像也能够直接暴力来做 令dp[i][j]表示以 j 开头的 i 位数有多少个满足条件 那么非常easy有状态转移方程 dp[i][j] = sum{ dp[ ...
- HDU 3652 B-number(数位dp&记忆化搜索)
题目链接:[kuangbin带你飞]专题十五 数位DP G - B-number 题意 求1-n的范围里含有13且能被13整除的数字的个数. 思路 首先,了解这样一个式子:a%m == ((b%m)* ...
- HDU 4960 Another OCD Patient(记忆化搜索)
HDU 4960 Another OCD Patient pid=4960" target="_blank" style="">题目链接 记忆化 ...
- POJ 2704 Pascal's Travels 【DFS记忆化搜索】
题目传送门:http://poj.org/problem?id=2704 Pascal's Travels Time Limit: 1000MS Memory Limit: 65536K Tota ...
随机推荐
- hdu 2049 考新郎
假设一共有N对新婚夫妇,其中有M个新郎找错了新娘,求发生这种情况一共有多少种可能. 和之前那道题一样,是错排,但是要乘上排列数. 选对的人有C(N,M)个组合,将它们排除掉,剩下的人就是错排了 #in ...
- 《JavaWeb从入门到改行》分页功能的实现
@目录 什么是分页 ? 两个子模块功能的问题分析 和 解决方案 有条件查和无条件查询的影响 和 解决方案 项目案例: mysql + commons-dbutils+itcast-tools+Base ...
- 多线程拷贝备份文件方法(Windows)
D:\bat\robocopy.exe D:\backupdata \\192.168.36.45\BData\ db_20170716.dmp 解释: 微软提供的robocopy.exe命令,默 ...
- springcloud 集成kafka问题记录,发消息报错:ERROR o.s.kafka.support.LoggingProducerListener - Exception thrown when sending a message with key='null' and payload='{-1,
在springcloud集成kafka,发送消息时报错: 2018-08-15 16:01:34.159 [http-nio-8081-exec-1] INFO org.apache.kafka.c ...
- Vue组件通讯
Vue最常用的组件通讯有三种:父->子组件通讯.子->父组件通讯,兄弟组件通讯.(template用的pug模板语法) 1.父->子组件通讯 父->子组件通讯,是通过props ...
- 【es6】Generator 函数
1. 基本概念 状态机,封装了多个内部状态 2. 应用 返回一个遍历器对象. 3. 代码形式 function* helloWorldGenertor() { yield 'hello'; yield ...
- No toolchains found in the NDK toolchains folder for ABI with prefix: arm-linux-androideabi
产生背景最近把Android Studio更新到3.0,更新之后出现了build错误:No toolchains found in the NDK toolchains folder for ABI ...
- zabbix fast
源码安装步骤 来源它站: 官方文档:https://www.zabbix.com/documentation/2.4/manual/installation/install#from_the_sour ...
- Python学习---Java和Python的区别小记
Java和Python的区别小记 注意这里使用的是 and/or/not 非java中的&&,||,!Java中的true是小写 Python中函数就是对象,函数和我们之前的[1,2 ...
- Linux ping命令详解
Linux系统的ping命令是常用的网络命令,它通常用来测试与目标主机的连通性 基于IMCP协议 常见命令参数 -q 不显示任何传送封包的信息,只显示最后的结果 -n 只输出数值 -R 记录路由过程 ...