题目传送门: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. (Frontend Newbie)Web三要素(三)

    上一篇简单介绍了Web三要素中的层叠样式表,本篇主要介绍三要素中最后一个,也是最难掌握的一个-----JavaScript. JavaScript 老规矩不能破,先简要交代 JavaScript 的历 ...

  2. unity的技术博客

    技术博客 http://www.cnblogs.com/wangergo/

  3. JS识别不同浏览器信息

    总所周知,不同浏览器兼容是不一致的,然而今天我在Coding的时候深深体会到那个痛苦,一样的代码在Firefox里面是没问题的,可以根据索引找到 对应的对象元素然后进行操作,但是同样的却获取不到对象元 ...

  4. hihoCoder题目之Magic Box

    #include <iostream> #include <cmath> #include <cstdio> using namespace std; void s ...

  5. java使用poi.3.10读取excel 2007以上版本(xlsx格式)

    1.在使用过程中,一直报错 throw new ClassNotFoundException(name);原因:没有导入xmlbeans-2.6.0.jar包,建议在使用poi时,将所有包都导入进工程 ...

  6. node之log4js

    log4js的配置文件: "log4js": { "appenders": { "out": { "type": &qu ...

  7. 数据库mysql中编码自动生成

    call PrGetRuleCodeNoDate('Table_Name'); call PrGetRuleCode('Table_Name');

  8. C#学习笔记8

    1.泛型的约束: (1)接口约束: (2)基类约束,基类约束必须放在第一(假如有多个约束): (3)struct/class约束: (4)多个参数类型的约束,每个类型参数都要用where关键字: (5 ...

  9. 检测IE浏览器兼容Edge模式及IE11

    document.documentMode || +(navigator.userAgent.match(/MSIE (\d+)/) && RegExp.$1) 判断布尔值

  10. ul标签在FF中默认只有padding值(即:padding-left:40px)