[T14] The Pythonsopher's Stone


English Version | [ver versão em português]

Write the function described below. Return the answer as an integer.

This is an example exercise used in T14. It cannot be submitted to Mooshak.

Harry reaching for a glowing red stone

After getting past Fluffy, the flying keys, Devil's Snare and the wizard chess, Harry finds a chamber full of stones. One of them might be the Pythonsopher's Stone! He can pick up a stone to check whether it is real only if there is enough space around it. How many stones can he inspect?

The Problem

Write a function accessible_stones(grid). The argument grid is a list of strings, one string per row of a rectangular map. The character @ represents a stone and . represents an empty cell.

A stone is accessible if fewer than four of its neighbours are stones. A cell has up to eight neighbours, including the cells directly above, below, to the left and right, and the four diagonals. Only positions inside the map count and the stone itself is excluded.

Count the accessible stones on the original map. Leave every stone in place while counting, even if Harry can access it. The function should return the total without printing or modifying the input.

Constraints

1 ≤ len(grid) ≤ 200Number of rows
1 ≤ len(grid[0]) ≤ 200Number of columns

All rows have the same length. Every character is @ or ..

Example Function Calls 1Example Output 1
print(accessible_stones(["@"]))
print(accessible_stones(["..."]))
print(accessible_stones(["@@@", ".@.", ".@."]))
1
0
4
Example Function Call 2Example Output 2
grid = [
    "..@@.@@@@.",
    "@@@.@.@.@@",
    "@@@@@.@.@@",
    "@.@@@@..@.",
    "@@.@@@@.@@",
    ".@@@@@@@.@",
    ".@.@.@.@@@",
    "@.@@@.@@@@",
    ".@@@@@@@@.",
    "@.@.@@@.@.",
]
print(accessible_stones(grid))
13
Each x marks one of the 13 stones Harry can inspect. The other stones are marked @, and empty cells are marked ..
..xx.xx@x.
x@@.@.@.@@
@@@@@.x.@@
@.@@@@..@.
x@.@@@@.@x
.@@@@@@@.@
.@.@.@.@@@
x.@@@.@@@@
.@@@@@@@@.
x.x.@@@.x.

Versão Portuguesa | [see English version]

Escreve a função descrita abaixo. Devolve a resposta como um número inteiro.

Este exercício serve de exemplo na aula T14 e não pode ser submetido no Mooshak.

Harry a tentar alcançar uma pedra vermelha brilhante

Depois de ultrapassar o Fluffy, as chaves voadoras, o Visgo do Diabo e o xadrez de feiticeiros, Harry encontra uma sala cheia de pedras. Uma delas poderá ser a Pedra Pythonsófica! Harry só consegue apanhar uma pedra para verificar se é a verdadeira quando tem espaço suficiente à sua volta. Quantas pedras consegue examinar?

O Problema

Escreve uma função accessible_stones(grid). O argumento grid é uma lista de strings, uma por cada linha de um mapa retangular. O carácter @ representa uma pedra e . representa uma célula vazia.

Uma pedra está acessível se houver menos de quatro pedras nas células vizinhas. Cada célula tem até oito vizinhas, incluindo as que ficam imediatamente acima, abaixo, à esquerda e à direita, e as quatro diagonais. Só contam as posições dentro do mapa e a própria pedra fica excluída.

Conta as pedras acessíveis no mapa original. Durante a contagem, deixa todas as pedras nas suas posições, mesmo que Harry lhes consiga aceder. A função deve devolver o total sem imprimir nem alterar o argumento.

Restrições

1 ≤ len(grid) ≤ 200Número de linhas
1 ≤ len(grid[0]) ≤ 200Número de colunas

Todas as linhas têm o mesmo comprimento. Cada carácter é @ ou ..

Exemplos de Chamadas 1Resultados 1
print(accessible_stones(["@"]))
print(accessible_stones(["..."]))
print(accessible_stones(["@@@", ".@.", ".@."]))
1
0
4
Exemplo de Chamada 2Resultado 2
grid = [
    "..@@.@@@@.",
    "@@@.@.@.@@",
    "@@@@@.@.@@",
    "@.@@@@..@.",
    "@@.@@@@.@@",
    ".@@@@@@@.@",
    ".@.@.@.@@@",
    "@.@@@.@@@@",
    ".@@@@@@@@.",
    "@.@.@@@.@.",
]
print(accessible_stones(grid))
13
Cada x marca uma das 13 pedras que Harry pode examinar. As outras pedras estão marcadas com @ e as células vazias com ..
..xx.xx@x.
x@@.@.@.@@
@@@@@.x.@@
@.@@@@..@.
x@.@@@@.@x
.@@@@@@@.@
.@.@.@.@@@
x.@@@.@@@@
.@@@@@@@@.
x.x.@@@.x.