Hdu 1978 How many ways

Hdu 1978 How many ways(记忆化搜索)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1978
在这里插入图片描述
在这里插入图片描述
解题思路:vis[n][m]置1,其他全置0,记忆化搜索搜一遍就行了;
代码如下:

#include<bits/stdc++.h>
using namespace std;
int num[105][105],vis[105][105];
int n,m;
inline bool check(int x,int y){
	if(x>=0&&x<n&&y>=0&&y<m)return true;
	return false;
}
inline int dfs(int x,int y){
	if(vis[x][y])return vis[x][y];//走过了直接返回值,不用再找一遍
	int res=0;
	for(int i=0;i<=num[x][y];i++){
		for(int j=0;j<=num[x][y]-i;j++){
			if(i==0&&j==0)continue;
			if(check(x+i,y+j)){
				res=(res+dfs(x+i,y+j))%10000;
			}
		}
	}
	return vis[x][y]=res;
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		scanf("%d %d",&n,&m);
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				scanf("%d",&num[i][j]);
				vis[i][j]=0;
			}
		}
		vis[n-1][m-1]=1;
		printf("%d\n",dfs(0,0));
	}
	return 0;
} 
发布了35 篇原创文章 · 获赞 3 · 访问量 872

猜你喜欢

转载自blog.csdn.net/weixin_43823753/article/details/105056231