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 def count_slices(pizza: List[str], n: int) -> int: # Marque chaque case visitée visited = [[False for _ in range(n)] for _ in range(n)] def dfs(x: int, y: int) -> None: # Vérifie si la position est dans les limites du tableau et non visitée if x < 0 or x >= n or y < 0 or y >= n or visited[x][y] or pizza[x][y] == '.': return # Marque la position comme visitée visited[x][y] = True # Recursion dans les quatre directions dfs(x + 1, y) dfs(x - 1, y) dfs(x, y + 1) dfs(x, y - 1) slice_count = 0 # Parcourt chaque position dans le tableau for i in range(n): for j in range(n): # Si la position est un morceau de pizza non visité if pizza[i][j] == '#' and not visited[i][j]: # Incrémente le nombre de parts slice_count += 1 # Lancement de DFS pour marquer toute la part dfs(i, j) return slice_count # Lecture de l'entrée import sys input = sys.stdin.read data = input().splitlines() n = int(data[0]) pizza = data[1:] # Calcul et affichage du nombre de parts print(count_slices(pizza, n))
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.