1. Get out of the maze
Brush question link
https://ac.nowcoder.com/acm/problem/14572
Note 1: this problem can be done by example, but it exceeds the time limit
The reason is that he didn't avoid looking back again and again, which increased the complexity
The solution is to prevent turning back by setting the searched position as dirty ('#')
Reduced complexity
Note 2: when there are multiple groups of inputs, the global variable array must be cleared
use
#include<string.h> memset(data, 0, sizeof(data)); flag=0;
To clean up the data
#include<iostream> #include<vector> #include<algorithm> #include<string.h> using namespace std; int N,M,a,b; char data[510][510]; int flag=0; void dfs(int x,int y){ if(x<1 || x>N || y<1 || y>M || data[x][y]=='#' ){ return; } if(data[x][y]=='E'){ flag=1; return ; } data[x][y]='#'; dfs(x+1,y); dfs(x,y+1); dfs(x-1,y); dfs(x,y-1); return; } int main(){ while(cin>>N>>M){ for(int i=1;i<=N;i++){ for(int j=1;j<=M;j++){ cin>>data[i][j]; if(data[i][j]=='S'){ a=i; b=j; } } } dfs(a,b); if(flag==1){ cout<<"Yes"<<endl; } else{ cout<<"No"<<endl; } memset(data, 0, sizeof(data)); flag=0; } return 0; }
This is a reference for setting back
https://ac.nowcoder.com/acm/contest/view-submission?submissionId=43704683
2. Beautiful sequence
Brush question link
https://ac.nowcoder.com/acm/problem/21313
In order to meet his three conditions, 1-40 is less than the average of the previous number without three decrements. The first condition can be solved when selecting the number. The second is the value range of the previous sum. The third is when judging the previous sum and the current size. What is the second one at this time
It seems to be general. In fact, it is accumulated from small to large
Those conditions that do not meet the conditions are not added and directly discarded
Code incomplete AC 50%
#include<iostream> #include<vector> using namespace std; #define Mod 1000000007 int n; long long data[41]; long long dp[41][41][3][1650]; int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>data[i]; } if(data[1]==-1){ for(int i=0;i<=40;i++){ dp[1][i][1][1]=1; } } else{ dp[1][data[1]][1][data[1]]=1; } for(int i=2;i<=n;i++){ if(data[i]==-1){ for(int j=0;j<=40;j++){ for(int L=0;L<=40;L++){ for(int k=j*(i-1);k<=1600-j;k++){ if(j>=L){ dp[i][j][1][k+j]=(dp[i][j][1][k+j]+dp[i-1][L][1][k])%Mod; dp[i][j][1][k+j]=(dp[i][j][1][k+j]+dp[i-1][L][2][k])%Mod; } else{ dp[i][j][2][k+j]=(dp[i][j][2][k+j]+dp[i-1][L][1][k])%Mod; } } } } } else{ for(int L=0;L<=40;L++){ for(int k=data[i]*(i-1);k<=1600-data[i];k++){ if(data[i]>=L){ dp[i][data[i]][1][k+data[i]]=(dp[i][data[i]][1][k+data[i]]+dp[i-1][L][1][k])%Mod; dp[i][data[i]][1][k+data[i]]=(dp[i][data[i]][1][k+data[i]]+dp[i-1][L][2][k])%Mod; } else{ dp[i][data[i]][2][k+data[i]]=(dp[i][data[i]][2][k+data[i]]+dp[i-1][L][1][k])%Mod; } } } } } long long sum=0; for(int j=0;j<=40;j++){ for(int k=j*n;k<=1600;k++){ sum=(sum+dp[n][j][1][k])%Mod; sum=(sum+dp[n][j][2][k])%Mod; } } cout<<sum<<endl; return 0; }
reference
https://www.cnblogs.com/five-great/p/12796650.html
3. Pass a note
Brush question link
https://ac.nowcoder.com/acm/contest/1041/E
The most important thing of dynamic programming is to see how many parameters this dp variable has and what its value represents
This question can be converted into two people from top left to bottom right at the same time, but the paths cannot overlap
And they must satisfy the formula (the number of steps to be taken must be the same synchronization), so i+j=k+l must satisfy
Just make sure they don't coincide before the end
Then simplify the calculation and avoid the same calculation
dp[i][j][k][l]
#include<iostream> #include<vector> #include<algorithm> using namespace std; int data[51][51]; int dp[51][51][51][51]; int main(){ int m,n; cin>>m>>n; for(int i=1;i<=m;i++){ for(int j=1;j<=n;j++){ cin>>data[i][j]; } } for(int i=1;i<=m;i++){ for(int j=1;j<=n;j++){ for(int k=1;k<=m;k++){ for(int l=1;l<=n;l++){ if((i<m || j<n)&& i==k && j==l)continue; int sum=0; sum=max(sum,dp[i-1][j][k-1][l]); sum=max(sum,dp[i][j-1][k][l-1]); if(1){ sum=max(sum,dp[i-1][j][k][l-1]); } if(1){ sum=max(sum,dp[i][j-1][k-1][l]); } dp[i][j][k][l]=sum+data[i][j]+data[k][l]; } } } } cout<<dp[m][n][m][n]<<endl; return 0; }
AC
reference
https://www.cnblogs.com/agenthtb/p/5745964.html