Advertisement
AlexAvram

Untitled

Apr 8th, 2025
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. //pion1 #4611
  2. #include <iostream>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. int n, dp[101][101];
  8. /*
  9. problema clasica de dinamica
  10. pentru toate elemente, verific toate modalitatile
  11. de a ajunge pe acea pozitie si o aleg pe cea de valoare
  12. maxima; afisez rezultat maxim;
  13. */
  14. void citire()
  15. {
  16. cin>>n;
  17. for (int i=1; i<=n; ++i)
  18. for (int j=1; j<=n; ++j)
  19. cin>>dp[i][j];
  20. }
  21. void rezolvare()
  22. {
  23. for (int i=2; i<=n; ++i)
  24. {
  25. for (int j=1; j<=n; ++j)
  26. {
  27. if (!dp[i][j])
  28. dp[i][j]+=dp[i-1][j];
  29. else
  30. dp[i][j]+=max(dp[i-1][j-1], dp[i-1][j+1]);
  31. }
  32. }
  33. int maxim=0;
  34. for (int i=1; i<=n; ++i)
  35. if (dp[n][i]>maxim)
  36. maxim=dp[n][i];
  37. cout<<maxim<<'\n';
  38. }
  39. /*reconstituire drum
  40. rezolva cerinta problemei+reconstituie drumul parcurs de pion
  41. int dp2[101][101];
  42. struct
  43. {
  44. int i_ant, j_ant;
  45. }poz[101][101];
  46. void copiere()
  47. {
  48. for (int i=1; i<=n; ++i)
  49. for (int j=1; j<=n; ++j)
  50. dp2[i][j]=dp[i][j];
  51. }
  52. pair<int, int> v[10001];
  53. void constituire_drum()
  54. {
  55. for (int i=2; i<=n; ++i)
  56. {
  57. for (int j=1; j<=n; ++j)
  58. {
  59. if (!dp2[i][j])
  60. {
  61. dp2[i][j]+=dp2[i-1][j];
  62. poz[i][j].i_ant=i-1;
  63. poz[i][j].j_ant=j;
  64. }
  65. else
  66. {
  67. if (dp2[i-1][j-1]>=dp2[i-1][j+1])
  68. {
  69. dp2[i][j]+=dp2[i-1][j-1];
  70. poz[i][j].i_ant=i-1;
  71. poz[i][j].j_ant=j-1;
  72. }
  73. else
  74. {
  75. dp2[i][j]+=dp2[i-1][j+1];
  76. poz[i][j].i_ant=i-1;
  77. poz[i][j].j_ant=j+1;
  78. }
  79. }
  80. }
  81. }
  82. int maxim=0, j_max;
  83. for (int i=1; i<=n; ++i)
  84. if (dp2[n][i]>maxim)
  85. maxim=dp2[n][i], j_max=i;
  86. cout<<maxim<<'\n';
  87. poz[1][1].i_ant=poz[1][1].j_ant=-1;
  88. int i=n, j=j_max, k=0;
  89. while (i!=-1)
  90. {
  91. v[++k].first=i;
  92. v[k].second=j;
  93. int a=i, b=i;
  94. i=poz[a][b].i_ant;
  95. j=poz[a][b].j_ant;
  96. }
  97. for (int i=k; i>=1; --i)
  98. cout<<v[i].first<<' '<<v[i].second<<'\n';
  99. }
  100. */
  101. int main()
  102. {
  103. citire();
  104. //copiere();
  105. //constituire_drum();
  106. rezolvare();
  107.  
  108. return 0;
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement