Blue Bridge Cup 2014 undergraduate C + + Group B underground Palace take treasure dfs+ memory Search

Source: Internet
Author: User
Tags ming

Previous Questions underground palace take treasure

Time limit: 1.0s memory limit: 256.0MB

Problem description

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.

Input 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

Output format

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.

Sample input

2 2 2
1 2
2 1

Sample output

2

Sample input

2 3 2
1 2 3
2 1 5

Sample output

14

The question is still quite obvious, and the number of solutions to be returned after the deep search is complete, but the question of good intentions is to be fully correct or learn to pay attention to many aspects when dealing with it.

First of all, first of all, to write its state transition equation , we ask for the solution is from the origin of the greatest value of the treasure v start through a different path to the end of the path to get the total number of K treasures , let's write this state as

d (1,1,k ( initial 0, not take the baby ), -1 ( at this time no treasure, the value is recorded as -1))

Then from this state, it can be transferred to the following decision

In addition, the state transfer equation can be used to decompose the original problem into several sub-problems that are the same as the original problem, and the scale becomes smaller.

The second question, if only in this case, there must be a repetition of the problem, for this it is necessary to take a memory search method, for the already computed nodes to save its value , on this topic, must be a four-dimensional array to save , because its state is related to the position, the number and the current maximum value of the treasure . The value is passed directly when the node is iterated over.

The third question, details determine success or failure!!

1. Type with long long is safe, because even if the MoD, but if the two number plus it may be super Int.

2. A small mistake that I did not pay attention to when I did it myself, resulting in the neglect of the end always being wrong. I mistakenly initialized the r[maxn][maxn][15][15] to 0, which is definitely not true, because it is possible to go in some way without a solution, or 0, and the initial value must be assigned to-1.

3. The initial value for a treasure is-1 , the best way to deal with this is that the final deposit is the fourth dimension of the subscript after moving one

4. It is also important to note that K up to a maximum of does not exceed this value, so the third dimension is only That 's enough.

Others look at the code:

1#include <iostream>2#include <cstdio>3#include <cstring>4 #defineMAXN 605 #defineMOD%10000000076 using namespacestd;7 Long Longr[maxn][maxn][ the][ the];//Save State8 Long LongMAP[MAXN][MAXN];//Initial Map9 Long LongM,n,num;Ten Long LongDfsLong LongILong LongJLong LongKLong Longv) One { A     Long Longs=0, T; -     if(r[i][j][k][v+1]!=-1) -         returnr[i][j][k][v+1]; the     if(i==m&&j==n)//reach the end -     { -         if(k==num) -         { +r[m][n][k][v+1]=1; -             returnr[m][n][k][v+1]; +         } A         Else if(k==num-1&&map[m][n]>v) at         { -r[m][n][k][v+1]=1; -             returnr[m][n][k][v+1]; -         } -         Else -         { inr[m][n][k][v+1]=0; -             returnr[m][n][k][v+1]; to         } +     } -     Else the     { *         if(map[i][j]>v) $         {Panax Notoginsengt=Map[i][j]; -             if(i+1<=m) theS= (S+dfs (i+1, j,k+1, T) Mod+dfs (i+1, J,k,v) MoD) mod; +             if(j+1<=N) AS= (S+dfs (i,j+1, K +1, T) Mod+dfs (i,j+1, K,v) MoD) mod; the         } +         Else -         { $             if(i+1<=m) $S= (S+dfs (i+1, J,k,v) MoD) mod; -             if(j+1<=N) -S= (S+dfs (i,j+1, K,v) MoD) mod; the         } -r[i][j][k][v+1]=s MOD;Wuyi         returnr[i][j][k][v+1]; the     } - } Wu intMain () - { About //freopen ("data.in", "R", stdin); $ //freopen ("Data.out", "w", stdout); -memset (r,-1,sizeof(R)); -     Long Longi,j,p,q; -Cin>>m>>n>>num; A      for(i=1; i<=m;i++) +          for(j=1; j<=n;j++) theCin>>Map[i][j]; -Dfs1,1,0,-1); $cout<<r[1][1][0][0]<<Endl;  the     return 0; the}
View Code

Blue Bridge Cup 2014 undergraduate C + + Group B underground Palace take treasure dfs+ memory Search

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.