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
1576 - zona3
(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!
==Rezolvare== <syntaxhighlight lang="python" line="1"> def validare(n, m, matrice): # validarea datelor de intrare if not (2 <= n <= 100 and 2 <= m <= 100): raise ValueError("Dimensiunile matricei trebuie sa fie cuprinse intre 2 si 100") if not all(len(linie) == m for linie in matrice): raise ValueError("Toate liniile matricei trebuie sa aiba aceeasi lungime") for linie in matrice: for element in linie: if element not in (0, 1): raise ValueError("Matricea trebuie sa contina doar elemente 0 sau 1") def rezolvare(n, m, matrice): # implementarea cerintei def calculeaza_zona(i, j): # calculeaza dimensiunea zonei din care face parte elementul de pe pozitia (i, j) if matrice[i][j] != 1: return 0 zona = 0 stack = [(i, j)] while stack: x, y = stack.pop() if matrice[x][y] == 1: zona += 1 matrice[x][y] = -1 if x > 0 and matrice[x - 1][y] == 1: stack.append((x - 1, y)) if x < n - 1 and matrice[x + 1][y] == 1: stack.append((x + 1, y)) if y > 0 and matrice[x][y - 1] == 1: stack.append((x, y - 1)) if y < m - 1 and matrice[x][y + 1] == 1: stack.append((x, y + 1)) return zona # calculeaza dimensiunea fiecarei zone si inlocuieste valorile cu numarul de elemente din zona for i in range(n): for j in range(m): if matrice[i][j] == 1: zona = calculeaza_zona(i, j) for x in range(n): for y in range(m): if matrice[x][y] == -1: matrice[x][y] = zona matrice[i][j] = zona return matrice def main(): # citim datele de intrare with open("zona3.in", "r") as f: n, m = map(int, f.readline().split()) matrice = [list(map(int, f.readline().split())) for _ in range(n)] # validam datele de intrare validare(n, m, matrice) # rezolvam problema rezultat = rezolvare(n, m, matrice) # afisam rezultatul in fisierul de iesire with open("zona3.out", "w") as f: for linie in rezultat: f.write(" ".join(str(x) for x in linie)) f.write("\n") if __name__ == '__main__': main() </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