3-26 hdu-1978 How many ways

Problem Description

This is a simple survival game where you control a robot come to the end of the board (n, m) from the starting point of a board of (1,1). Rules of the game is described as follows:
1. The robots start at the starting point of the board and have some energy to the starting point mark.
The robot can go right or down, and every step of a unit of energy consumed.
3. A robot can not stay in place.
4. When the robot has chosen a feasible path, when he went to the end of this path, he will only mark the end of the energy.

As shown above, the robot initially at (1,1) point, and has four units of energy, blue square represents the point he can reach, if he is selected in the path selection in the end is (2,4)

Point, when he reached (2,4) will have one unit of energy when the point, and the next path selection began, until it reaches the (6,6) point.
Our problem is how many ways can a robot come to the end from the beginning. This may be the result of a very large number, the output of modulo 10000.

Input

A first line of input integer T, represents the number of sets of data. For each set of data in the first line of the input two integers n, m (1 <= n, m <= 100). It indicates the size of the board. The next n input lines of m integers e (0 <= e <20).

Output

The total number of results for each group of data output modulo 10000.

Sample Input

1
6 6
4 5 6 6 4 3
2 2 3 1 7 2
1 1 4 6 2 7
5 8 4 3 9 5
7 6 6 2 1 5
3 1 1 3 7 2

Sample Output

3948

Ideas:

Each time from one point to the next point. She has come to an end. So the starting point is that you can go to the end point of the sum.

And so on down.

AC Code:

#include<bits/stdc++.h>

using namespace std;
int a[105][105];
int dp[105][105];
int t,n,m;
int ma(int x,int y){
	if(x==n&&y==m)return 1;
	if(dp[x][y]!=-1)return dp[x][y];
	dp[x][y]=0;
	for(int i=0;i<=a[x][y];i++){
		if(x+i>n)break;
		for(int j=0;i+j<=a[x][y];j++){
			if(y+j>m)break;
			if(i==0&&j==0)continue;
			dp[x][y]+=ma(x+i,y+j);
		}
	}
	return dp[x][y]=dp[x][y]%10000;
}
int main(){
	while(cin>>t){
		while(t--){
			memset(dp,-1,sizeof dp);
			cin>>n>>m;
			for(int i=1;i<=n;i++){
				for(int j=1;j<=m;j++){
					cin>>a[i][j];
				}
			}
			cout<<ma(1,1)<<endl;
		}
	}
	
	return 0;
} 

Source

2008 Hangzhou Electric Team Trials

Published 34 original articles · won praise 6 · views 1338

Guess you like

Origin blog.csdn.net/qq_44669377/article/details/105116237