Le migliori 150 intervisteFacile

Regioni Circondate

Guida dettagliata e implementazione Python per il problema delle "regioni circondate".

Dichiarazione del problema

Facile

Data una scheda matrice m x n contenente "X" e "O", cattura tutte le regioni circondate in 4 direzioni da "X".

Una regione viene catturata invertendo tutte le "O" nelle "X" nella regione circondata.

Scrivi una funzione solve(board: List[List[str]]) -> List[List[str]] che restituisca la scheda modificata.

Vincoli
  • m == len(board)
  • n == len(board[i])
  • 1 <= m, n <= 200
  • board[i][j] is 'X' or 'O'

Esempi

Example 1
Input
board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]
Output
[["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]
Explanation

Surrounded regions should not be on the border, which means any 'O' on the border of the board is not flipped to 'X'. Any 'O' that is connected to a border 'O' is also not flipped.

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche di Graphs come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Apri nell'editor
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.