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
0882 - Lac
(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" start="1"> def validare(nr_n, nr_m, matrix): # functia de validare a datelor de intrare if nr_n < 1 or nr_n > 1000 or nr_m < 1 or nr_m > 1000: raise ValueError for linie in matrix: if len(linie) != nr_m: raise ValueError file_out.write("Datele de intrare corespund restrictiilor impuse\n") def dfs(i, j, nr_n, nr_m, matrix, vizitat): dx = [-1, 0, 1, 0] dy = [0, 1, 0, -1] vizitat[i][j] = True for directie in range(4): x_nou, y_nou = i + dx[directie], j + dy[directie] if 0 <= x_nou < nr_n and 0 <= y_nou < nr_m and matrix[x_nou][y_nou] == 1 and not vizitat[x_nou][y_nou]: dfs(x_nou, y_nou, n, m, matrix, vizitat) def numara_insule_peninsule(nr_n, nr_m, matrix): # functia de rezolvare vizitat = [[False]*nr_m for _ in range(nr_n)] insule = peninsule = 0 for i in range(nr_n): for j in range(nr_m): if matrix[i][j] == 1 and not vizitat[i][j]: if i == 0 or i == nr_n-1 or j == 0 or j == nr_m-1: peninsule += 1 else: insule += 1 dfs(i, j, nr_n, nr_m, matrix, vizitat) file_out.write(f'{insule} {peninsule}\n') if __name__ == '__main__': try: file_in = open('lacin.txt', 'r') file_out = open('lacout.txt', 'w') n, m = map(int, file_in.readline().split()) matrice = [list(map(int, file_in.readline().split())) for _ in range(n)] validare(n, m, matrice) numara_insule_peninsule(n, m, matrice) except ValueError: file_out.write("Datele de intrare nu corespund restrictiilor impuse\n") except IndexError: file_out.write("Datele de intrare nu corespund restrictiilor impuse\n") </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