POJ 2704 && HDU 1208 Pascal’s Travels (基础记忆化搜索)[通俗易懂]

POJ 2704 && HDU 1208 Pascal’s Travels (基础记忆化搜索)[通俗易懂] Pascal’sTravelsTimeLimit:1000MS   MemoryLimit:65536K TotalSubmissions:5328   Accepted:2396  DescriptionAnnxngameboardispopulatedwithintegers,onenonnegative…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

 

Pascal’s Travels

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5328   Accepted: 2396

 

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.

POJ 2704 && HDU 1208 Pascal's Travels (基础记忆化搜索)[通俗易懂] POJ 2704 && HDU 1208 Pascal's Travels (基础记忆化搜索)[通俗易懂]
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

Mid-Central USA 2005

题目链接:http://poj.org/problem?id=2704    http://acm.hdu.edu.cn/showproblem.php?pid=1208

题目大意:从左上角开始到右下角,每次只能向右或者向下,并且只能走当前位置点数的步数,求从起点到终点有多少种走法

题目分析:简单的记忆化搜索,dp[i][j]记录点(i,j)到终点的方案数,终点处为0需特殊处理

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int const MAX = 40;
int mp[MAX][MAX], n;
long long dp[MAX][MAX];
char s[100];
int dx[2] = {0, 1};
int dy[2] = {1, 0};
 
long long DFS(int x, int y) {
    if (dp[x][y] != -1) {
        return dp[x][y];
    }
    if (x == n && y == n) {
        return 1;
    }
    long long ans = 0;
    for (int i = 0; i < 2; i++) {
        int xx = x + dx[i] * mp[x][y];
        int yy = y + dy[i] * mp[x][y];
        if (xx > n || yy > n || (mp[xx][yy] == 0 && !(xx == n && yy == n))) {
            continue;
        }
        ans += DFS(xx, yy);
    }
    return dp[x][y] = ans;
}
 
int main() {
    while (scanf("%d", &n) != EOF && n != -1) {
        memset(dp, -1, sizeof(dp));
        for (int i = 1; i <= n; i++) {
            scanf("%s", s + 1);
            for (int j = 1; j <= n; j++) {
                mp[i][j] = s[j] - '0';
            }
        }
        cout << DFS(1, 1) << endl;
    }
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/164449.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 内存映射文件「建议收藏」

    内存映射文件「建议收藏」在做科研,实现一些大数据的算法的时候,经常要调用一些文件的I/O函数,在数据量很大的时候,除了设计的算法和数据结构的耗时以外,其实主要的耗时还是文件的I/O。因为一般常规的方法就是先读出磁盘文件的内容到内存中,然后修改,最后写回到磁盘上。读磁盘文件是要经过一次系统调用,先将文件的内容从磁盘拷贝到内核空间的一个缓冲区,然后再将这些数据拷贝到用户空间,实际上是两次数据拷贝。写回同样也需要经过两次数据拷

    2022年6月17日
    54
  • 云硬盘

    云硬盘

    2021年9月22日
    56
  • linux中sftp默认登录的端口号是多少? sftp通过指定的端口号连接?sftp默认端口号[通俗易懂]

    linux中sftp默认登录的端口号是多少? sftp通过指定的端口号连接?sftp默认端口号[通俗易懂]需求描述:  今天一个同事,遇到个问题,程序连接sftp服务器连接不上,问我端口号是多少,  我想了一下是21还是22,所以就做了测试,发现sftp默认的连接端口号是22,  在此做下记录.操作过程:1.使用sftp默认的端口号进行登录aiuap_cj@cuchc802:~>sftpaisftp@10.124.163.133aisftp@10.124.163.13…

    2025年11月16日
    4
  • 磁盘占用率100%——哪些程序可以禁用(详细版)【还讲到独立显卡、集成显卡、双显卡、固态硬盘卡机卡死卡顿解决】

    磁盘占用率100%——哪些程序可以禁用(详细版)【还讲到独立显卡、集成显卡、双显卡、固态硬盘卡机卡死卡顿解决】我买内存条之前,电脑超级卡的,我不喜欢用完电脑就关机,经常晚上用完就“睡眠、待机”,第二天早上用电脑,一般都打开谷歌浏览器、网易云音乐、有道词典、Eclipse、Oracle、Tomcat、电脑管家、文件资源管理器;我使用Eclipse编写代码,我都输完一行代码,电脑卡的呀,30分钟至一分钟才有反应,这算是快的,最恶心的是“卡至Eclipse软件自己关闭了”!!!所以只能看看哪些服务进程可以禁用…

    2022年6月26日
    27
  • phpstrom2021激活码_通用破解码「建议收藏」

    phpstrom2021激活码_通用破解码,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月17日
    47
  • insertBefore()

    insertBefore()insertBefore()方法将把一个给定的节点插入到一个给定元素节点的给定子节点前面,他返回一个指向新增子节点的引用指针:如上所示,节点newNode将被插入元素节点element并出现在节点t

    2022年8月2日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号