当前位置:网站首页>地宮取寶(記憶化深搜)
地宮取寶(記憶化深搜)
2022-07-01 06:11: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);
}
边栏推荐
- Small guide for rapid completion of mechanical arm (VI): stepping motor driver
- CJC8988带2个立体声耳机驱动器的低功率立体声编解码器
- 3D printer threading: five simple solutions
- FPGA - 7系列 FPGA内部结构之Clocking -01- 时钟架构概述
- 68 Cesium代码datasource加载czml
- Oracle sequence + trigger
- Database problems, how to optimize Oracle SQL query statements faster and more efficient
- Highmap gejson data format conversion script
- excel动态图表
- kubeadm搭建kubenetes 集群(个人学习版)
猜你喜欢

How to add a gourd pie plate

TiDB单机模拟部署生产环境集群(闭坑实践,亲测有效)

Multi label lsml for essay learning records

机械臂速成小指南(六):步进电机驱动器

OpenGL es: (3) EGL, basic steps of EGL drawing, eglsurface, anativewindow

Chip, an empire built on sand!

Index method and random forest to realize the information of surface water body in wet season in Shandong Province

PLA not pasted on the bed: 6 simple solutions

68 cesium code datasource loading czml

Thesis learning record essay multi label lift
随机推荐
Freeswitch dial the extension number
Don't put your notes and videos everywhere!
Preliminary level of C language -- selected good questions on niuke.com
Why use huluer pie disk instead of U disk?
JDBC database operation
SystemVerilog学习-07-类的继承和包的使用
【文件系统】如何在ubi之上运行squashfs
ArcServer密码重置(账号不可以重置)
Understanding of C manualresetevent class
skywalking集成nacos动态配置
MinIO纠错码、分布式MinIO集群搭建及启动
浏览器端保存数据到本地文件
Pychart configuring jupyter
π disk, turning your computer into a personal private cloud
FPGA - 7系列 FPGA内部结构之Clocking -01- 时钟架构概述
Index method and random forest to realize the information of surface water body in wet season in Shandong Province
ONEFLOW source code parsing: automatic inference of operator signature
扩散(多源广搜)
DHT11 temperature and humidity sensor
TIDB数据库特性总结