Underground Palace Treasure Blue Bridge Cup memory search DFS

Source: Internet
Author: User


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

1#include <stdio.h>2#include <string.h>3 #defineN 10000000074 intn,m,k;5 intmap[ -][ -];6 intvis[ -][ -][ the][ the];//The status xy coordinates are recorded in the VIS array having the maximum value of the treasure (these 4 can be described in detail without a possibility)7 //if Vis[3][4][5][6]=7 is in map[3][4] and the body has 5 treasures of the maximum value is 6 is to reach the end of the 7 path8 9 intDfsintXintYintNumintMax//The current position has the maximum value of the treasures possessed by the number of treasuresTen { One     if(vis[x][y][num][max+1]!=-1)//because the treasure is likely to be 0, it is possible to define max with a minimum of -1 which leads to the inability to use as subscript actually if the value of the treasure in the test data is not 0 will be all +1 removed is also possible A     { -         returnvis[x][y][num][max+1];  -}//memory refers to the above the     if(x==n&&y==m) -     { -         if(num==k)returnvis[x][y][num][max+1]=1;//There are 1 scenarios to meet the current point-to-target criteria -         Else if(num==k-1&&max<map[x][y])returnvis[x][y][num][max+1]=1;//There are 1 options to meet the current point-to-target criteria. +         Else returnvis[x][y][num][max+1]=0;//There are 0 scenarios for the current point-to-target without satisfying the condition -     } +     Long Longs=0; A     if(x+1<=n)//can go down at     { -         if(max<Map[x][y]) -         { -S+=dfs (x+1, y,num+1, Map[x][y]); -s%N; -         } inS+=dfs (x+1, Y,num,max); -s%N; to     } +     if(y+1<=m) -     { the         if(max<Map[x][y]) *         { $S+=dfs (x,y+1, num+1, Map[x][y]);Panax Notoginsengs%N; -         } theS+=dfs (x,y+1, Num,max); +s%N; A     } the     returnvis[x][y][num][max+1]=s%N;; + } - intMainintargcChar Const*argv[]) $ { $ //freopen ("C:\\users\\cong\\desktop\\input.txt", "R", stdin); - //freopen ("C:\\users\\cong\\desktop\\output.txt", "w", stdout); -  thescanf"%d%d%d",&n,&m,&k); - Wuyi      for(inti =1; i<=n;i++) the     { -          for(intj =1; J <=m; J + +) Wu         { -scanf"%d",&map[i][j]); About         } $}//Initial underground Palace -memset (vis,-1,sizeof(Vis)); -Dfs1,1,0,-1); -printf"%d", vis[1][1][0][0]); A  +     return 0; the}

Underground Palace Treasure Blue Bridge Cup memory search DFS

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.