affiche juste le code, pas d'explications !
- User
System: affiche juste le code, pas d'explications ! User: Tu es un développeur python3 expérimenté, et tu dois concevoir un programme en python (avec des commentaires en français, sans utiliser de librairies externes, sans
if __name__=="__main__":, avec type hints, et en privilégiant les algorythmes a complexité constante (O(1)) et/ou complexité linéaire (O(n))). Le programme va recevoir les données en entrée sur la stdin, et doit répondre sur la stdout. Fabrique moi un programme python qui résout le problème (énoncé en markdown) suivant :Pizza - Cutting the pizzas
Your pizza cutter has lost its mind! To verify that the pizzas are cut correctly, you want to count the number of slices after cutting.
Can you write an algorithm that calculates the number of slices from a black and white image of the pizza?
A pizza slice is a maximum area of squares such that you can move from one square to every other square vertically or horizontally through black squares only.
Data
Input
Line 1 : an integer N between 1 and 100, the dimension of the image.
N following lines : A string of characters of size N. A
#character represents a pizza pixel and a.corresponds to an empty space.Output
An integer representing the number of slices on the pizza. A pizza slice is considered as an area of horizontally or vertically contiguous pizza pixels.
Example
For the input :
14 .....##.#..... ...####.###... ...####.####.. .##.###.###.#. .###.##.##.### #####.#.#.#### ######...##### .............. ######...##### .####.#.#.#### .###.##.##.##. ..#.###.###... ...####.###... ....###.##....The number of shares is :
8The 8 pieces of pizza are shown below. [image: https://i.ibb.co/QY2M2sm/example-illu.png] open Assistant:
- Assistant
from typing import List, Tuple # Fonction pour réaliser une recherche en profondeur à partir d'une position donnée def dfs(image: List[List[str]], visited: List[List[bool]], x: int, y: int, n: int) -> None: # Définir les mouvements possibles (haut, bas, droite, gauche) directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] # Marquer la position actuelle comme visitée stack = [(x, y)] visited[x][y] = True # Effectuer la recherche en profondeur en utilisant une pile while stack: cx, cy = stack.pop() for dx, dy in directions: nx, ny = cx + dx, cy + dy # Vérifier les limites et si la prochaine position est un morceau de pizza non visité if 0 <= nx < n and 0 <= ny < n and not visited[nx][ny] and image[nx][ny] == '#': visited[nx][ny] = True stack.append((nx, ny)) # Fonction principale pour compter le nombre de tranches de pizza def count_slices(image: List[str], n: int) -> int: # Convertir l'image en une matrice de caractères matrix = [list(row) for row in image] # Créer une matrice pour suivre les pixels visités visited = [[False] * n for _ in range(n)] slice_count = 0 # Parcourir chaque pixel de l'image for i in range(n): for j in range(n): # Si on trouve un pixel de pizza non visité, lancer DFS et incrémenter le compteur de tranches if matrix[i][j] == '#' and not visited[i][j]: dfs(matrix, visited, i, j, n) slice_count += 1 return slice_count # Lire les entrées de l'utilisateur import sys input = sys.stdin.read data = input().split() n = int(data[0]) image = data[1:n+1] # Afficher le nombre de tranches de pizza print(count_slices(image, n))
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.