当前位置:网站首页>地宫取宝(记忆化深搜)
地宫取宝(记忆化深搜)
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);
}
边栏推荐
- How does MySQL store Emoji?
- Arcserver password reset (account cannot be reset)
- Small guide for rapid completion of mechanical arm (VI): stepping motor driver
- In win10 and win11, the scroll direction of Elan touch panel is reversed, and "double finger click to open the right-click menu" and "double finger scroll" are started“
- make: g++:命令未找到
- 指数法和Random Forest实现山东省丰水期地表水体信息
- Chip, an empire built on sand!
- c# Xml帮助类
- 让厦门灌口镇田头村变“甜头”村的特色农产品之一是
- Huluer app help
猜你喜欢

云盘里资料被和谐了,怎么办?

Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills

Cjc8988 Low Power Stereo codec with 2 stereo headphone drivers
![[note] e-commerce order data analysis practice](/img/03/367756437be947b5b995d5f7f55236.png)
[note] e-commerce order data analysis practice

数据库问题,如何优化Oracle SQL查询语句更快,效率更高

Infinite horizontal marble game

ArcServer密码重置(账号不可以重置)

让田头村变甜头村的特色农产品是仙景芋还是白菜

Know the future of "edge computing" from the Nobel prize!

SystemVerilog学习-08-随机约束和线程控制
随机推荐
UOW of dev XPO comparison
OpenGL es: (5) basic concepts of OpenGL, the process of OpenGL es generating pictures on the screen, and OpenGL pipeline
Oracle create user + Role
MySQL里记录货币
One of the characteristic agricultural products that make Tiantou village, Guankou Town, Xiamen into a "sweet" village is
Don't put your notes and videos everywhere!
指数法和Random Forest实现山东省丰水期地表水体信息
Index method and random forest to realize the information of surface water body in wet season in Shandong Province
El tooltip in the table realizes line breaking display
SQL必会题之留存率
SystemVerilog学习-08-随机约束和线程控制
freeswitch拨打分机号
SystemVerilog学习-07-类的继承和包的使用
SystemVerilog学习-09-进程间同步、通信和虚方法
Using Baidu map to query national subway lines
srpingboot security demo
MySQL怎么存储emoji?
He struggled day and night to protect his data
OpenGL ES: (4) EGL API详解 (转)
【文件系统】如何在ubi之上运行squashfs