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
3651 - Ghem
(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!
= Exemplu: = <code>ghem.in</code> 4 1 1 O <code>ghem.out</code> 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 === Explicație === Matricea alăturată se desfășoară “trăgând” de colțul din stânga sus în direcție orizontală obținându-se șirul: <code>1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10</code> <syntaxhighlight lang="python" line="1"> def generate_matrix(N): matrix = [] num = 1 for i in range(N): row = [] for j in range(N): row.append(num) num += 1 matrix.append(row) return matrix def spiral_from_top_left(matrix, N): result = [] top, left, bottom, right = 0, 0, N - 1, N - 1 while top <= bottom and left <= right: for i in range(left, right + 1): result.append(matrix[top][i]) top += 1 for i in range(top, bottom + 1): result.append(matrix[i][right]) right -= 1 if top <= bottom: for i in range(right, left - 1, -1): result.append(matrix[bottom][i]) bottom -= 1 if left <= right: for i in range(bottom, top - 1, -1): result.append(matrix[i][left]) left += 1 return result def spiral_from_bottom_right(matrix, N): result = [] bottom, right, top, left = N - 1, N - 1, 0, 0 while bottom >= top and right >= left: for i in range(right, left - 1, -1): result.append(matrix[bottom][i]) bottom -= 1 for i in range(bottom, top - 1, -1): result.append(matrix[i][left]) left += 1 if bottom >= top: for i in range(left, right + 1): result.append(matrix[top][i]) top += 1 if right >= left: for i in range(top, bottom + 1): result.append(matrix[i][right]) right -= 1 return result def spiral_from_top_right(matrix, N): result = [] top, right, bottom, left = 0, N - 1, N - 1, 0 while top <= bottom and right >= left: for i in range(top, bottom + 1): result.append(matrix[i][right]) right -= 1 for i in range(right, left - 1, -1): result.append(matrix[bottom][i]) bottom -= 1 if right >= left: for i in range(bottom, top - 1, -1): result.append(matrix[i][left]) left += 1 if top <= bottom: for i in range(left, right + 1): result.append(matrix[top][i]) top += 1 return result def spiral_from_bottom_left(matrix, N): result = [] bottom, left, top, right = N - 1, 0, 0, N - 1 while bottom >= top and left <= right: for i in range(left, right + 1): result.append(matrix[bottom][i]) bottom -= 1 for i in range(bottom, top - 1, -1): result.append(matrix[i][right]) right -= 1 if left <= right: for i in range(right, left - 1, -1): result.append(matrix[top][i]) top += 1 if bottom >= top: for i in range(top, bottom + 1): result.append(matrix[i][left]) left += 1 return result def get_spiral_order(N, X, Y, D): matrix = generate_matrix(N) if (X, Y) == (1, 1): if D == 'O': return spiral_from_top_left(matrix, N) else: return spiral_from_bottom_left(matrix, N)[::-1] elif (X, Y) == (1, N): if D == 'O': return spiral_from_top_right(matrix, N) else: return spiral_from_top_left(matrix, N)[::-1] elif (X, Y) == (N, N): if D == 'O': return spiral_from_bottom_right(matrix, N) else: return spiral_from_top_right(matrix, N)[::-1] elif (X, Y) == (N, 1): if D == 'O': return spiral_from_bottom_left(matrix, N) else: return spiral_from_bottom_right(matrix, N)[::-1] # Citirea datelor de intrare N = int(input()) X = int(input()) Y = int(input()) D = input().strip() # Generarea și afișarea șirului de numere result = get_spiral_order(N, X, Y, D) print(" ".join(map(str, result))) </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