|
def print_board(tablero): |
|
output = "" |
|
for i in range(len(tablero)): |
|
if i % 3 == 0 and i != 0: |
|
output += "------+-------+------\n" |
|
|
|
for j in range(len(tablero[0])): |
|
if j % 3 == 0 and j != 0: |
|
output += "| " |
|
|
|
if j == 8: |
|
output += str(tablero[i][j]) + "\n" |
|
else: |
|
output += str(tablero[i][j]) + " " |
|
|
|
return output |
|
|
|
def resolver(tablero): |
|
buscar = buscar_vacio(tablero) |
|
if not buscar: |
|
return True |
|
else: |
|
fila, columna = buscar |
|
|
|
for i in range(1,10): |
|
if es_valido(tablero, i, (fila, columna)): |
|
tablero[fila][columna] = i |
|
|
|
if resolver(tablero): |
|
return True |
|
|
|
tablero[fila][columna] = 0 |
|
|
|
return False |
|
|
|
def es_valido(tablero, num, pos): |
|
|
|
for i in range(len(tablero[0])): |
|
if tablero[pos[0]][i] == num and pos[1] != i: |
|
return False |
|
|
|
|
|
for i in range(len(tablero)): |
|
if tablero[i][pos[1]] == num and pos[0] != i: |
|
return False |
|
|
|
|
|
caja_x = pos[1] // 3 |
|
caja_y = pos[0] // 3 |
|
|
|
for i in range(caja_y*3, caja_y*3 + 3): |
|
for j in range(caja_x * 3, caja_x*3 + 3): |
|
if tablero[i][j] == num and (i,j) != pos: |
|
return False |
|
|
|
return True |
|
|
|
def buscar_vacio(tablero): |
|
for i in range(len(tablero)): |
|
for j in range(len(tablero[0])): |
|
if tablero[i][j] == 0: |
|
return (i, j) |
|
|
|
return None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import gradio as gr |
|
|
|
def function(cadena): |
|
|
|
tablero = [] |
|
for i in range(0, 81, 9): |
|
row = [int(num) for num in cadena[i:i+9]] |
|
tablero.append(row) |
|
|
|
resolver(tablero) |
|
|
|
return print_board(tablero) |
|
|
|
examples=[["400030000000600800000000001000050090080000600070200000000102700503000040900000000"], |
|
["708000300000201000500000000040000026300080000000100090090600004000070500000000000"], |
|
["708000300000601000500000000040000026300080000000100090090200004000070500000000000"], |
|
["307040000000000091800000000400000700000160000000250000000000380090000500020600000"], |
|
["500700600003800000000000200620400000000000091700000000000035080400000100000090000"], |
|
["400700600003800000000000200620500000000000091700000000000043080500000100000090000"], |
|
["040010200000009070010000000000430600800000050000200000705008000000600300900000000"], |
|
["705000002000401000300000000010600400200050000000000090000370000080000600090000080"], |
|
["000000410900300000300050000048007000000000062010000000600200005070000800000090000"], |
|
["705000002000401000300000000010600400200050000000000090000370000090000800080000060"], |
|
["080010000005000030000000400000605070890000200000300000200000109006700000000400000"], |
|
["809000300000701000500000000070000026300090000000100040060200004000080500000000000"], |
|
["609000008000701000400000000000060004020000030030000500010500070800090000000000200"], |
|
["000000410900300000300020000048007000000000052010000000500200006070000800000090000"], |
|
["100048000050000900006000300000570200803000000000900000000000041670000000000200000"], |
|
["708000300000601000400000000060000025300080000000100090090500002000070400000000000"], |
|
["403000002000601000800000000000500790200030000010000000000840000090000600070000050"], |
|
["010620000500000043000090000700000080005000004000100000000003600090000200800007000"], |
|
["403000002000601000800000000000500970200030000010000000000840000090000600070000050"], |
|
["805000002000901000300000000060700400200050000000000060000380000010000900040000070"], |
|
["000020040070006000010500000200000080000300700409000000000600103800090000000000500"], |
|
["805000002000901000300000000060700400200050000000000060000380000040000700010000090"], |
|
["000040001030600000800000000109005000000000870000200000070000260500094000000000300"], |
|
["307004020000100800900000000000030090050800000040600000000000501200070000000000600"], |
|
["000903100607000080200000000050000400000060020010000000800070000000300500000400009"], |
|
["805000002000401000300000000060700400200050000000000090000380000010000700090000060"], |
|
["704000002000801000300000000050600100200040000000000090000370000090000500080000060"], |
|
["704000002000801000300000000050600100200040000000000090000370000080000600090000050"], |
|
["001000007000890000000000600260030000000500074900000000000104050830000000000000200"], |
|
["200040500010000030000000000006000802070309000000100000400050600000700090008000000"], |
|
["805000002000901000300000000060700400200050000000000060000380000010000700040000090"], |
|
["080000063000040200000000000010803500700000900000600000209070000000000035400000000"], |
|
["090300020000070500010000000708600400000902000500000000000100063400080000000000000"], |
|
["780400120600075009000601078007040260001050930904060005070300012120007400049206007"]] |
|
|
|
demo = gr.Interface( |
|
fn=function, |
|
inputs=gr.Textbox(lines=2, label="Sudoku", placeholder="Ingrese la cadena del sudoku"), |
|
outputs=gr.Textbox(lines=10, label="Resultado", placeholder="Resultado..."), |
|
title="Resolver sudoku", |
|
examples=examples |
|
) |
|
|
|
demo.launch(inline=False) |