Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Bitnami MediaWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
1189 - Lenes
(section)
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
= Explicație = <code>p = 2</code> Leneșul traversează terenul de la Nord la Sud pe coloana a <code>7</code>-a cu popasuri în zonele <code>(3, 6)</code>, <code>(1, 6)</code>, <code>(4, 6)</code>, iar de la Sud la Nord pe coloana a <code>5</code> – a, cu popas în zonele <code>(4, 4)</code> și <code>(2, 6)</code>. '''Atenție! Pentru acest test se rezolvă doar cerința 2.''' <syntaxhighlight lang="python" line="1"> import numpy as np def min_effort_nord_sud(matrix, k1): M, N = len(matrix), len(matrix[0]) inf = float('inf') # dp[c][r][p]: efort minim pentru a ajunge la (c, r) cu p popasuri rămase dp = np.full((M, N, k1 + 1), inf) # Inițializare for r in range(N): dp[0][r][k1] = matrix[0][r] for c in range(1, M): for r in range(N): for p in range(k1 + 1): # Deplasare pe coloana curentă if dp[c-1][r][p] < inf: dp[c][r][p] = min(dp[c][r][p], dp[c-1][r][p] + matrix[c][r]) # Popasuri (stânga/dreapta) if r > 0 and p > 0: dp[c][r][p-1] = min(dp[c][r][p-1], dp[c-1][r-1][p] + matrix[c][r]) if r < N - 1 and p > 0: dp[c][r][p-1] = min(dp[c][r][p-1], dp[c-1][r+1][p] + matrix[c][r]) return min(dp[M-1][r][0] for r in range(N)) def min_effort_nord_sud_sud_nord(matrix, k1, k2): M, N = len(matrix), len(matrix[0]) inf = float('inf') # Minim efort Nord-Sud min_effort_nord_sud_val = min_effort_nord_sud(matrix, k1) # Inversăm matricea pentru Sud-Nord matrix_reversed = matrix[::-1] # Minim efort Sud-Nord min_effort_sud_nord_val = min_effort_nord_sud(matrix_reversed, k2) return min_effort_nord_sud_val + min_effort_sud_nord_val # Exemple de utilizare M, N = 4, 5 k1 = 2 k2 = 1 matrix = [ [2, 4, 1, 8, 3], [6, 3, 2, 5, 1], [5, 2, 6, 7, 9], [8, 4, 2, 1, 5] ] # Efort minim pentru traversarea Nord-Sud effort_nord_sud = min_effort_nord_sud(matrix, k1) print(f"Efort minim pentru traversarea Nord-Sud: {effort_nord_sud}") # Efort minim pentru traversarea Nord-Sud și Sud-Nord effort_nord_sud_sud_nord = min_effort_nord_sud_sud_nord(matrix, k1, k2) print(f"Efort minim pentru traversarea Nord-Sud și Sud-Nord: {effort_nord_sud_sud_nord}") </syntaxhighlight>
Summary:
Please note that all contributions to Bitnami MediaWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Bitnami MediaWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Toggle limited content width