题目传送门: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. jdk8涉及到的接口、类及方法

    bi是binary的简写,二元的,表示两个参数 unary,一元的,表示一个参数 1.函数式接口Supplier T get(),不接收参数,有返回值 IntSupplier,int getAsInt ...

  2. 算法市场 Algorithmia

    算法市场 官网:(需要***,fan qiang,不然可能访问不了或登录不了) https://algorithmia.com/ 官方的例子: 我不用 curl 发请求,把 curl 命令粘贴给你们用 ...

  3. Epplus导出Excel(DataTable)

    1.先将dataTable转换成流 public Stream DataTableToExcel(DataTable dataTable, string[] columns, string sheet ...

  4. [转]python中对文件、文件夹的操作——os模块和shutil模块常用说明

    转至:http://l90z11.blog.163.com/blog/static/187389042201312153318389/ python中对文件.文件夹的操作需要涉及到os模块和shuti ...

  5. 绘图神器-matplotlib入门

    这次,让我们使用一个非常有名且十分有趣的玩意儿来完成今天的任务,它就是jupyter. 一.安装jupyter matplotlib入门之前,先安装好jupyter.这里只提供最为方便快捷的安装方式: ...

  6. gcc 链接非标准名称库

    一般库的标准名称是libxxx.so或者libxxx.a, 如果没有, 也可以搞个linkname出来, 那就可以直接用 "-lxxx" 来链接了, 但要是你想直接用realnam ...

  7. 利用pandas生成csv文件

    # -*- coding:UTF-8 -*- import json from collections import OrderedDict with open('dns_status.json',' ...

  8. 04.Continue,和三元表达式的学习

    立即结束本次循环,判断循环条件,如果成立,则进入下一次循环,否则退出循环. 举例:运动员跑步喝水的例子 比如:我编写代码的时候,上个厕所,回来继续写代码 练习1: namespace _09.练习02 ...

  9. python学习(六)--正则的一些例子

    import re #正则表达式#compile函数,--将正则表达式转变为内部函数,提高执行效率strr = "python123456"pattern = "Pyth ...

  10. oracle UDT 有关数据字典的研究

    1.数据及类型准备 创建了一个自定义类型 create or replace type addr_type as object( street varchar2(30); city varchar2( ...