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
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.
4 2331 1213 1231 3110 4 3332 1213 1232 2120 5 11101 01111 11111 11101 11101 -1
3 0 7Brute 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.HintHint
#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这个函数不能看表面,要知道它是执行到找到目标或者判断找不到目标的那个递归层开始计算的,没计算好一个,就像上一层返回这一层计算的结果,好好看看吧,这个需要点耐心才能消化的,自己脑子转一转就会明白了,擦,还是不懂啊?像找骂是不?这都已经灰常明白了呢,至少我要是第一次看的话会很快就找到的呢- -!亲~不懂的话我倒是要鄙视再鼻屎砸你了,别说我暴力,其实我很温柔!