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. 【Chromium】GPU进程启动流程

    本篇文档以gpu进程的创建和启动为例,讲述chormium如何启动一个browser进程的子进程 PS:本文使用的chromium代码版本为71 前言 GPU进程的启动时机是由browser进程负责的 ...

  2. frameset 在 Google Chrome 中无法隐藏左边栏解决方法!

    使用Frameset 框架,发现在IE下, <frameset name="mainDefine" cols="200,10,*" frameborder ...

  3. 洛谷 P3391 文艺平衡树

    题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 --b ...

  4. 浏览器组成、线程及event loop

    浏览器组成 User interface: a. Every part of the browser display, except the window. b. The address bar, b ...

  5. Microsoft Visual Studio 2010下log4cplus的安装,集成,测试

    原文:http://blog.csdn.net/eclipser1987/article/details/6904301 log4cplus是C++编写的开源的日志系统,功能非常全面,用到自己开发的工 ...

  6. 使用ajax请求数据时的几种做法

    在进行前后端交互的时候,一般前端使用ajax向后端发送数据,后端根据发送的数据来返回数据,前端将这些数据接收并进行相应的处理 以下是在日常工作中总结的几点使用ajax传递数据时的情况: 1.在本页面( ...

  7. Java web相关内容

    我们即将学习Java web 这是通过查阅资料找到的和Java web 相关的内容. 一:Java web的含义 JavaWeb,是用Java技术来解决相关web互联网领域的技术总和.web包括:we ...

  8. Halo 的缔造者们在忙什么?

    如果你自认为是一名主机游戏玩家,就一定知道 Halo.自 2001 年首代作品问世至今,十多年的磨炼已使得『光环』成为世界顶级的 FPS 游戏之一.<光环4>的推出,更让系列走向一个重要的 ...

  9. 使用ifstream和getline读取文件内容[c++] ZZ

      假设有一个叫 data.txt 的文件, 它包含以下内容: Fry: One Jillion dollars.[Everyone gasps.]Auctioneer: Sir, that's no ...

  10. C# 试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)

    C# 在调用C++dll时,可能会出现 :试图加载格式不正确的程序. (异常来自 HRESULT:0x8007000B)这个错误. 一般情况下是C#目标平台跟C++dll不兼容,64位跟32位兼容性问 ...