Problem DescriptionKing X has a underground palace treasure trove. is a matrix of n x m lattices. Put a baby in every grid. Each treasure is tagged with a value.
The entrance to the underground palace is in the upper left corner and exits in the lower right corner.
Xiaoming was taken to the entrance of underground palace, and the king demanded that he only walk right or down.
walk through a lattice, if the treasure in the grid is worth more than Xiaoming's hand any treasure value is big, xiaoming can pick up it (of course, can not take).
when Xiao Ming walked to the exit, if the baby in his hand happens to be a K-piece, then these treasures can be given to xiaoming.
please help Xiao Ming calculate, in a given situation, he has how many different ways of action to get this K-piece baby. Input FormatEnter a line of 3 integers separated by spaces: N M K (1<=n,m<=50, 1<=k<=12)
Next there are n rows of data, each line having m integer Ci (0<=ci<=12) representing the value of the treasure on this latticeoutput FormatAn integer is required to indicate the number of action plans that fetch the K baby. The number may be large, outputting the result of its 1000000007 modulo. Sample Input2 2 2
1 2
2 1Sample Output2Sample Input2 3 2
1 2 3
2 1 5Sample Output - Violent Dfs will time out, can DP or memory search .... Understanding can ... Write your own may .... GG ...in this way, it is still more interesting to remember the search, because you don't have to consider so many boundaries ...See code for ideas.DP:
/* Blue Bridge Cup previous questions underground palace treasure DP Status: Dp[i][j][num][val] indicates that from the beginning (1, 1) to (I, j), has taken Num treasures, the maximum value is the number of Val scheme. Initial state: dp[1][1][0][0] = 1; DP[1][1][1][MP[1][1]] = 1; transfer equation: from the upper or left lattice transfer, see Code; */#include <stdio.h> #include <string.h> #include < iostream>using namespace std; #define MOD 1000000007int dp[55][55][15][15];int mp[55][55];int main () {int n, m, K; while (~SCANF ("%d%d%d", &n, &m, &k)) {for (Int. I=1; i<=n; ++i) {for (int j=1; j<=m; ++J) {scanf ("%d", &mp[i][j]); }} memset (DP, 0, sizeof (DP)); Dp[1][1][0][0] = 1; DP[1][1][1][MP[1][1]] = 1; for (int i=1, i<=n; ++i) {for (int j=1; j<=m; ++j) {dp[i][j][0][0] + = (Dp[i-1][j][0][0] + dp[i][j-1][0][0]); DP[I][J][0][0]%= mod; for (int num=1, num<=k; ++num) {for (int val=0; val<=12; ++val) {dp[i][j ][num][val] + = (Dp[i-1][j][num][val] + dp[I][j-1][num][val]); Dp[i][j][num][val]%= mod; } if (num = = 1) {Dp[i][j][1][mp[i][j]] + = dp[i-1][j][0][0]; DP[I][J][1][MP[I][J]]%= mod; DP[I][J][1][MP[I][J]] + = dp[i][j-1][0][0]; DP[I][J][1][MP[I][J]]%= mod; } else {for (int t=0; t<mp[i][j]; ++t) {Dp[i][j] [Num] [Mp[i][j]] + = dp[i-1][j][num-1][t]; DP[I][J][NUM][MP[I][J]]%= mod; DP[I][J][NUM][MP[I][J]] + = dp[i][j-1][num-1][t]; DP[I][J][NUM][MP[I][J]]%= mod; }}}}} int ans = 0; for (int i=0; i<=12; ++i) {ans + = dp[n][m][k][i]; Ans%= MoD; } printf ("%d\n", ans); } return 0;}
Memory Search:
/* Blue Bridge Cup previous questions underground palace take treasure Dp[i][j][num][k] to position (I, J), take the first num treasure, the maximum treasure value is K, can reach the end of the route plan number. DFS timed out. Memory search ... */#include <stdio.h> #include <string.h> #include <iostream>using namespace std; #define MoD 1000000007int dp[55][55][15][15];int N, M, k;int mp[55][55];int dfs (int nowx, int nowy, int cnt, int nowmax) {if (dp[n OWX][NOWY][CNT][NOWMAX+1]! =-1) {return dp[nowx][nowy][cnt][nowmax+1]; } int ans = 0; if (nowx = = N-1 && Nowy = = m-1) {if (Mp[nowx][nowy] > Nowmax) {if (cnt = = k | | cnt = = k-1) ans++; Ans%= MoD; } else if (cnt = = k) ans++; Ans%= MoD; return dp[nowx][nowy][cnt][nowmax+1] = ans; if (Nowx+1 < n) {if (Mp[nowx][nowy] > Nowmax) {ans + = DFS (nowx+1, Nowy, Cnt+1, Mp[nowx][now Y]); Ans%= MoD; } ans + = DFS (nowx+1, Nowy, CNT, Nowmax); Ans%= MoD; } if (Nowy+1 < m) {if (Mp[nowx][nowy] > Nowmax) { Ans + = DFS (nowx, nowy+1, Cnt+1, Mp[nowx][nowy]); Ans%= MoD; } ans + = DFS (nowx, nowy+1, CNT, Nowmax); Ans%= MoD; } return dp[nowx][nowy][cnt][nowmax+1] = ans;} int main () {while (~scanf ("%d%d%d", &n, &m, &k)) {memset (DP,-1, sizeof (DP)); for (int i=0; i<n; ++i) {for (int j=0; j<m; ++j) {scanf ("%d", &mp[i][j]); }} int ans = DFS (0, 0, 0,-1); printf ("%d\n", ans); } return 0;}
Blue Bridge Cup previous questions underground palace take treasure DP or memory search