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;
    }
}

 

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • 遗传算法的应用实例python实现_python遗传算法库

    遗传算法的应用实例python实现_python遗传算法库遗传算法遗传算法是用于解决最优化问题的一种搜索算法。从名字来看,遗传算法借用了生物学里达尔文的进化理论:”适者生存,不适者淘汰“,将该理论以算法的形式表现出来就是遗传算法的过程。问题引入上面提到遗传算法是用来解决最优化问题的,下面我将以求二元函数:defF(x,y): return3*(1-x)**2*np.exp(-(x**2)-(y+1)**2)-10*(x/5-x**3……

  • FRP内网穿透_frp内网穿透原理

    FRP内网穿透_frp内网穿透原理frp点对点udp方式内网穿透ssh,节省服务器流量(2019年5月30日) frpssh安全连接和服务器安全设置(2019年5月29日) frp控制台监控dashboard配置(2019年5月27日) frp内网穿透公网访问本地web服务(2019年5月26日) frp安装教程穿透SSH(2019年5月25日) fr…

  • oracle字段精度修改,oracle number类型增加精度

    oracle字段精度修改,oracle number类型增加精度oracle迁移到sqlserver时,报错-如下图,查找原因,发现是因为有些表number类型没有设精度导致的,解决方法如下,修改表结构加上精度,加上之后就可以了。考虑到有些表有多个字段没有设精度,所以采取以下方式实现。–split函数createorreplacetypetype_splitastableofvarchar2(4000);/createorreplace…

  • 免费的ASP.NET AJAX 培训

    免费的ASP.NET AJAX 培训  好久没有写blog了,在这里感慨一下先 ;-))  最近BradAbrams 做了个关于ajax的E-Learning ,内容通俗易懂,估计这个教程2个小时可以完成。在学Ajax的朋友不妨看一下;点击here ,关于这个elearning的详细信息FreeASP.NETAJAXOnlineTraining在这里也郁闷一下,在asp.netajax1.0中列表

  • vmware中安装centos_虚拟机系统安装教程

    vmware中安装centos_虚拟机系统安装教程一、前言最近有网友反应初学Linx不会安装,找了许多教程不是太全面,总会遇到一些要不是启动不了,要不是连不上网,各种问题,为了让大家能够顺利的安装,小乐写了一个非常详细的教程,让大家少入坑。二、背

  • arcgis python二次开发_arcgis二次开发python_arcgis二次开发是什么_arcgis二次开发

    arcgis python二次开发_arcgis二次开发python_arcgis二次开发是什么_arcgis二次开发VS2013中ArcGIS二次开发部分问题问题解决方法VS2013中新建项目时没有ArcGIS模板解决办法:安装ArcGIS10.x会自动生成C:\ProgramFilesx86\MicrosoftVisualStudio10.0\Common7\IDE\ItemTemplates\CSharp\ArcGIS文件夹和C:\ProgramFilesx86\MicrosoftVi…

发表回复

您的电子邮箱地址不会被公开。

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