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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python hexdump_hexdump用法[通俗易懂]

    python hexdump_hexdump用法[通俗易懂]可用参数[-bcCdovx][-eformat_string][-fformat_file][-nlength][-sskip]file…参数含义:-b单字节八进制显示,十六进制显示偏移量,每行显示16个字符,每字符用三位显示,不足补零,列间以空格分隔-c单字节字符显示,十六进制显示偏移量,每行显示16个字符,每字符三位显示,不足补空格,列间以空格分隔-C标准十六进制…

    2022年9月21日
    1
  • java 向上取整方法 Math.ceil() 用法、源码分析

    java 向上取整方法 Math.ceil() 用法、源码分析刷题用到了,正好好好看看源码。

    2022年6月21日
    55
  • MessageDigest详解

    MessageDigest详解一、概述java.security.MessageDigest类用于为应用程序提供信息摘要算法的功能,如MD5或SHA算法。简单点说就是用于生成散列码。信息摘要是安全的单向哈希函数,它接收任意大小的数据,输出固定长度的哈希值。关于信息摘要和散列码请参照《数字证书简介》MessageDigest 通过其getInstance系列静态函数来进行实例化和初始化。MessageDigest对象通…

    2022年6月29日
    25
  • 从高考落榜生到网络专家

    从高考落榜生到网络专家成功的背后,有着许多不为人知的故事,而正是这些夹杂着泪水和汗水的过去,才成就了一个个走 向成功的普通人——凌晨两点半,早已习惯了一个人坐在电脑前的我,望着屏幕,任思绪在暗夜的包容 下静静流淌,时光仿佛又定格在三年多前的那一刻:“283分”。那是被中国万千学子称为“黑色七 月”中的一天,下班回家的母亲从家门打开后说出的一个数字,虽然早知道自己不会考上大学,但如此 的成绩也多少出乎自己

    2025年8月22日
    2
  • Opencv 中 waitkey()& 0xFF,“0xFF”的作用解释

    Opencv 中 waitkey()& 0xFF,“0xFF”的作用解释这几日学习OpenCV,刚碰到这个表达式时,对于0xFF的作用不太理解,难道下面两个语句还有区别?(Esc的ASCII码为27,即判断是否按下esc键)ifcv2.waitkey(30)==27ifcv2.waitkey(30)&0xff==27疑惑首先&运算即“and”运算。其次0xFF是16进制数,对应的二进制数为11111111。然后cv2….

    2022年6月19日
    30
  • goland破解激活码【在线破解激活】

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

    2022年3月16日
    91

发表回复

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

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