Hdu 1978 How many ways

Hdu 1978 How many ways (memory search)

Link Title: http://acm.hdu.edu.cn/showproblem.php?pid=1978
Here Insert Picture Description
Here Insert Picture Description
solving ideas: vis [n] [m] is set, the other all set to 0, the memory search again found on the list;
Code as follows:

#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;
} 
Published 35 original articles · won praise 3 · Views 872

Guess you like

Origin blog.csdn.net/weixin_43823753/article/details/105056231