题目传送门: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. css垂直居中几种方法(二)

    方法1:table-cell .box1{ display: table-cell; vertical-align: middle; text-align: center; } <div cla ...

  2. 牛客网Java刷题知识点之TCP、UDP、TCP和UDP的区别、socket、TCP编程的客户端一般步骤、TCP编程的服务器端一般步骤、UDP编程的客户端一般步骤、UDP编程的服务器端一般步骤

    福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号:   大数据躺过的坑      Java从入门到架构师      人工智能躺过的坑         Java全栈大联盟   ...

  3. 转-------基于R-CNN的物体检测

    基于R-CNN的物体检测 原文地址:http://blog.csdn.net/hjimce/article/details/50187029 作者:hjimce 一.相关理论 本篇博文主要讲解2014 ...

  4. mongodb连接失败原因排查

    安装了mongodb,添加了管理员root和test数据库的用户rex,并且开启的用户认证. 按照说明文档连接mongodb数据库:$mongo = new Mongo("mongodb:/ ...

  5. Windows phone 8.1应用集成cortana语音命令

    微软推出小娜已经有一段时间了,最近恰好在研究其用法,就随便写点记录一下自己的心得. 在研究时参考了@王博_Nick的博客:http://www.cnblogs.com/sonic1abc/p/3868 ...

  6. 如何取得GridView被隐藏列的值

    如何取得GridView被隐藏列的值         分类:             ASP.net              2009-06-25 12:47     943人阅读     评论(1 ...

  7. setInterval()的三种写法

    前言: setInterval("fun()",time)有两个参数:fun()为要执行的函数:time为多久执行一次函数,单位是毫秒: 我们做一个简单的例子,就是每隔5s弹出一个 ...

  8. poi学习

    需要节点  <dependency>  <groupId>org.apache.poi</groupId>  <artifactId>poi</a ...

  9. Number()转换规则

    转换规则: Number(): 1)如果是Boolean值,true和false将分别转换为1和0. 2)如果是数字值,只是简单的传入和返回. 3)如果是null值,返回0. 4)如果是undefin ...

  10. 《ArcGIS Runtime SDK for Android开发笔记》——离在线一体化技术:概述

    1.前言 数据生产和数据展示是常见的两大专业级移动GIS应用场景,这里我们针对数据生产环节的ArcGIS的离在线一体化技术给大家做一个基本的介绍和梳理. 使用ArcGIS离在线一体化技术首先需要以下基 ...