题目链接:

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

Pascal's Travels

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2795    Accepted Submission(s): 1298

Problem Description
An n x n game board is populated with integers, one nonnegative integer per square. The goal is to travel along any legitimate path from the upper left corner to the lower right corner of the board. The integer in any one square dictates how large a step away from that location must be. If the step size would advance travel off the game board, then a step in that particular direction is forbidden. All steps must be either to the right or toward the bottom. Note that a 0 is a dead end which prevents any further progress.

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
The input contains data for one to thirty boards, followed by a final line containing only the integer -1. The data for a board starts with a line containing a single positive integer n, 4 <= n <= 34, which is the number of rows in this board. This is followed by n rows of data. Each row contains n single digits, 0-9, with no spaces between them.
 
Output
The output consists of one line for each board, containing a single integer, which is the number of paths from the upper left corner to the lower right corner. There will be fewer than 2^63 paths for any board.
 
Sample Input
4
2331
1213
1231
3110
4
3332
1213
1232
2120
5
11101
01111
11111
11101
11101
-1
 
Sample Output
3
0
7

Hint

Hint

Brute force methods examining every path will likely exceed the allotted time limit.
64-bit integer values are available as "__int64" values using the Visual C/C++ or "long long" values
using GNU C/C++ or "int64" values using Free Pascal compilers.

 
Source
分析;
看到一句话:
 所谓动态规划,就是每一个阶段你要做出一个决策,从最开始的决策到结束的决策的集合叫做一个策略,而各个阶段决策的多样性直接导致了策略的多样性,而我们一般求得的不是最有策略结束最大最小花费策略,每一个阶段的决策在这种有目的的作用下都与上一个阶段的决策相关,我们用记录这个阶段的一个东西(通常是动态数组)来记录这个状态符合我们需要的策略的决策
 
唉,还是不能很好的理解dp思想
 
本题分析;
要 求左上角到右下角的方法数
每次只能往下或者往右跳,跳的格子数必须等于现在站的格子的权值(不能少跳,比如你站的格子中权值是3,那么你在不超出边界的条件下你一定要跳3个格子),这样一来,有的格子可能跳不到,而有的格子可能有多种到达的方式
 
 
dp[i][j] 表示到达i,j的方法数目
注意:
1.初始化问题:dp初始化为0,dp[0][0]=1
2.状态转移方程:
  dp[i][j+a[i][j]]+=dp[i][j];
  dp[i+a[i][j]][j]+=dp[i][j];
不超边界的前提下(j+a[i][j]<n   i+a[i][j]<n)
3.long long 类型(注意题目中拿到2的63次方)
代码如下:
#include<bits/stdc++.h>
using namespace std;
#define max_v 40
long long dp[max_v][max_v];//dp[i][j] 表示到达i行j列有多少种走法
long long a[max_v][max_v];
char s[max_v];
int main()
{
int n;
while(~scanf("%d",&n))
{
if(n==-1)
break;
getchar();
for(int i=0;i<n;i++)
{
scanf("%s",s);
for(int j=0;j<strlen(s);j++)
{
a[i][j]=s[j]-'0';
}
}
memset(dp,0,sizeof(dp));
dp[0][0]=1;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[i][j]==0)//防止死循环
continue;
if(j+a[i][j]<n)//向右边跳
{
dp[i][j+a[i][j]]+=dp[i][j];
}
if(i+a[i][j]<n)//向下跳
{
dp[i+a[i][j]][j]+=dp[i][j];
}
}
}
printf("%lld\n",dp[n-1][n-1]);
}
return 0;
}

  

HDU 1208 跳格子题(很经典,可以有很多变形)的更多相关文章

  1. HDU 1208 Pascal's Travels 经典 跳格子的方案数 (dp或者记忆化搜索)

    Pascal's Travels Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Su ...

  2. HDU 1728 逃离迷宫(DFS经典题,比赛手残写废题)

    逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  3. 一道很经典的 BFS 题

    一道很经典的 BFS 题 想认真的写篇题解. 题目来自:https://www.luogu.org/problemnew/show/P1126 题目描述 机器人移动学会(RMI)现在正尝试用机器人搬运 ...

  4. HDU 1241 Oil Deposits(经典DFS)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 很经典的一道dfs,但是注意每次查到一个@之后,都要把它变成“ * ”,然后继续dfs ...

  5. java 基础题 很基础, 很有趣

    都是一些非常非常基础的题,是我最近参加各大IT公司笔试后靠记忆记下来的,经过整理献给与我一样参加各大IT校园招聘的同学们,纯考Java基础功底, 老手们就不用进来了,免得笑话我们这些未出校门的孩纸们, ...

  6. HTTP协议详解(真的很经典)(转载)

    HTTP协议详解(真的很经典):http://www.cnblogs.com/li0803/archive/2008/11/03/1324746.html 引言 HTTP是一个属于应用层的面向对象的协 ...

  7. hdu 1254(搜索题)

    推箱子 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  8. HDU-1042-N!(Java大法好 &amp;&amp; HDU大数水题)

    N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Subm ...

  9. 为IT程序员量身定制的12个目标——很经典

    对程序员们来说挑战自我非常重要,要么不断创新,要么技术停滞不前.新年伊始,我整理了12个月的目标,每个目标都是对技术或个人能力的挑战,而且可以年复一年循环使用. 01. 变得有耐心 02. 保持健康 ...

随机推荐

  1. MyEclipse去除网上复制下来的代码带有的行号(使用正则表达式)

    一.正则表达式去除代码行号 作为开发人员,我们经常从网上复制一些代码,有些时候复制的代码前面是带有行号,如: MyEclipse本身自带有查找替换功能,并且支持正则表达式替换,使用正则替换就可以很容易 ...

  2. JavaScript访问对象的属性和方法

    对象的属性和方法统称为对象的成员. 访问对象的属性 在JavaScript中,可以使用“ . ”和“ [ ] ”来访问对象的属性. 二者区别:“ . ”表示法一般作为静态对象使用时来存取属性.而“[ ...

  3. CSS属性之margin

    0.对自身可视宽度的影响 1>改变处于标准文档流中,未设置width值的block元素的可视宽度 在标准文档流中,对于没有设置宽度的block元素,当其具有内容或者设置高度后,其自身宽度为父元素 ...

  4. jquery 仿windows10菜单效果下载

    http://www.kuitao8.com/20150923/4079.shtml jquery 仿windows10菜单效果下载

  5. Linux常用命令(二)————压缩+解压

    tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...

  6. VS2013个版本密钥(亲测可用)

    Visual Studio Ultimate 2013 KEY(密钥):BWG7X-J98B3-W34RT-33B3R-JVYW9 Visual Studio Premium 2013 KEY(密钥) ...

  7. Excel VBA开发

    一.Excel添加treeview控件 如果是以VBA中为窗体添加,菜单:工具->附加控件,从中选择“Microsoft TreeView Control”: 在控件工具箱中点击其它控件,从中选 ...

  8. 配置 Tomcat 服务 和 自启动

    如果我们使用war 包进行部署项目的时候,需要把包放进Tomcat的目录下,为了使我们的服务能够在服务器重启的时候自动启动起来,我们需要把Tomcat设置成自起服务. 配置 Tomcat 服务 新建服 ...

  9. Java.util 包(Date 类、Calendar类、Random类)

    java.util 包提供了一些实用的方法和数据结构. Date 类 Date 类表示日期和时间,里面封装了操作日期和时间的方法.Date 类经常用来获取系统当前时间. 构造方法: 构造方法 说明 D ...

  10. APPLICATION FAILED TO START

    Description: Cannot determine embedded database driver class for database type NONE Action: If you w ...