题目传送门: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. Bootstrap 斜体、文本对齐、缩略图、地址、列表等

    目录1.标题2.页面主体3.强调    a.小号文本    b.着重    c.斜体    d.对齐class    e.强调class4.缩略语5.地址6.列表    a.无序列表    b.有序列 ...

  2. git win7 dos下设置代理

    git config --global http.proxy http://username:pwd@my.you.com:port

  3. log4j整理

    <meta http-equiv="refresh" content="1"/> # log4j日志组件 #- SLF4J,一个**通用日志接口** ...

  4. MongoDB的聚合函数 Aggregate

    Aggregate的使用,有利于我们对MongoDB中的集合进行进一步的拆分. 示例: db.collection.aggregate( {$match:{x:1}, {limit:10}, {$gr ...

  5. jQuery中的DOM操作——《锋利的JQuery》

    jQuery封装了大量DOM操作的API,极大提高了操作DOM节点的效率. 1.查找节点 通过我们上一节介绍了JQuery选择器,可以非常轻松地查找节点元素.不过,这时得到的是jQuery对象,只能使 ...

  6. LintCode刷题小记491

    题目: 判断一个正整数是不是回文数. 回文数的定义是,将这个数反转之后,得到的数仍然是同一个数. 样例: 11, 121, 1, 12321 这些是回文数. 23, 32, 1232 这些不是回文数. ...

  7. java版两人聊天程序

    server.java import java.io.*; import java.net.*; import java.text.SimpleDateFormat; import java.util ...

  8. javaweb九大个内置对象,四大域

    9个内置对象如下: 1.session对象:会话对象 当客户端第一次访问服务器的页面时,web服务器会自动为该客户端创建一个session对象并分配一个唯一的id号 常常用它来在多个页面间共享数据,如 ...

  9. 微信token验证源码分享(c#版)

    在开发时遇到一个问题: 上线后提交申请微信提示"您的服务器没有正确响应token验证...",我查看日志发现根本就没有接收到来自微信的参数. 后来我又记录了微信请求方式和请求的字符 ...

  10. 课堂笔记&总结与遇错纠错篇

    一.课堂笔记 二.个人总结 在学习和工作JDK是必不可少的程序员必备工具,遇到问题可以在帮助文档寻找答案! 接受能力不足,老师讲的知识点过去了,我经常还在想上一个知识点.希望老师有时候重点可以讲慢点哈 ...