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 ...
随机推荐
- Web安全相关(五):SQL注入(SQL Injection)
简介 SQL注入攻击指的是通过构建特殊的输入作为参数传入Web应用程序,而这些输入大都是SQL语法里的一些组合,通过执行SQL语句进而执行攻击者所要的操作,其主要原因是程序没有细致地过滤用户输入的数据 ...
- sql:PostgreSQL
PostgreSQL sql script: -- Database: geovindu -- DROP DATABASE geovindu; CREATE DATABASE geovindu WIT ...
- JavaWeb学习总结(九):Cookie进行会话管理
一.会话的概念 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 有状态会话:一个同学来过教室,下次再来教室,我们会知道这个同学曾 ...
- js array copy method
//浅拷贝: let arr=[1,2,3,4] let arr2=arr arr[3]=0 console.log(arr,arr2) //output: (4) [1, 2, 3, 0] (4) ...
- Vuejs入门级简单实例
Vue作为2016年最火的框架之一,以其轻量.易学等特点深受大家的喜爱.今天简单介绍一下Vue的使用. 首先,需要在官网下载vuejs,或者直接用cdn库.以下实例使用Vue实现数据绑定与判断循环: ...
- asp.net修改上传文件大小
我们大家都知道ASP.NET为我们提供了文件上传服务器控件FileUpload,默认情况下可上传的最大文件为4M,如果要改变可上传文件大小限制,那么我们可以在web.config中的httpRunti ...
- Vue.js学习(常用指令)
Vue.js的指令是以v-开头,它们用于HTML元素,指令提供了一些特殊的特性,将指令绑定在元素上时,指令会为绑定的目标元素添加一些特殊的行为,我们可以将指令看作特殊的HTML特性. 本文参考:htt ...
- webform 使用log4net配置
效果: web.config配置 <configuration> <configSections> <!--log4net日志记录--> <section n ...
- oracle decode函数和 sign函数
流程控制函数 DECODE decode()函数简介: 主要作用: 将查询结果翻译成其他值(即以其他形式表现出来,以下举例说明): 使用方法: Select decode(columnname,值1, ...
- SQL点点滴滴_特殊用法笔记
声明: 本文为转载,感谢原作者的辛勤付出. 原博客地址为:http://www.cnblogs.com/icyJ/p/SQL_Statement.html 1.MERGE用法:关联两表,有则改,无则加 ...