地宫取宝
- X 国王有一个地宫宝库。是 n x m 个格子的矩阵。每个格子放一件宝贝。每个宝贝贴着价值标签。
地宫的入口在左上角,出口在右下角。
小明被带到地宫的入口,国王要求他只能向右或向下行走。
走过某个格子时,如果那个格子中的宝贝价值比小明手中任意宝贝价值都大,小明就可以拿起它(当然,也可以不拿)。
当小明走到出口时,如果他手中的宝贝恰好是k件,则这些宝贝就可以送给小明。
请你帮小明算一算,在给定的局面下,他有多少种不同的行动方案能获得这k件宝贝。
- 输入一行3个整数,用空格分开:n m k (1<=n,m<=50, 1<=k<=12)
接下来有 n 行数据,每行有 m 个整数 Ci (0<=Ci<=12)代表这个格子上的宝物的价值
要求输出一个整数,表示正好取k个宝贝的行动方案数。该数字可能很大,输出它对 1000000007 取模的结果。
-
例如,输入:
2 2 2
1 2
2 1
程序应该输出:
2
再例如,输入:
2 3 2
1 2 3
2 1 5
程序应该输出:
14
解法一,开始时, 我没有初始化记忆数组, 数组的每个格子都是0, 最后没有节省时间, 因为很多格子上记录的就是0, 只有0种取法.
没有留意题目种对结果取余的告诫, 只在最后return res之前取余数. 最终有3个测试用例过不了, 因为每次递归的结果都有可能超过1000000007, 最后才进行取余在样本量极大的情况下是会出错的.以下代码提交后只能得57分。
import java.util.Scanner;
public class Main {
static int k;
static int[][] arr;
static int n;
static int m;
static int MOD=1000000007;
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
n=in.nextInt();
m=in.nextInt();
k=in.nextInt(); //2、可以取宝总数
arr=new int[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
arr[i][j]=in.nextInt();
}
}
for(int i=0;i<51;i++) {
for(int j=0;j<51;j++) {
for(int l=0;l<14;l++) {
for(int o=0;o<14;o++) {
cat[i][j][l][o]=-1;
}
}
}
}
int res=dfs(0,0,-1,0);
System.out.println(res);
}
static int[][][][] cat=new int[51][51][14][14]; //1、带有记忆的递归,有几个参数,建立几维数组,没有这一步只能的60分
//1、max是取宝总数中最大价值,count是取宝总数
public static int dfs(int x,int y,int max,int count) {
if(x==n||y==m||count>k) {
return 0;
}
if(cat[x][y][max+1][count]!=-1) {
return cat[x][y][max+1][count];
}
int ans=0;
int cur=arr[x][y]; //cur是当前宝物的价值
//1、到最后一个位置了,说明最多有一种取法,刚好差一个宝物,并且最后一个还符合条件,则是一种
if(x==n-1&&y==m-1) {
if(count==k||(count==k-1&&cur>max)){
return 1;
}
//1、没有满足要求,方案不计数,好比那些贪心的人,总想获得价值最高,结果什么也得不到
return ans;
}
if(cur>max) {
ans+=dfs(x+1,y,cur,count+1); //1、向下走
ans+=dfs(x,y+1,cur,count+1);
}
//2、当前价值小,不取直接走
ans+=dfs(x+1,y,max,count);
ans+=dfs(x,y+1,max,count);
cat[x][y][max+1][count]=ans%MOD;
return ans;
}
}
- 以下改进版可得100分。
import java.util.Scanner;
public class Main {
public static int n;
public static int m;
public static int k;
public static int[][][][] dp = new int[50][50][15][15];//用于记忆搜索
public static int[][] arr = new int[50][50];
public static void main(String[] args) {
//处理输入
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
k = sc.nextInt();
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
arr[i][j] = sc.nextInt();
}
}
initDp();//初始化记忆数组,不能用java初始的0值,因为很多格子只有0种可能, 会造成重复搜索.
System.out.println(process(arr, 0, 0, -1, 0));//宝藏的最小值为0, 所以初始的max值为-1
}
public static int process(int[][] arr, int x, int y, int max, int cur){
if(dp[x][y][max + 1][cur] != -1){//max初始为-1,为防止越界填入max+1
return dp[x][y][max + 1][cur];
}
if(x >= n || y >= m || cur > k){
return 0;
}
int value = arr[x][y];
if(x == n - 1 && y == m - 1){
if(cur == k || (value > max && cur == k - 1)){
return 1;
}
return 0;
}
int res = 0;
if(value > max){//如果宝藏大于当前手上的最大值, 就可以拿走.
res += process(arr, x, y + 1, value, cur + 1);//每次递归完对结果取模, 否则数据量大的时候会出错.
res %= 1000000007;
res += process(arr, x + 1, y, value, cur + 1);
res %= 1000000007;
}
//不拿走
res += process(arr, x, y + 1, max, cur);
res %= 1000000007;
res += process(arr, x + 1, y, max, cur);
res %= 1000000007;
dp[x][y][max + 1][cur] = res;
return res;
}
public static void initDp(){
for(int i = 0; i < 50; i++){
for(int j = 0; j < 50; j++){
for(int k = 0; k < 15; k++){
for(int l = 0; l < 15; l++){
dp[i][j][k][l] = -1;
}
}
}
}
}
}