当前位置:网站首页>地宮取寶(記憶化深搜)
地宮取寶(記憶化深搜)
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);
}
边栏推荐
- TIDB数据库特性总结
- Flink practice -- multi stream merge
- How does MySQL store Emoji?
- MySQL中 in 和 exists 的区别
- OpenGL es: (4) detailed explanation of EGL API (Continued)
- 69 Cesium代码datasource加载geojson
- Why use huluer pie disk instead of U disk?
- HDU - 1501 Zipper(记忆化深搜)
- 68 Cesium代码datasource加载czml
- Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills
猜你喜欢

Talking from mlperf: how to lead the next wave of AI accelerator

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

FPGA - 7系列 FPGA内部结构之Clocking -02- 时钟布线资源

相同区域 多源栅格数据 各个像元行列号一致,即行数列数相同,像元大小相同

FPGA - 7系列 FPGA内部结构之Clocking -01- 时钟架构概述

My experience from technology to product manager

Why use huluer pie disk instead of U disk?

Multi label lsml for essay learning records

Arcserver password reset (account cannot be reset)

What if the data in the cloud disk is harmonious?
随机推荐
地宫取宝(记忆化深搜)
Database problems, how to optimize Oracle SQL query statements faster and more efficient
kubeadm搭建kubenetes 集群(个人学习版)
3D打印机穿线:5种简单的解决方案
excel初级应用案例——杜邦分析仪
ArcServer密码重置(账号不可以重置)
CJC8988带2个立体声耳机驱动器的低功率立体声编解码器
How to add a gourd pie plate
QT write custom control - self drawn battery
Transformer le village de tiantou en un village de betteraves sucrières
Record currency in MySQL
My experience from technology to product manager
Skywalking integrated Nacos dynamic configuration
What if the data in the cloud disk is harmonious?
Send you through the data cloud
浏览器端保存数据到本地文件
相同区域 多源栅格数据 各个像元行列号一致,即行数列数相同,像元大小相同
One of the characteristic agricultural products that make Tiantou village, Guankou Town, Xiamen into a "sweet" village is
Primary application case of Excel DuPont analyzer
SystemVerilog学习-09-进程间同步、通信和虚方法