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


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,不然WA

	if(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 long
		cout<<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 2 63 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.


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值