HDU 1978 How many ways (basic memory-based search)
How many ways
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission (s): 3475 Accepted Submission (s): 2041Problem Description this is a simple survival game where you control a robot from the starting point () of a board to the ending point (n, m) of the Board ). The rules of the game are described as follows:
1. The robot starts at the starting point of the Board and has the energy marked by the starting point.
2. robots can only go right or down, and each step consumes one unit of energy.
3. Robots cannot stay in place.
4. When the robot selects a feasible path, when he reaches the end of the path, he will only have the energy marked by the end point.
For example, a robot starts at () and has 4 units of energy. The blue square indicates the point he can reach. If the end point he chooses in this path is)
Point. When it reaches (2, 4), it will have 1 unit of energy and start the next path selection until it reaches (6, 6.
The question is how many ways robots can go from the start point to the end point. This may be a large number. The output result is a modulo of 10000.
The first line of Input is an integer T, indicating the number of data groups.
For each group of data, enter two integers n, m (1 <= n, m <= 100) in the first row ). Indicates the size of the Board. Next, enter n rows, with m integers e (0 <= e <20) in each row ).
Output is the result of Modulo 10000 for the total number of data Output modes in each group.
Sample Input
16 64 5 6 6 4 32 2 3 1 7 21 1 4 6 2 75 8 4 3 9 57 6 6 2 1 53 1 1 3 7 2
Sample Output
3948
Question link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1978
Question Analysis: in fact, the meaning of the question is not very clear. The points that can be reached are what can be reached in a path, after knowing this, dp [I] [j] indicates the number of solutions from column j of row I to point (n, m). At the beginning, dp [n] [m] = 1, then you can perform the Memory search.
#include
#include
int const MAX = 150;int const MOD = 10000;int n, m;int a[MAX][MAX], dp[MAX][MAX];int DFS(int x, int y){ if(x > n || y > m || x < 1 || y < 1) return dp[x][y] = 0; if(dp[x][y]) return dp[x][y]; for(int i = 0; i <= a[x][y]; i++) for(int j = 0; i + j <= a[x][y]; j++) if(i || j) dp[x][y] = (dp[x][y] % MOD + DFS(x + i, y + j) % MOD) % MOD; return dp[x][y];}int main(){ int T; scanf("%d", &T); while(T--) { scanf("%d %d", &n, &m); for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) scanf("%d", &a[i][j]); memset(dp, 0, sizeof(dp)); dp[n][m] = 1; printf("%d\n", DFS(1, 1)); }}