当前位置:网站首页>地宫取宝(记忆化深搜)
地宫取宝(记忆化深搜)
2022-07-01 06:07:00 【大 聪 明】
传送门
题目描述
X 国王有一个地宫宝库。是 n x m 个格子的矩阵。每个格子放一件宝贝。每个宝贝贴着价值标签。
地宫的入口在左上角,出口在右下角。
小明被带到地宫的入口,国王要求他只能向右或向下行走。
走过某个格子时,如果那个格子中的宝贝价值比小明手中任意宝贝价值都大,小明就可以拿起它(当然,也可以不拿)。
当小明走到出口时,如果他手中的宝贝恰好是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
分析:对 1000000007 取模说明数据量很大,使用long long类型和记忆化深搜;
1、所有宝贝价值加1:因为初始价值可能为0,maxn不能也同时为0 函数里比较的时候改为>=就不对了(要求捡到的宝贝比之前的都贵才可以)maxn为-1也不行 数组不能是负数,所以要同时加1 这样最小的初始值为1 互不影响
2、每次ans初始化为0,走到某一步了,后面的每一步都对应的几种情况,所以dp代表走到这一步了,可以对应多少种情况,这样逐渐累加,都return后第一步起点的ans就是总的情况
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int mod=1000000007;
ll a[55][55];
ll dp[55][55][15][15];
ll n,m,k;
ll dfs( ll x,ll y,ll maxn,ll t)
{
ll ans=0;
if(t>k||x>n||y>m) return 0;
if(dp[x][y][maxn][t]!=-1)
return dp[x][y][maxn][t];
if(x==n&&y==m)
{
if( t==k || (t==k-1&&a[x][y]>maxn) )
return 1;
return 0;
}
if(a[x][y]>maxn)//捡起宝贝
{
ans+=dfs(x+1,y,a[x][y],t+1);
ans+=dfs(x,y+1,a[x][y],t+1);
}
ans+=dfs(x+1,y,maxn,t);
ans+=dfs(x,y+1,maxn,t);
//遇到合适的宝贝有捡的情况,也有可以不捡起的情况,所以都要遍历一遍不捡的情况
ans%=mod;
dp[x][y][maxn][t]=ans;
return ans;
}
int main()
{
scanf("%lld%lld%lld",&n,&m,&k);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
scanf("%lld",&a[i][j]);
a[i][j]+=1;
}
memset(dp,-1,sizeof(dp));
printf("%lld\n",dfs(1,1,0,0)%mod);
}
边栏推荐
- C XML help class
- 表格中el-tooltip 实现换行展示
- OpenGL ES: (2) OpenGL ES 与 EGL、GLSL的关系
- Skywalking integrated Nacos dynamic configuration
- CJC8988带2个立体声耳机驱动器的低功率立体声编解码器
- 69 cesium code datasource loading geojson
- Make Tiantou village sweet. Is Xianjing taro or cabbage the characteristic agricultural product of Tiantou Village
- 2022 年面向初学者的 10 大免费 3D 建模软件
- DHT11 温湿度传感器
- make: g++:命令未找到
猜你喜欢

69 Cesium代码datasource加载geojson

Multi label lsml for essay learning records

Arcserver password reset (account cannot be reset)

Scope data export mat

Pla ne colle pas sur le lit: 6 solutions simples

PLA not pasted on the bed: 6 simple solutions

C language beginner level - realize the minesweeping game

Excel dynamic chart

The row and column numbers of each pixel of multi-source grid data in the same area are the same, that is, the number of rows and columns are the same, and the pixel size is the same

PLA不粘貼在床上:6個簡單的解决方案
随机推荐
Scope data export mat
PLA不粘貼在床上:6個簡單的解决方案
Save data in browser to local file
PLA not pasted on the bed: 6 simple solutions
Differences between in and exists in MySQL
Send you through the data cloud
DHT11 temperature and humidity sensor
highmap gejson数据格式转换脚本
el-table 动态表头渲染 固定第一列 高度问题
SQL必会题之留存率
three.js小结
Fragment upload and breakpoint resume
MinIO纠错码、分布式MinIO集群搭建及启动
Essay learning record essay multi label Global
Solve the problem of garbled files uploaded by Kirin v10
69 Cesium代码datasource加载geojson
Cjc8988 Low Power Stereo codec with 2 stereo headphone drivers
OpenGL es: (2) relationship between OpenGL es, EGL and glsl
利用百度地图查询全国地铁线路
论文学习记录随笔 多标签之LIFT