Pascal's Travels

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status

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
 
分析:
思路1:
dp[i][j]:表示从1,1到i,j的方案数目
注意初始化dp[1][1]=1
 
状态转移方程:
 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];
下一个状态有当前状态决定
code:
#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或者记忆化搜索)的更多相关文章

  1. HDU 1513 Palindrome:LCS(最长公共子序列)or 记忆化搜索

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 题意: 给你一个字符串s,你可以在s中的任意位置添加任意字符,问你将s变成一个回文串最少需要添加 ...

  2. hdu 1208 Pascal's Travels

    http://acm.hdu.edu.cn/showproblem.php?pid=1208 #include <cstdio> #include <cstring> #inc ...

  3. HDU 4597 Play Game(区间DP(记忆化搜索))

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4597 题目大意: 有两行卡片,每个卡片都有各自的权值. 两个人轮流取卡片,每次只能从任一行的左端或右端 ...

  4. HDU 4597 Play Game (DP,记忆化搜索,博弈)

    题意:Alice和Bob玩一个游戏,有两个长度为N的正整数数字序列,每次他们两个,只能从其中一个序列,选择两端中的一个拿走.他们都希望可以拿到尽量大的数字之和, 并且他们都足够聪明,每次都选择最优策略 ...

  5. HDU 4597 Play Game (DP,记忆化搜索)

    Play Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total S ...

  6. HDU 2089 不要62(数位DP&#183;记忆化搜索)

    题意  中文 最基础的数位DP  这题好像也能够直接暴力来做   令dp[i][j]表示以 j 开头的 i 位数有多少个满足条件 那么非常easy有状态转移方程 dp[i][j] = sum{ dp[ ...

  7. HDU 3652 B-number(数位dp&amp;记忆化搜索)

    题目链接:[kuangbin带你飞]专题十五 数位DP G - B-number 题意 求1-n的范围里含有13且能被13整除的数字的个数. 思路 首先,了解这样一个式子:a%m == ((b%m)* ...

  8. HDU 4960 Another OCD Patient(记忆化搜索)

    HDU 4960 Another OCD Patient pid=4960" target="_blank" style="">题目链接 记忆化 ...

  9. POJ 2704 Pascal's Travels 【DFS记忆化搜索】

    题目传送门:http://poj.org/problem?id=2704 Pascal's Travels Time Limit: 1000MS   Memory Limit: 65536K Tota ...

随机推荐

  1. delphi之读写文件的三种方式

    一.Tstrings.Tstringlist procedure TForm1.Button2Click(Sender: TObject); var strlist: TStringList; pat ...

  2. CentOS-Linux系统下安装Tomcat

    步骤1:解压Tomcat 命令: unzip apache-tomcat-8.5.20.zip 步骤2:将tomcat 移动到“/usr/local/src/java/tomcat8.5”下并重命名 ...

  3. lincode 题目记录6

    the Duplicate Number  132 PatternFind 找重复的数字··直接暴力枚举是不行的···又超时提示·· 暴力枚举的写法· res=0 def findDuplicate( ...

  4. js类的笔记

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Document对象关于窗口的一些属性

    在网上搜罗的,只为自己查用方便,不做他用 window.screen.availWidth 返回当前屏幕宽度(空白空间) window.screen.availHeight 返回当前屏幕高度(空白空间 ...

  6. PHP中使用Jpgraph生成统计图

    Jpgraph是PHP图表类库,可以生成折线图.柱状图.大饼图等等统计图.如果你想使用PHP生成统计图来统计数据,使用它再方便不过啦. 如果说你要亲自使用GD库来写的话,那我只能膜拜大神啦(我不会哈哈 ...

  7. AngularJS 最常用指令的功能

    第一迭代输出之ng-repeat标签 ng-repeat让table ul ol等标签和js里的数组完美结合 <ul> <li ng-repeat="person in p ...

  8. 【Angularjs】ng-repeat中使用ng-model遇到的问题

    总结:在ng-repeat中ng-model的问题,原因是ng-model对controller中的$scope是不可见的,所以在使用repeat中的某个对象的属性的时候,最好还是将该对象或者该对象的 ...

  9. Intellij IDEA通过SVN导入基于Springboot的maven项目以及对已有项目做更新

    一.导入外部maven项目 点击“+”,输入SVN地址并下载项目 弹出窗口,选择new window(自己觉得哪个好就选哪个) 等待执行完毕,执行完后会出现以下情况,就需要配置一下你的maven库了 ...

  10. Exception in thread "main" java.lang.UnsatisfiedLinkError: no awt in java.library.path:

    Exception in thread "main" java.lang.UnsatisfiedLinkError: no awt in java.library.path: 这是 ...