当前位置:网站首页>牛客网:龙与地下城游戏
牛客网:龙与地下城游戏
2022-07-02 22:06:00 【lsgoose】


这题是走迷宫的升级版本吧。
我们反着从终点走到起点,设dp[i][j]代表从(i,j)走到终点的最少血量。mp表示迷宫里面的值。
我们要知道dp[i][j]和右边下边的mp,dp的关系。
思路如下:
我们希望一个点能血量少一点就尽量少一点,即也是到达终点的最小血量,由题目可知最小血量是1。接着对dp[i][j]引入dp[i+1][j]-mp[i][j]和dp[i][j+1]-mp[i][j]两个量。
概括说一下这两个表达式求的都是要完成接下来的路径的当前节点血量的最小值,如果为负说明只要当前血量是个正值都可以走完下面的路径,我们可以直接取1,如果为正说明我们还需要至少有多少的血量才能保证在走完当前点后还能走完接下来的路径。
如果当前的矩阵mp[i][j]是负的,说明是要掉血的,那么显然我们从上一个点来的时候就要求血要多一些,如果是从左边来的,用dp[i][j+1]-mp[i][j]就得到了从左边那个点出发至少需要的血量(这个左边是当前点),这个血量要和1取最大值,因为至少是1,而小于1只能说明我们的mp[i][j]足够大,可能是个加血的,而求出来的值如果比1大则说明mp[i][j]足够小,所以我们要取更大的值以保证过了当前点后,仍然有血量走完接下来的路径。
如果是向下走思路类似。
以下是具体操作流程:
我们从终点开始走,首先初始化终点dp[n-1][m-1]=max(1, 1-mp[n-1][m-1])
之后对每个点更新dp[i][j]=max(1, min(dp[i+1][j], dp[i][j+1])-mp[i][j]),显然,对于第一列,第一行需要特殊处理。这里的min是因为我们始终要求最小的血量,max是因为我们的血量至少要是1
代码如下所示:
#include<bits/stdc++.h>
using namespace std;
// 递归
int solve1(int i, int j, int n, int m, vector<vector<int>> &mp, vector<vector<int>> &dp){
if(dp[i][j]!=-1){
return dp[i][j];
}
if(i==n-1 && j==m-1){
dp[i][j]=max(1,1-mp[i][j]);
}else if(i==n-1){
dp[i][j]=max(1, solve1(i, j+1, n, m, mp, dp)-mp[i][j]);
}else if(j==m-1){
dp[i][j]=max(1, solve1(i+1, j, n, m, mp, dp)-mp[i][j]);
}else{
dp[i][j]=max(1, min(solve1(i, j+1, n, m, mp, dp), solve1(i+1, j, n, m, mp, dp))-mp[i][j]);
}
return dp[i][j];
}
//递推
int solve2(int n,int m,vector<vector<int>> &mp, vector<vector<int>> &dp){
dp[n-1][m-1]=max(1, 1-mp[n-1][m-1]);
for(int i=n-2;i>=0;--i){
dp[i][m-1]=max(1, dp[i+1][m-1]-mp[i][m-1]);
}
for(int j=m-2;j>=0;--j){
dp[n-1][j]=max(1, dp[n-1][j+1]-mp[n-1][j]);
}
for(int i=n-2;i>=0;--i){
for(int j=m-2;j>=0;--j){
dp[i][j]=max(1, min(dp[i+1][j], dp[i][j+1])-mp[i][j]);
}
}
return dp[0][0];
}
//空间优化
int solve3(int n, int m, vector<vector<int>> &mp, vector<int> &dp){
dp[m-1]=max(1, 1-mp[n-1][m-1]);
for(int i=m-2;i>=0;--i){
dp[i]=max(1, dp[i+1]-mp[n-1][i]);
}
for(int i=n-2;i>=0;--i){
dp[m-1]=max(1, dp[m-1]-mp[i][m-1]);
for(int j=m-2;j>=0;--j){
dp[j]=max(1, min(dp[j+1],dp[j])-mp[i][j]);
}
}
return dp[0];
}
int main(){
int m,n;
cin>>n>>m;
vector<vector<int>> mp(n, vector<int>(m));
// vector<vector<int>> dp(n, vector<int>(m, -1));
vector<int> dp(m);
int res=INT_MAX;
for(int i=0;i<m;++i){
for(int j=0;j<n;++j){
cin>>mp[i][j];
}
}
// res=solve1(0,0,n,m,mp,dp);
// res=solve2(n, m, mp, dp);
res=solve3(n, m, mp, dp);
cout<<res<<endl;
return 0;
}这是牛客网的一个题解带给我的启发~
边栏推荐
- Lightgbm principle and its application in astronomical data
- [shutter] shutter application theme (themedata | dynamic modification theme)
- 开发者分享 | HLS, 巧用AXI_master总线接口指令的定制并提升数据带宽-面积换速度...
- Notes on key vocabulary in the English original of the biography of jobs (11) [chapter nine]
- 《乔布斯传》英文原著重点词汇笔记(九)【 chapter seven】
- 【AUTOSAR-DCM】-4.3-UDS $22和$2E服务如何读取和写入NVM数据
- Servicemesh mainly solves three pain points
- 【C 题集】of Ⅴ
- Promise optimized callback hell
- Market Research - current market situation and future development trend of aircraft wireless intercom system
猜你喜欢

From "bronze" to "King", there are three secrets of enterprise digitalization

Infrastructure is code: a change is coming

Source code analysis - lightweight asynchronous crawler framework Ruia

Simpleitk use - 4 Strange question

Promise optimized callback hell

sql service 截取字符串

Socket socket c/s end process

Based on asp Net (used mobile phone sales management system) +asp Net+c # language +vs2010+ database can be used for course design and post design learning

基于ASP.net的手机销售管理系统(二手手机销售管理系统)+ASP.NET+C#语言+VS2010+数据库可以用于课设、毕设学习

Kubernetes resource object introduction and common commands (4)
随机推荐
Leetcode circular linked list (fast and slow pointer) code line by line interpretation
Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
开发者分享 | HLS, 巧用AXI_master总线接口指令的定制并提升数据带宽-面积换速度...
Sql service intercepts string
Introduction to database system Chapter 1 short answer questions - how was the final exam?
U++ learning notes - relaxation
"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
Attack and defense world PWN question: Echo
Regular expression (2)
数学建模——图与网络模型及方法(一)
Official announcement! The golden decade of new programmers and developers was officially released
20220702 how do programmers build knowledge systems?
UE4 UI自适应屏幕
Unity publishes a method of webgl playing sound
Bridge emqx cloud data to AWS IOT through the public network
Simpleitk use - 3 Common operations
Notes on key vocabulary of the original English book biography of jobs (IX) [chapter seven]
Simpleitk use - 4 Strange question
Promise optimized callback hell
540. Single element in ordered array