Solucionador Tatamibari


10

Antecedentes

Tatamibari es un rompecabezas lógico diseñado por Nikoli.

Un rompecabezas Tatamibari se juega en una cuadrícula rectangular con tres tipos diferentes de símbolos: +, -. y |. El solucionador debe dividir la cuadrícula en regiones rectangulares o cuadradas de acuerdo con las siguientes reglas:

  • Cada partición debe contener exactamente un símbolo en ella.
  • Un +símbolo debe estar contenido en un cuadrado.
  • Un |símbolo debe estar contenido en un rectángulo con una altura mayor que el ancho.
  • Un -símbolo debe estar contenido en un rectángulo con un ancho mayor que la altura.
  • Cuatro piezas nunca pueden compartir la misma esquina. (Así es como se suelen colocar los mosaicos de tatami japoneses)

El siguiente es un ejemplo de rompecabezas, con una solución:

Ejemplo de rompecabezas Tatamibari Ejemplo de solución de rompecabezas Tatamibari

Tarea

Resuelve el rompecabezas Tatamibari dado.

De entrada y salida

La entrada es una cuadrícula 2D que representa el rompecabezas Tatamibari dado. Cada célula contiene uno de los cuatro personajes: +, -, |, y un personaje de su elección para representar una célula no pista. En los casos de prueba, *se usa un asterisco .

Puede elegir cualquier formato de salida adecuado que pueda representar inequívocamente cualquier solución válida para un rompecabezas Tatamibari. Esto incluye, pero no se limita a: (si tiene dudas, pregunte en los comentarios).

  • Una lista de 4 tuplas, donde cada tupla incluye el índice superior, el índice izquierdo, el ancho y la altura de un rectángulo (o cualquier representación equivalente)
  • Una cuadrícula numérica de la misma forma que la entrada, donde cada número representa un rectángulo
  • Una lista de conjuntos de coordenadas, donde cada conjunto incluye todas las coordenadas de las celdas en un rectángulo

Si un rompecabezas tiene múltiples soluciones, puede generar cualquier número (una o más) de sus soluciones válidas. Se garantiza que la entrada tenga al menos una solución.

Casos de prueba

Puzzle:
|-*
*+|
*-*
Solution:
122
134
554
=====
Puzzle:
+***
**|*
*+**
***-
Solution:
1122
1122
3322
3344
======
Puzzle:
|*+*+
*****
****-
***+|
+****
Solution:
12233
12233
44444
55667
55667
=======
Puzzle:
****-**
**-**|*
*|*****
****-**
*******
**+*|**
*****+*
One possible solution:
1122222
1133344
1155544
1155544
6667744
6667788
6667788
===========
Puzzle:
*-****|+**
+*-******|
****+*****
*-******||
**++|*****
+****-|***
-****-**+*
********-*
|*+*+|****
*-*--**+*+
Solution:
1111122334
5666622334
7777822994
7777A2299B
CCDEA2299B
CCFFFFGGHH
IIIIJJGGHH
KLLMMNGGOO
KLLMMNGGPP
QQRRSSSTPP

Reglas

Se aplican reglas estándar de . El código más corto en bytes gana.

Respuestas:


5

Python 2 , 417 374 366 bytes

La entrada es una lista de líneas, ~para no tener idea. Produce una única solución para stderr en el formato (x_start, width, y_start, height).

R=range
k=input()
X,Y=len(k[0]),len(k)
W,H=R(X),R(Y)
Q=[[]]
for q in Q:C=[(x,y)for(a,b,c,d)in q for x in(a,a+b)for y in(c,c+d)];max(map(C.count,C+W))<4>0<all(sum(w>x-s>-1<y-t<h<[c for r in k[t:t+h]for c in r[s:s+w]if'~'>c]==['+|-'[cmp(h,w)]]for(s,w,t,h)in q)==1for x in W for y in H)>exit(q);Q+=[q+[(s,w+1,t,h+1)]for s in W for w in R(X-s)for t in H for h in R(Y-t)]

Pruébalo en línea! Esto es demasiado ineficiente para los casos de prueba sugeridos.


Sin golf

grid = input()
total_width = len(grid[0])
total_height = len(grid)

partitions = [[]]

for partition in partitions:
    # list the corners of all rectangles in the current partition
    corners = [(x, y)
               for (start_x, width, start_y, height) in partition
               for x in (start_x, start_x + width)
               for y in (start_y, start_y + height)]
    # if no corners appears more than three times ...
    if corners != [] and max(map(corners.count, corners)) < 4:
        # .. and all fields are covered by a single rectangle ...
        if all(
                sum(width > x - start_x > -1 < y - start_y < height
                    for (start_x, width, start_y, height) in partition) == 1
                for x in range(total_width)
                for y in range(total_height)):
            # ... and all rectangles contain a single non-~
            # symbol that matches their shape:
            if all(
                [char for row in grid[start_y: start_y + height]
                    for char in row[start_x:start_x + width] if '~' > char]
                == ['+|-'[cmp(height, width)]]
                    for (start_x, width, start_y, height) in partition):
                # output the current partition and stop the program
                exit(partition)

    # append each possible rectangle in the grid to the current partition,
    # and add each new partition to the list of partitions.
    partitions += [partition + [(start_x, width + 1, start_y, height + 1)]
                   for start_x in range(total_width)
                   for width in range(total_width - start_x)
                   for start_y in range(total_height)
                   for height in range(total_height - start_y)]

Pruébalo en línea!

Al usar nuestro sitio, usted reconoce que ha leído y comprende nuestra Política de Cookies y Política de Privacidad.
Licensed under cc by-sa 3.0 with attribution required.