<title>Blue Bridge Cup: Underground Palace take Treasure</title> Blue Bridge Cup: Underground Palace table of Contents
- The wrong wording
- After improvement
- Improve again
- To improve by means of memory.
Title: Underground Palace Take Treasure
King 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.
"Data Format"
Enter 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 lattice
An 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.
For example, enter:
2 2 2
1 2
2 1
The program should output:
2
Again, for example, enter:
2 3 2
1 2 3
2 1 5
The program should output:
14
The wrong wording
/** createtime:2015-04-06 10:29:05 */#include <cstdio>Const int MAXN= 80;int k;int N;int m;int Count;int g[MAXN] [MAXN];int Cur_max;void DFS(int I,int J,int Step) {if(I > N | | j > M | | step > K) {return; }if(step = = k) {count++; }if(G[i][j] > Cur_max) {int Ori_max= Cur_max; Cur_max = G[i][j]; DFS (I+1, J, step+1); DFS (i, j+1, step+1); Cur_max = Ori_max;// BacktrackingDFS (i+1, J, Step); DFS (i, j+1, step); }Else{DFS (i+1, J, Step); DFS (i, j+1, step); }}int Main(void) {Cur_max = 0; n = 2; m = 3; K = 2; Count = 0; G[1][1] = 1; G[1][2] = 2; G[1][3] = 3; G[2][1] = 2; G[2][2] = 1; G[2][3] = 5; DFS (1, 1, 0); printf"%d\n", count);return0;}
After improvement
#include <cstdio>Const int MAXN= 80;int k;int N;int m;int Count;int g[MAXN] [MAXN];int Cur_max;void DFS(int I,int J,int Step) {if(I > N | | j > M | | step > K) {return; }if(n = = i && m = = j) {if(step = = k) {count++; }Else if(step = = k-1) {if(G[i][j] > Cur_max) {count++; } }return; }if(G[i][j] > Cur_max) {int Ori_max= Cur_max; Cur_max = G[i][j]; DFS (I+1, J, step+1); DFS (i, j+1, step+1); Cur_max = Ori_max;// BacktrackingDFS (i+1, J, Step); DFS (i, j+1, step); }Else{DFS (i+1, J, Step); DFS (i, j+1, step); }}int Main(void) {Cur_max = 0; n = 2; m = 3; K = 2; Count = 0; G[1][1] = 1; G[1][2] = 2; G[1][3] = 3; G[2][1] = 2; G[2][2] = 1; G[2][3] = 5; DFS (1, 1, 0); printf"%d\n", count);return0;}
Improve again
#include <iostream>using namespace STD;Const int MAXN= 80;int k;int N;int m;Long Long Count;int g[MAXN] [MAXN];void DFS(int I,int J,int Step,int Max) {if(I > N | | j > M | | step > K) {return; }if(i = = N && j = = m) {if(step = = k) {count++; }Else if(Step = = K-1 && max < g[i][j]) {count++; } }if(G[i][j] > Max) {DFS (i+1, J, Step+1, G[i][j]); DFS (i, j+1, step+1, G[i][j]); } dfs (I+1, J, step, Max); DFS (i, j+1, step, max);}int Main(void) {cin >> n >> m >> K; for(int I= 1; I <= N; i++) { for(int J= 1; J <= M; J + +) {cin >> g[i][j]; }} DFS (1, 1, 0,-1); cout << count% 1000000007 << Endl;return0;}
To improve by means of memory.
/** createtime:2015-04-06 10:29:05 */#include <iostream>#include <cstring>#include <cstdio>using namespace STD;Const int MAXN= 80;Const int MoD= 1000000007;int k;int N;int m;int g[MAXN] [MAXN];int Mem[MAXN] [MAXN] [15] [15];int DFS(int I,int J,int Step,int Max) {if(mem[i][j][step][max+1]! =-1) {returnMEM[I][J][STEP][MAX+1]; }if(I > N | | j > M | | step > K) {return0; }if(i = = N && j = = m) {if(step = = k) {returnMEM[I][J][STEP][MAX+1] = 1; }Else if(Step = = K-1 && max < g[i][j]) {returnMEM[I][J][STEP][MAX+1] = 1; } }int s= 0;if(G[i][j] > Max) {s + = DFS (i+1, J, Step+1, G[i][j]); s = s% mod; s + = DFS (i, j+1, step+1, G[i][j]); s = s% mod; } s + = Dfs (i+1, J, step, Max); s = s% mod; s + = DFS (i, j+1, step, Max); s = s% mod;returnMEM[I][J][STEP][MAX+1] = s;}int Main(void) {memset (mem,-1,sizeof(MEM)); CIN >> n >> m >> K; for(int I= 1; I <= N; i++) { for(int J= 1; J <= M; J + +) {cin >> g[i][j]; } }int s= DFS (1, 1, 0,-1); cout << s% mod << Endl;return0;}
Blue Bridge Cup: Underground Palace take Treasure