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


相关推荐

  • 人工智能猴子摘香蕉代码_猴子妈妈有14个香蕉

    人工智能猴子摘香蕉代码_猴子妈妈有14个香蕉只有简单的状态显示#include"iostream"usingnamespacestd;voidAT(charmonkeyplace,charboxplace){cout&lt;&lt;"AT(monkey,"&lt;&lt;monkeyplace&lt;&lt;")"&lt;&lt;endl;cout&lt;&

    2022年9月26日
    0
  • vs2010注册密钥_vs2012ultimate密钥

    vs2010注册密钥_vs2012ultimate密钥MicrosoftVisualStudioUltimate2012旗舰版有效注册密钥:YKCW6-BPFPF-BT8C9-7DCTH-QXGWC

    2022年10月14日
    0
  • django 用户注册_支付宝注册用户数量

    django 用户注册_支付宝注册用户数量前言我们使用django创建用户可以使用注册接口的方式,也可以使用django自带的后台管理系统,这里就介绍使用后台管理系统创建用户admin后台管理系统在使用之前我们可以使用第三方的插件,来美

    2022年7月31日
    3
  • 仿朋友圈相册图片选择以及画廊效果「建议收藏」

    仿朋友圈相册图片选择以及画廊效果「建议收藏」仿朋友圈相册图片选择以及画廊效果1.效果展示2.导入相关第三方库依赖3.编写选择图片页面a.编写布局b.编写Activityc.相册选择工具类部分代码d.相册4宫图适配器4.编写画廊页面a.编写画廊页面b.编写Activityc.画廊适配器5.源码1.效果展示该demo适配Android6、7、10。画廊效果,支持缩放效果。视频展示:(等我B站视频审核通过再来修改)部分截图:文章有点长,如果没时间就拉到最底下下载源码,再给个一键三联哈(* ̄︶ ̄)2.导入相关第三方库依赖站在巨人的肩膀上,

    2022年5月22日
    32
  • 7000词汇这么背我比较可以接受,连续看20天足以[通俗易懂]

    7000词汇这么背我比较可以接受,连续看20天足以[通俗易懂]
    16天记住7000考研词汇(第一天)

    1.WithmyownearsIclearlyheardtheheartbeatofthenuclearbomb.
    我亲耳清楚地听到原子弹的心脏的跳动。
    2.Nextyearthebeardedbearwillbearadearbabyintherear.
    明年,长胡子的熊将在后方产一头可爱的小崽.
    3.EarlyIsearchedthr

    2022年8月24日
    5
  • tomcat 部署war包的两种方法「建议收藏」

    tomcat 部署war包的两种方法「建议收藏」编辑tomcat/conf/server.xml文件viserver.xml->ESC+i->编辑后->ESC+:wq!第一种:添加一个Service                     connectionTimeout=”20000″                redirectPort=”9443″/>    

    2022年5月22日
    130

发表回复

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

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