HDU--杭电--1208--Pascal's Travels--记忆化搜索--这个也和机器人那个一样算半个DP吧

来源:互联网 发布:centos开启snmp 编辑:程序博客网 时间:2024/06/06 16:25

Pascal's Travels

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1331    Accepted Submission(s): 591


Problem 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.


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 2^63 paths for any board.
 


 

Sample Input
423311213123131104333212131232212051110101111111111110111101-1
 


 

Sample Output
307
Hint
Hint
Brute force methods examining every path will likely exceed the allotted time limit. 64-bit integer values are available as "__int64" values using the Visual C/C++ or "long long" values using GNU C/C++ or "int64" values using Free Pascal compilers.

 

#include <iostream>
#include <cstdio>
using namespace
std;

__int64
map[44][44],dp[44][44],n;  //dp是记录路径数量的
__int64
DP(__int64 x,__int64 y)
{

    int
i;
    if
(x==n-1&&y==n-1)return 1;  //找到终点就算一条路,就返回1
    if(map[x][y]==0||dp[x][y])return dp[x][y];  //map肯定不能为0撒,这还要意思问我为什么?为0就不能动看,还有DP不为0就是已经确定数量了
    if(x+map[x][y]<n)  //题目说了只能走直线,这样方便多了,这样就是要用两if来分别横着和竖着走了
    dp[x][y]+=DP(x+map[x][y],y);  //当前点的dp要加上走到的那一点的dp,就把DP看成求出里面坐标的dp值的函数咯
    if
(y+map[x][y]<n)
    dp[x][y]+=DP(x,y+map[x][y]);

    return
dp[x][y];  //结束完之后返回查找完成的当前点
}

int
main (void)
{

    __int64
i,j,k,l;
    char
c;
    while
(~scanf("%I64d%*c",&n)&&n!=-1)
    {

        for
(i=0;i<n;i++)
        {

            for
(j=0;j<n;j++)
                scanf("%c",&c),map[i][j]=c-48,dp[i][j]=0;
            getchar();
        }

        printf("%I64d\n",DP(0,0
));  //是从(.,.)开始走到,所以从就从这点开始走,就把DP当作返回当前点的路径数的函数咯
    }

    return
0;
}

  这个和上面写的那个博客几乎是一样的,唉~~这原理你去看那个OK?我可不会悠着悠着来告诉你DP这个函数不能看表面,要知道它是执行到找到目标或者判断找不到目标的那个递归层开始计算的,没计算好一个,就像上一层返回这一层计算的结果,好好看看吧,这个需要点耐心才能消化的,自己脑子转一转就会明白了,擦,还是不懂啊?像找骂是不?这都已经灰常明白了呢,至少我要是第一次看的话会很快就找到的呢- -!亲~不懂的话我倒是要鄙视再鼻屎砸你了,别说我暴力,其实我很温柔!