今天蓝桥杯考试 ,我什么都不会,感觉以前做的算法太少了所以今天出写一下算法相关的东西。
标题:地宫取宝
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
资源约定:
峰值内存消耗 < 256M
CPU消耗 < 1000ms
package op;
import java.util.Scanner;
public class tt {
final static int MOD = 1000000007;
private static int n;
private static int m;
private static int k;
static int data[][]; // 用来存储物体的价值
static int cache[][][][] = new int [51][51][14][14];
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n =scanner.nextInt();
m =scanner.nextInt();
k =scanner.nextInt();
data = new int [n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j <m; j++) {
data[i][j] = scanner.nextInt();
}
}
for (int i = 0; i < 51; i++) {
for (int j = 0; j < 51; j++) {
for (int j2 = 0; j2 < 14; j2++) {
for (int k = 0; k < 14; k++) {
cache[i][j][j2][k] =-1;
}
}
}
}
int a =dfs(0, 0, -1, 0);// 由于从起点出发 可以取0 所以要设置为-1
System.out.println(a);
}
//x:行 y:列 max:物体的最大价值 cnt:行动方案
public static int dfs(int x,int y, int max,int cnt)
{
if(cache[x][y][max+1][cnt] !=-1) {return cache[x][y][max+1][cnt];}
if(x==n||y==m||cnt>k)return 0;
int cur =data[x][y];
int ans =0;
if(x==n-1 && y == m-1)
{
if(cnt==k ||(cnt==k-1 && cur>max))return 1;
return ans;
}
//当前价值超过 记录的最大价值,可以选择 向下或者向右取物体
if(cur>max)
{
ans+=dfs(x+1, y, cur, cnt+1);
ans+=dfs(x, y+1, cur, cnt+1);
}
否则 就是只能向下 或者向右边探索而不能取物体
ans += dfs(x+1,y,max,cnt);
ans += dfs(x,y+1,max,cnt);
cache[x][y][max+1][cnt] = ans%MOD;
return ans;
}
}
public class 跳台阶 {
public static void main(String[] args) {
int [] nums = {3,2,1,4,1};
System.out.println(jump(nums));
}
public static int jump(int[] nums) {
int len =nums.length;
int maxPosition =0;
int count =0;
int end =0;
for (int i = 0; i < len-1; i++) {
maxPosition = Math.max(maxPosition,nums[i]+i);
if(i == end)
{
end = maxPosition;
count++;
}
}
return count;
}
}