fork download
  1. #include <bits/stdc++.h>
  2. #define xout cout<<" "
  3. #define xln cout<<"\n"
  4. #define ll long long
  5. #define pb push_back
  6. using namespace std;
  7. const int N=1010;
  8. ll n,m,p,x,y,a[N][N];
  9. ll dp[N][N][20][3];
  10. bool vis[N][N][20][3];
  11. ll dfs(int i,int j,int p,int t){
  12. if(p < 0)return -1 ;
  13. if(i>n||i<1||j>m||j<1)return 0;
  14. if(a[i][j]==-1)return -1;
  15.  
  16. if(vis[i][j][p][t])return dp[i][j][p][t];
  17. vis[i][j][p][t]=1;
  18.  
  19. ll &res=dp[i][j][p][t],g=-1;
  20. res=a[i][j];
  21. if(t==1){
  22. g=max(dfs(i,j+1,p,t),g);
  23. }
  24. if(t==2){
  25. g=max(dfs(i,j-1,p,t),g);
  26. }
  27. if(t==0){
  28. g=max(g,dfs(i,j+1,p-1,1));
  29. g=max(g,dfs(i,j-1,p-1,2));
  30. }
  31. g=max(g,dfs(i+1,j,p,0));
  32. if(g==-1)res=-1;
  33. else res=a[i][j]+g;
  34.  
  35. return res;
  36. }
  37. int main(){
  38. cin>>n>>m;
  39. cin>>p;
  40. cin>>x>>y;
  41. for(int i=1;i<=n;i++){
  42. for(int j=1;j<=m;j++)cin>>a[i][j];
  43. }
  44. ll res=dfs(x,y,p,0);
  45. cout<<res;xln;
  46. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
0