当前位置:网站首页>2022-2-14 acwing1027 grid access
2022-2-14 acwing1027 grid access
2022-07-03 21:04:00 【kkzzjx】
1027. Take the number of squares
Someone from the top left corner of the picture A set out , You can walk down , You can also walk to the right , Until we get to the bottom right B spot .
On the way , He can take the number of squares ( The removed squares will be changed to numbers 0).
This person is from A Point to B I walked twice at 8 o'clock , Try to find two such paths , Make the sum of the obtained numbers the largest .
Compared with ordinary walk grid , Find the maximum path , The difference is : Take two paths . therefore DP The dimension of increases , use 4 Dimension represents a current state .
And because the steps of the two paths are the same , So we can use the equivalence relationship to reduce the dimension :k=i1+j1=i2+j2
The starting point :2 A little bit
End :2 A little bit
There will be duplication of two paths , Judge the repetition , Then add the corresponding weight .
#include<bits/stdc++.h>
using namespace std;
const int N=15;
int n;
int grid[N][N]={
0};
int dp[2*N][N][N]={
0};
int main(){
cin>>n;
int a,b,c;
while(1){
cin>>a>>b>>c;
if(a==0&&b==0&&c==0) break;
grid[a][b]=c;
}
for(int k=2;k<=n+n;k++){
for(int i1=1;i1<=n;i1++){
for(int i2=1;i2<=n;i2++){
int j1=k-i1,j2=k-i2;
if(j1>=1&&j1<=n&&j2>=1&&j2<=n){
int value=grid[i1][j1];
if(i1!=i2) value+=grid[i2][j2];
int &x=dp[k][i1][i2];
x=max(dp[k-1][i1-1][i2-1]+value,x);
x=max(dp[k-1][i1-1][i2]+value,x);
x=max(dp[k-1][i1][i2-1]+value,x);
x=max(dp[k-1][i1][i2]+value,x);
}
}
}
}
printf("%d",dp[2*n][n][n]);
}
275. Pass slip
Xiaoyuan and Xiaoxuan are good friends and classmates , They have a lot to talk about together .
In a quality development activity , The class arranged to sit in a m That's ok n Columns of the matrix , And Xiaoyuan and Xiaoxuan are arranged at both ends of the diagonal of the matrix , therefore , They can't talk directly .
Fortunately, , They can communicate by passing notes .
The note has to be passed to each other through many students , Xiaoyuan sits in the upper left corner of the matrix , coordinate (1,1), Xiaoxuan sits at the bottom right corner of the matrix , coordinate (m,n).
The note from Xiaoyuan to Xiaoxuan can only be passed down or to the right , The note from Xiaoxuan to Xiaoyuan can only be passed up or left .
In progress , Xiaoyuan hopes to deliver a note to Xiaoxuan , At the same time, I hope Xiaoxuan will reply to him .
Everyone in the class can pass it on for them , But only once , That is to say, if this person helps Xiaoxuan when Xiaoyuan hands him the note , Then when Xiaoxuan hands it to Xiaoyuan, he won't help again , vice versa .
One more thing to note , Everyone in the class is willing to help ( Be careful : There is no definition of the degree of kindness of Xiaoyuan and Xiaoxuan , Input time 0 Express ), You can use a 0∼100 It's a natural number , The greater the number, the better .
Xiaoyuan and Xiaoxuan hope to find students with high degree of kindness to help pass the notes , I.e. find two transfer paths back and forth , So that the sum of the two paths is the largest .
Now? , Please help Xiaoyuan and Xiaoxuan find such two paths .
Input format
The first line has 2 Integers separated by spaces m and n, Indicates that the student matrix has m That's ok n Column .
Next m Row is one. m×n Matrix , In matrix i That's ok j The integer representation of the column sits at i That's ok j The degree of kindness of the students , Per row n Integers separated by spaces .
Output format
Output an integer , Represents the maximum value of the sum of the kindness degree of the students participating in the transfer of the notes on the two routes back and forth .
Data range
1≤n,m≤50
AcWing 275. A code that proves why you can use a square to retrieve data in a note
The boss' explanation is very clear ~ If the two paths pass through repeated points , Then the weight of the repeated point for the second time must be 0, The weight obtained by detour is greater than or equal to 0, Therefore, it is enough to use the idea of grid counting to find out two paths and walk the number and the largest at the same time
Modify the previous question slightly according to the meaning of the question code that will do :
#include<bits/stdc++.h>
using namespace std;
const int N=55;
int main(){
int n,m;
cin>>n>>m;
int grid[N][N]={
0};
int dp[2*N][N][N]={
0};
int v;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>v;
grid[i][j]=v;
}
}
for(int k=2;k<=n+m;k++){
for(int i1=1;i1<=n;i1++){
for(int i2=1;i2<=n;i2++){
int j1=k-i1,j2=k-i2;
if(j1>=1&&j1<=m&&j2>=1&&j2<=m){
int value=grid[i1][j1];
if(i1!=i2) value+=grid[i2][j2];
int &x=dp[k][i1][i2];
x=max(dp[k-1][i1-1][i2-1]+value,x);
x=max(dp[k-1][i1-1][i2]+value,x);
x=max(dp[k-1][i1][i2-1]+value,x);
x=max(dp[k-1][i1][i2]+value,x);
}
}
}
}
printf("%d",dp[n+m][n][n]);
}
边栏推荐
- Basic number theory -- Chinese remainder theorem
- 大神们,我想发两个广播流1 从mysql加载基础数据,广播出去2 从kafka加载基础数据的变更
- Cesiumjs 2022 ^ source code interpretation [7] - Analysis of the request and loading process of 3dfiles
- Brief analysis of ref nerf
- Reinforcement learning - learning notes 1 | basic concepts
- Hcie security Day10: six experiments to understand VRRP and reliability
- Single page application architecture
- Discussion Net legacy application transformation
- Apprentissage intensif - notes d'apprentissage 1 | concepts de base
- Preliminary practice of niuke.com (11)
猜你喜欢

Recommendation of books related to strong foundation program mathematics

Shortest path problem of graph theory (acwing template)

Goodbye 2021, how do programmers go to the top of the disdain chain?

Hcie security Day11: preliminarily learn the concepts of firewall dual machine hot standby and vgmp

如临现场的视觉感染力,NBA决赛直播还能这样看?

TLS environment construction and plaintext analysis

Design e-commerce seckill system

Node MySQL serialize cannot rollback transactions

鹏城杯 WEB_WP

Line segment tree blue book explanation + classic example acwing 1275 Maximum number
随机推荐
Capturing and sorting out external articles -- autoresponder, composer, statistics [III]
抓包整理外篇——————autoResponder、composer 、statistics [ 三]
@Scenario of transactional annotation invalidation
Discussion Net legacy application transformation
Do you really know how old you are?
抓包整理外篇——————autoResponder、composer 、statistics [ 三]
MySQL——规范数据库设计
Monkey/ auto traverse test, integrate screen recording requirements
Inventory 2021 | yunyuansheng embracing the road
17 websites for practicing automated testing. I'm sure you'll like them
强化学习-学习笔记1 | 基础概念
How to choose cache read / write strategies in different business scenarios?
Transformer structure analysis and the principle of blocks in it
JS three families
Line segment tree blue book explanation + classic example acwing 1275 Maximum number
[Yugong series] February 2022 Net architecture class 004 ABP vNext used in WPF project
浅议.NET遗留应用改造
Viewing Chinese science and technology from the Winter Olympics (II): when snowmaking breakthrough is in progress
【愚公系列】2022年7月 Go教学课程 002-Go语言环境安装
Talk about daily newspaper design - how to write a daily newspaper and what is the use of a daily newspaper?