POJ2704 DP + 记忆化搜索 + 注意中间存储数据用long long不然WA

来源:互联网 发布:网络买重庆时时彩违法 编辑:程序博客网 时间:2024/06/05 04:12


1)注意中间存储的数可能超出int范围,所以开long long ,否则越界以后,给一个随机数,就会WA。

虽然方格最大是34×34,每次选择向下或者向右,那么路径数最多可能为2^(34+34-1)-X<2^63

2^(34+43+1) 可以这么理解,把正方形从左上角起点开始拆成二叉树的形式来看,诸如

1 2 3 4

2 3 4 5

3 4 5 6

4 5 6 7

1、2 、3 、4 、5、 6、 7 依次为二叉树的第一层、第二层、...第七层,那么4×4的矩形的路径计算次数就是,2^(4+4-1)-X,X是最右的边和最下的边不断超出边界的路径数,则34×34同理,2^(34+34-1)-X,而这个结果最大小于2^63,别问我X怎么算,用心去感受...(没推出来..反正结果不会比2^(34+34-1)小,按它去准备就先要反应过来开long long了)

#include <iostream>#include <string.h>using namespace std;const int maxn=40;int boards[maxn][maxn];long long int dp[maxn][maxn];int flag[maxn][maxn];int n;//int sum=0;用sum记数恰恰没有用到DP不断利用子结构的特性,dp[i][j]是经过点(i,j)有几条路可以到达终点,父节点不断被子节点更新,不断返回通过该点的路径数,并最终返回从起点出发到达终点的路径数long long int DFS(int x,int y){//注意long long,不然WAif(x==n&&y==n){//sum++;//cout<<x<<" "<<y<<endl;return 1;}if(flag[x][y]==1){return dp[x][y];}if(boards[x][y]==0){flag[x][y]=1;dp[x][y]=0;return 0;}for(int i=0;i<=1;i++){int temp_x;int temp_y;if(i==0){//右temp_x=x+0;temp_y=y+boards[x][y];}if(i==1){//下temp_x=x+boards[x][y];temp_y=y+0;flag[x][y]=1;}if(1<=temp_x&&temp_x<=n&&1<=temp_y&&temp_y<=n){long long int temp=DFS(temp_x,temp_y);if(temp!=0){dp[x][y]+=temp;}}}return dp[x][y];}int main(){while(cin>>n&&n!=-1){//sum=0;memset(flag,0,sizeof(flag));memset(dp,0,sizeof(dp));for(int i=1;i<=n;i++){string number;cin>>number;for(int j=1;j<=number.size();j++){boards[i][j]=number[j-1]-'0';//cout<<boards[i][j]<<" ";}//cout<<endl;}long long int g=DFS(1,1);//注意long longcout<<g<<endl;}}

2)

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 1Figure 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

423311213123131104333212131232212051110101111111111110111101-1

Sample Output

307

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.


1 0
原创粉丝点击