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


相关推荐

  • 13款国内外知名PHP集成环境的优缺点分析,PHP集成环境推荐、PHP绿色集成环境推荐「建议收藏」

    13款国内外知名PHP集成环境的优缺点分析,PHP集成环境推荐、PHP绿色集成环境推荐「建议收藏」在本地测试网站,有个集成环境直接测试还是蛮方便的,下面向各位推荐国内和国外各种牛逼的php集成环境 排名不分先后! Xampp集成环境下载解压就能使用了,还支持苹果系统,溜的飞起。英文界面,用着B格也提高了不少。优点:支持的系统多啊,软件使用简单,可视化界面缺点:没有集成VC运行库,遗憾  然后就是老牌的apm

    2022年6月28日
    29
  • String与StringBuffer的区别

    String与StringBuffer的区别String与StringBuffer的区别简单地说,就是一个变量和常量的关系。StringBuffer对象的内容可以修改;而String对象一旦产生后就不可以被修改,重新赋值其实是两个对象。StringBuffer的内部实现方式和String不同,StringBuffer在进行字符串处理时,不生成新的对象,在内存使用上要优于String类。所以在实际使用时,如果经常需要对一个字符串进行修改,例如插入、删除等操作,使用StringBuffer要更加适合一些。String:在String类中没有用来改变已有字

    2022年9月21日
    2
  • 概率的定义与性质_概率有哪些性质

    概率的定义与性质_概率有哪些性质概率的统计学定义:概率的公理化公式:

    2022年8月31日
    1
  • hibernate它5.many2one单向

    hibernate它5.many2one单向

    2022年1月4日
    36
  • 如何运行SpringBoot项目

    如何运行SpringBoot项目最近在Ecplise上面写了一个简单的SpringBoot的测试项目,SpringBoot里面是有主函数的:我们知道的是在Ecplise上面找到这个主函数然后runas-&gt;javaApplication就可以了但是总不能一直不脱离Ecplise,总要出来自己单练的第一步:我就新建的一个文件夹boottest,然后右键导出整个工程:导出的是jar包,然后我们看…

    2022年10月13日
    2
  • 10个常用的3D建模软件,作为3D建模的软件东西很杂很碎,还需多练习才最重要「建议收藏」

    10个常用的3D建模软件,作为3D建模的软件东西很杂很碎,还需多练习才最重要「建议收藏」很多人都会好奇,电脑是怎么将手绘的2D图形变成3D的实际物品的?究竟是什么神奇魔法能够瞬间将我们的想法变成现实的呢?今天来和大家介绍下工业设计师经常会用到的10个3D建模软件。SolidworksSolidworks是工业设计师经常会用到的一款建模软件。SolidWorks是一款在MircosoftWindows上才能运行的建模计算机辅助设计和计算机辅助工程的计算机程序,由DassaultSystemes开发和发布。这是一款很常见的很普遍的工业设计建模软件,如果你以后有机会在国外找工

    2022年5月12日
    75

发表回复

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

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