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 sys import stdin, stdout from typing import List, Tuple def dfs(matrix: List[List[str]], x: int, y: int, n: int) -> None: """ Effectue une recherche en profondeur pour marquer tous les pixels continus de pizza à partir de la position (x, y). """ stack = [(x, y)] while stack: cx, cy = stack.pop() if matrix[cx][cy] == '#': matrix[cx][cy] = '.' # Vérifie les voisins directs (haut, bas, gauche, droite) if cx > 0: stack.append((cx - 1, cy)) if cx < n - 1: stack.append((cx + 1, cy)) if cy > 0: stack.append((cx, cy - 1)) if cy < n - 1: stack.append((cx, cy + 1)) def count_pizza_slices(n: int, pizza: List[str]) -> int: """ Compte le nombre de tranches de pizza. """ matrix = [list(row) for row in pizza] count = 0 for i in range(n): for j in range(n): if matrix[i][j] == '#': # Nouvelle tranche trouvée, lance un DFS pour marquer toute la tranche dfs(matrix, i, j, n) count += 1 return count def main(): input = stdin.read data = input().strip().split('\n') n = int(data[0]) pizza = data[1:n+1] result = count_pizza_slices(n, pizza) stdout.write(f"{result}\n")
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.