当前位置:网站首页>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 ~
边栏推荐
- SimpleITK使用——3. 常见操作
- Task and privilege level protection
- UE4 游戏架构 学习笔记
- [QT] QT multithreading development - reentrancy and thread safety
- Socket套接字C/S端流程
- Dynamic memory allocation (malloc calloc realloc free)
- Share how to make professional hand drawn electronic maps
- [error record] the flutter reports an error (could not read script 'xxx\flutter\u tools\gradle\app\u plugin\u loader.gradle')
- 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
- 牛客网:龙与地下城游戏
猜你喜欢
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
NC24325 [USACO 2012 Mar S]Flowerpot
[shutter] shutter opens a third-party application (url|launcher plug-in search and installation | url| launcher plug-in official example | open browser | open a third-party application)
Get off work on time! Episode 6 of Excel Collection - how to split and count document amounts
Utilisation de simpletk - 4. Question étrange
Developers share | HLS and skillfully use Axi_ Customize the master bus interface instructions and improve the data bandwidth - area exchange speed
Build your own website (22)
Struct, bit segment, enumeration, union
[shutter] shutter application theme (themedata | dynamic modification theme)
Phpcms realizes the direct Alipay payment function of orders
随机推荐
服务器响应状态码
Market Research - current market situation and future development trend of aircraft wireless intercom system
Simpleitk use - 4 Strange question
Sql service intercepts string
New feature of go1.18: trylock, which has been tossed n times
ArrayList analysis 2: pits in ITR, listiterator, and sublist
PHP微信抢红包的算法
Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation
Try to get property'num for PHP database data reading_ rows' of non-object?
【外刊】睡眠与减肥
数学建模——图与网络模型及方法(一)
Notes on key vocabulary in the English original of the biography of jobs (11) [chapter nine]
Notes on key vocabulary in the English original of the biography of jobs (10) [chapter eight]
Market Research - current market situation and future development trend of night vision goggles for pilots
Les trois principaux points de douleur traités par servicemesh
"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!
Leetcode circular linked list (fast and slow pointer) code line by line interpretation
Market Research - current market situation and future development trend of marine wet exhaust hose
Promise optimized callback hell
[QT] QT multithreading development - reentrancy and thread safety