当前位置:网站首页>Niuke: Dragon and dungeon games
Niuke: Dragon and dungeon games
2022-07-02 22:46:00 【lsgoose】


This question is an upgraded version of the maze .
We walk backwards from the end to the starting point , set up dp[i][j] For from (i,j) The minimum amount of blood to reach the end .mp Represents the value in the maze .
We need to know dp[i][j] And the one on the lower right mp,dp The relationship between .
Ideas as follows :
We hope that one point can have as little blood as possible , That is, the minimum amount of blood to reach the destination , According to the title, the minimum blood volume is 1. Then on dp[i][j] introduce dp[i+1][j]-mp[i][j] and dp[i][j+1]-mp[i][j] Two quantities .
To sum up, these two expressions calculate the minimum value of the current node's blood volume to complete the next path , If it is negative, it means that you can complete the following path as long as the current HP is positive , We can take 1, If it is positive, it means that we need at least how much blood to ensure that we can finish the next path after walking the current point .
If the current matrix mp[i][j] It's negative , It means that you are going to lose blood , So obviously, when we came from the last point, we asked for more blood , If it comes from the left , use dp[i][j+1]-mp[i][j] You get at least the amount of blood you need to start from the point on the left ( The left side is the current point ), This blood volume should be the same as 1 Taking the maximum , Because at least 1, Less than 1 It only shows our mp[i][j] Large enough , It may be a bloody , And if the calculated value is greater than 1 General description mp[i][j] Small enough , So we need to take a larger value to ensure that after the current point , There is still blood volume to complete the next path .
If it's going down, the idea is similar .
The following is the specific operation process :
We start from the end , First initialize the endpoint dp[n-1][m-1]=max(1, 1-mp[n-1][m-1])
Then update each point dp[i][j]=max(1, min(dp[i+1][j], dp[i][j+1])-mp[i][j]), obviously , For the first column , The first line needs special treatment . there min Because we always require the minimum amount of blood ,max Because our blood volume is at least 1
The code is as follows :
#include<bits/stdc++.h>
using namespace std;
// recursive
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];
}
// Recurrence
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];
}
// Space optimization
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;
}This is the inspiration brought to me by a problem solving of Niuke network ~
边栏推荐
- Market Research - current situation and future development trend of anti-counterfeiting label market
- Task and privilege level protection
- UE4 游戏架构 学习笔记
- Market Research - current market situation and future development trend of night vision goggles for pilots
- U++ 学习笔记 ----松弛
- 《乔布斯传》英文原著重点词汇笔记(十)【 chapter eight】
- Oracle-PL/SQL编程
- 杰理之内置短按再长按,不管长按多长时间都是短按【篇】
- 腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
- Oracle PL / SQL programming
猜你喜欢

C language, to achieve three chess games

牛客网:龙与地下城游戏

Source code analysis - lightweight asynchronous crawler framework Ruia

#include errors detected. Please update your includePath.

Simpleitk use - 3 Common operations

数学建模——图与网络模型及方法(一)

【ODX Studio编辑PDX】-0.1-如何快速查看各Variant变体间的支持的诊断信息差异(服务,Sub-Function...)
![[001] [arm-cortex-m3/4] internal register](/img/49/a0eceac1a67267216dd9b2566033a1.jpg)
[001] [arm-cortex-m3/4] internal register

Perceptron model and Application

"Actbert" Baidu & Sydney University of technology proposed actbert to learn the global and local video text representation, which is effective in five video text tasks!
随机推荐
Share how to make professional hand drawn electronic maps
影视随摘
[shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)
Unity publishes a method of webgl playing sound
[QT] QT multithreading development - reentrancy and thread safety
wait解决僵尸进程
New feature of go1.18: introduce new netip Network Library
基于ASP.net的手机销售管理系统(二手手机销售管理系统)+ASP.NET+C#语言+VS2010+数据库可以用于课设、毕设学习
Oracle cursor
Notes on key vocabulary in the English original of the biography of jobs (11) [chapter nine]
图形视图框架
NC24325 [USACO 2012 Mar S]Flowerpot
佩服,竟然有人把高等数学这么晦涩难懂的科目,讲解得如此通俗易懂
Task and privilege level protection
Unity3d learning notes 4 - create mesh advanced interface
phpcms实现订单直接支付宝支付功能
杰理之修改不需要长按开机功能【篇】
Micro service gateway selection, please accept my knees!
Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation
JS solution for obtaining the width and height of hidden elements whose display is none