Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //pion1 #4611
- #include <iostream>
- #include <cmath>
- using namespace std;
- int n, dp[101][101];
- /*
- problema clasica de dinamica
- pentru toate elemente, verific toate modalitatile
- de a ajunge pe acea pozitie si o aleg pe cea de valoare
- maxima; afisez rezultat maxim;
- */
- void citire()
- {
- cin>>n;
- for (int i=1; i<=n; ++i)
- for (int j=1; j<=n; ++j)
- cin>>dp[i][j];
- }
- void rezolvare()
- {
- for (int i=2; i<=n; ++i)
- {
- for (int j=1; j<=n; ++j)
- {
- if (!dp[i][j])
- dp[i][j]+=dp[i-1][j];
- else
- dp[i][j]+=max(dp[i-1][j-1], dp[i-1][j+1]);
- }
- }
- int maxim=0;
- for (int i=1; i<=n; ++i)
- if (dp[n][i]>maxim)
- maxim=dp[n][i];
- cout<<maxim<<'\n';
- }
- /*reconstituire drum
- rezolva cerinta problemei+reconstituie drumul parcurs de pion
- int dp2[101][101];
- struct
- {
- int i_ant, j_ant;
- }poz[101][101];
- void copiere()
- {
- for (int i=1; i<=n; ++i)
- for (int j=1; j<=n; ++j)
- dp2[i][j]=dp[i][j];
- }
- pair<int, int> v[10001];
- void constituire_drum()
- {
- for (int i=2; i<=n; ++i)
- {
- for (int j=1; j<=n; ++j)
- {
- if (!dp2[i][j])
- {
- dp2[i][j]+=dp2[i-1][j];
- poz[i][j].i_ant=i-1;
- poz[i][j].j_ant=j;
- }
- else
- {
- if (dp2[i-1][j-1]>=dp2[i-1][j+1])
- {
- dp2[i][j]+=dp2[i-1][j-1];
- poz[i][j].i_ant=i-1;
- poz[i][j].j_ant=j-1;
- }
- else
- {
- dp2[i][j]+=dp2[i-1][j+1];
- poz[i][j].i_ant=i-1;
- poz[i][j].j_ant=j+1;
- }
- }
- }
- }
- int maxim=0, j_max;
- for (int i=1; i<=n; ++i)
- if (dp2[n][i]>maxim)
- maxim=dp2[n][i], j_max=i;
- cout<<maxim<<'\n';
- poz[1][1].i_ant=poz[1][1].j_ant=-1;
- int i=n, j=j_max, k=0;
- while (i!=-1)
- {
- v[++k].first=i;
- v[k].second=j;
- int a=i, b=i;
- i=poz[a][b].i_ant;
- j=poz[a][b].j_ant;
- }
- for (int i=k; i>=1; --i)
- cout<<v[i].first<<' '<<v[i].second<<'\n';
- }
- */
- int main()
- {
- citire();
- //copiere();
- //constituire_drum();
- rezolvare();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement