当前位置:网站首页>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]);
}
边栏推荐
- 9 pyqt5 qscrollarea scroll area and qscrollbar scroll bar
- 19、 MySQL -- SQL statements and queries
- UI automation test: selenium+po mode +pytest+allure integration
- jvm jni 及 pvm pybind11 大批量数据传输及优化
- Interval product of zhinai sauce (prefix product + inverse element)
- The 12th Blue Bridge Cup
- Basic preprocessing and data enhancement of image data
- From the behind the scenes arena of the ice and snow event, see how digital builders can ensure large-scale events
- String and+
- How to handle wechat circle of friends marketing activities and share production and release skills
猜你喜欢
Xai+ network security? Brandon University and others' latest "interpretable artificial intelligence in network security applications" overview, 33 page PDF describes its current situation, challenges,
Custom view incomplete to be continued
你真的知道自己多大了吗?
thrift go
In 2021, the global foam protection packaging revenue was about $5286.7 million, and it is expected to reach $6615 million in 2028
强化学习-学习笔记1 | 基础概念
不同业务场景该如何选择缓存的读写策略?
MySQL——索引
Sightseeing - statistics of the number of shortest paths + state transfer + secondary small paths
浅议.NET遗留应用改造
随机推荐
Deep search DFS + wide search BFS + traversal of trees and graphs + topological sequence (template article acwing)
Refer to some books for the distinction between blocking, non blocking and synchronous asynchronous
Capturing and sorting out external articles -- autoresponder, composer, statistics [III]
Summary of common operation and maintenance commands
jvm jni 及 pvm pybind11 大批量数据传输及优化
Kubernetes abnormal communication network fault solution ideas
Wireless network (preprocessing + concurrent search)
2022 melting welding and thermal cutting examination materials and free melting welding and thermal cutting examination questions
Apprentissage intensif - notes d'apprentissage 1 | concepts de base
Qualcomm platform WiFi update disconnect end open event
鹏城杯 WEB_WP
For in, foreach, for of
全网都在疯传的《老板管理手册》(转)
Is flush account opening and registration safe and reliable? Is there any risk?
Xai+ network security? Brandon University and others' latest "interpretable artificial intelligence in network security applications" overview, 33 page PDF describes its current situation, challenges,
thrift go
(5) Web security | penetration testing | network security operating system database third-party security, with basic use of nmap and masscan
SQL injection - Fundamentals of SQL database operation
上周内容回顾
LabVIEW training