题目传送门:http://poj.org/problem?id=2704

Pascal's Travels

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5535   Accepted: 2500

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 263 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

Brute force methods examining every path will likely exceed the allotted time limit. 64-bit integer values are available as long values in Java or long long values using the contest's C/C++ compilers.

Source

 
 

题意概括:

有一个N*N的游戏板, 每一格的数字代表可以跳的步数,起点在左下角,每次可以选择向下或者向右跳,问从左上角起点(固定)跳到右下角终点(固定)的路径有几条。

解题思路:

DFS模拟暴力跳的可能性,记忆化搜索需要记录从当前格可以到达终点的路径数。

!!!因为每天路径都是独一无二的,需要一个标记数组 vis (一开始想着每次都是向下向右应该不会重复,不过wa掉了)

AC code:

 ///POJ 2704 记忆化搜索
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
#define ll long long int
using namespace std;
const int MAXN = ; ll d[MAXN][MAXN];
int mmp[MAXN][MAXN];
bool vis[MAXN][MAXN];
int N; bool ok(int x, int y)
{
if(x >= && x <= N && y >= && y <= N) return true;
else return false;
}
ll dfs(int x, int y)
{
if(d[x][y] || (x==N && y==N)) return d[x][y];
if(!ok(x+mmp[x][y], y) && !ok(x, y+mmp[x][y])) d[x][y] = -;
if(ok(x+mmp[x][y], y) && d[x+mmp[x][y]][y]!=-)
{
if(!vis[x+mmp[x][y]][y])
{
vis[x+mmp[x][y]][y] = ;
ll len = dfs(x+mmp[x][y], y);
vis[x+mmp[x][y]][y] = ;
if(len > ) d[x][y] += len;
}
}
if(ok(x, y+mmp[x][y]) && d[x][y+mmp[x][y]]!=-)
{
if(!vis[x][y+mmp[x][y]])
{
vis[x][y+mmp[x][y]] = ;
ll len = dfs(x, y+mmp[x][y]);
vis[x][y+mmp[x][y]] = ;
if(len > ) d[x][y] += len;
}
}
return d[x][y];
}
int main()
{
int T = ;
char str[MAXN][MAXN];
while(T--)
{
scanf("%d", &N);
if(N == -) break;
for(int i = ; i <= N; i++)
scanf("%s", &str[i]);
for(int i = ; i <= N; i++)
for(int j = ; j < N; j++)
mmp[i][j+] = str[i][j]-'';
memset(d, , sizeof(d));
memset(vis, , sizeof(vis));
d[N][N] = ;
ll ans = dfs(, );
printf("%lld\n", ans);
}
return ;
}

POJ 2704 Pascal's Travels 【DFS记忆化搜索】的更多相关文章

  1. POJ 1191 棋盘分割 【DFS记忆化搜索经典】

    题目传送门:http://poj.org/problem?id=1191 棋盘分割 Time Limit: 1000MS   Memory Limit: 10000K Total Submission ...

  2. 不要62 hdu 2089 dfs记忆化搜索

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意: 给你两个数作为一个闭区间的端点,求出该区间中不包含数字4和62的数的个数 思路: 数位dp中 ...

  3. dfs+记忆化搜索,求任意两点之间的最长路径

    C.Coolest Ski Route 题意:n个点,m条边组成的有向图,求任意两点之间的最长路径 dfs记忆化搜索 #include<iostream> #include<stri ...

  4. POJ 1088 滑雪 DFS 记忆化搜索

    http://poj.org/problem?id=1088 校运会放假继续来水一发^ ^ 不过又要各种复习,功课拉下了许多 QAQ. 还有呀,就是昨天被一个学姐教育了一番,太感谢了,嘻嘻^ ^ 好了 ...

  5. poj1088-滑雪 【dfs 记忆化搜索】

    http://poj.org/problem?id=1088 滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 79806 ...

  6. POJ 1579 Function Run Fun 【记忆化搜索入门】

    题目传送门:http://poj.org/problem?id=1579 Function Run Fun Time Limit: 1000MS   Memory Limit: 10000K Tota ...

  7. hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)

    pid=1078">FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/ ...

  8. hdu 1078(dfs记忆化搜索)

    题意:容易理解... 思路:我开始是用dfs剪枝做的,968ms险过的,后来在网上学习了记忆化搜索=深搜形式+dp思想,时间复杂度大大降低,我个人理解,就是从某一个点出发,前面的点是由后面的点求出的, ...

  9. UVA 10400 Game Show Math (dfs + 记忆化搜索)

    Problem H Game Show Math Input: standard input Output: standard output Time Limit: 15 seconds A game ...

随机推荐

  1. spring开发中commons-logging.jar包的功能

    删除后程序会报错 Java.lang.NoClassDefFoundError 记录日志,通常和  log4j.jar共同使用 原因: 在 sun 开发 logger 前,apache 项目已经开发了 ...

  2. Oracle中查询关键字select--from--where--group by--having--order by执行顺序

    select--from--where--group by--having--order by 这6个查询关键字的执行顺序: 1.from组装来自不同数据源的数据:2.where基于指定的条件对记录行 ...

  3. LaTex 2

    LaTex 入门 此时是否安装成功 如果安装成功了LaTeX, 那么在计算机上会多出来LaTeX的编译器, LaTex Live 安装包在计算机上安装了多个不同的编译器, 有latex, xelate ...

  4. MarkDown 语言简单使用

    # Markdown file ![alt img is error](http://cdn2.jianshu.io/assets/web/logo-58fd04f6f0de908401aa561cd ...

  5. 【学习笔记】HTML基础:使用html制作网页

    一.初识HTML 1.什么是HTML? Hyper Text Markup Language(超文本标记语言) 扩展XML:Extendsible  Markup Language(可扩展性标记语言) ...

  6. String常用操作

    常量池: 字符串一旦被初始化就不会被改变 String s="123"; s="abc"; System.out.print(s); 这段代码看上去s的值是被改 ...

  7. XHR的应用场景

    一.简史 IE5.5最早实现XHR,需要通过ActiveXObject创建xhr实例,直到IE7才定义了XMLHttpRequest对象.IE5.5实现XHR之后,其他浏览器紧随其后实现了XHR,直接 ...

  8. vue——组件

    一.组件概念 vue的核心基础就是组件的使用,玩好了组件才能将前面学的基础更好的运用起来.组件的使用更使我们的项目解耦合.更加符合vue的设计思想MVVM. // 定义一个名为 button-coun ...

  9. Html5的map在实际使用中遇到的问题及解决方案

    前言:百度了一下html map,嗯嗯,介绍的挺详细的,如果是初学者,直接看他们的教程,挺好的,就不用我再多说了. 不过我发现一个问题,就是都是介绍map有什么属性怎么用的,这明显就是照搬文档自己再改 ...

  10. 创建简单的node服务器

    昨天咱们说了封装ajax,今天咱们说一下 自己创建一个建议的node服务器: 话不多说直接上代码: var http = require('http') //对URL 解析为对象//1.导入模块 UR ...