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


相关推荐

  • hibernate的几种主键

    hibernate的几种主键

    2021年9月6日
    44
  • linux查看jdk安装目录

    linux查看jdk安装目录1 安装包方式安装的 jdk 如果是现在安装包安装的话 一般都会配置环境变量 echo JAVA HOME 或者查看 etc profilevi etc profile nbsp 2 yum 安装 nbsp 查看安装目录 nbsp nbsp 本人比较懒 就用的 yum 安装的 nbsp nbsp 先找到 javad 的执行目录 nbsp nbsp whereisjava 通过执行文件找到链接文件 ls lrt usr bin

    2025年8月25日
    5
  • maven镜像还有不支持发型版本5

    maven镜像还有不支持发型版本5maven镜像<mirror> <id> alimaven </id> <mirrorOf> central </mirrorOf> <name> aliyunmaven </name> <url> http://maven.aliyun.com/nexus/content/repositories/central/ </u

    2022年8月21日
    7
  • python的学生信息管理系统_学员信息管理系统设计

    python的学生信息管理系统_学员信息管理系统设计一.系统需求使用面向对象编程思想完成学员管理系统的开发,具体如下:系统要求:学员数据存储在文件中系统功能:添加学员、删除学员、修改学员信息、查询学员信息、显示所有学员信息、保存学员信息及退出系统等功能。程序文件如下:程序入口文件:main.py学员文件:student.py管理系统⽂文件:managerSystem.pymain.py#1.导入managerSystem模块frommanagerSystemimport*#2.启动学员管理系统if__name__

    2026年1月30日
    4
  • 硬核!高频Linux命令大总结,建议收藏~

    硬核!高频Linux命令大总结,建议收藏~前言记得不久前跟大家大分享了一波个人在平时日常工作、学习、开发、写文字、做视频等过程中,一些好用高效的在线工具和网站,并且把自己的浏览器收藏夹书签离线文件都导出给大家了。很多小伙伴后台反馈还不错,说书签一导入后,很多工具确实挺好用,主要省了很多找资源和整理的时间。今天继续分享,最近花了不少时间把平时开发过程中常用的一些Linux系统命令给做了一个大整理,形成一个常用高频Linux速查备忘录。有了它,还怕Linux操作系统常用操作和命令记不住么?接下来直接上菜吧。注:本文GitHubhtt

    2022年5月8日
    39
  • 51单片机入门教程(5)——定时器中断

    51单片机入门教程(5)——定时器中断51 单片机入门教程 5 定时器中断一 中断的概念二 定时器中断 2 1 软件延时的不足 2 2 中断寄存器 2 2 1 中断允许控制寄存器 IE2 2 2 定时器工作方式寄存器 TMOD2 2 3 定时器控制寄存器 TCON2 2 4 定时器初值寄存器 THx TLx2 3 定时器中断程序写法写在开头 中断是包括单片机在内的所有微处理器很重要的功能之一 初学单片机必须这一部分的知识 一 中断的概

    2025年11月17日
    3

发表回复

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

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