1
\$\begingroup\$

I want an n*n table that is filled with black or white and the following conditions are satisfied in it:

  1. All cells of a 2*2 square in this table should not be the same color.
  2. All black and white cells must be connected horizontally or vertically.

Acceptable examples: Yin-Yang table examples

Can anyone design a function that takes n as input and generates an acceptable n*n random table?

  • It is not necessary for the output to be an image, we can use symbols such as 0 and 1 in a two-dimensional array to display the result.
\$\endgroup\$
8
  • 5
    \$\begingroup\$ Should the code generate any one such picture? Or one at random? Must all be possible? What about languages without image support? \$\endgroup\$ Commented Dec 13, 2023 at 20:13
  • 3
    \$\begingroup\$ I want a code that generate an acceptable random table. It is not necessary for the output to be an image, we can use symbols such as 0 and 1 in a two-dimensional array to display the result. \$\endgroup\$ Commented Dec 13, 2023 at 20:23
  • 3
    \$\begingroup\$ @Shahnazi2002 You should make that clear in the post. \$\endgroup\$ Commented Dec 13, 2023 at 20:30
  • 10
    \$\begingroup\$ @Shahnazi2002 If you want random output, you will also need to elaborate what range of outputs and what kind of distribution you want. \$\endgroup\$ Commented Dec 13, 2023 at 20:31
  • 4
    \$\begingroup\$ When people here see the word "random", they expect that you want programs that can output one of multiple tables at random. If you don't intend that, I suggest to change "random table" to "a table of your choice". If you do, please check out things to avoid when writing challenges: random without further specification. \$\endgroup\$ Commented Dec 14, 2023 at 23:34

3 Answers 3

2
\$\begingroup\$

Python 3, 45 bytes

lambda n:["1"*n]+[("10"*n)[:n]]*(n-2)+["0"*n]

Try it online!

\$\endgroup\$
2
\$\begingroup\$

APL (Dyalog Unicode), 12 bytes

Anonymous tacit prefix function, returning a matrix of 0s and 1s.

⊢↑1⍪2↓,⍨⍴2|⍳

Try it online!

indices 1…n

2| division remainder for those, when divided by 2 (gives [1,0,1,0,…])

 cyclically reshape into an array of the following dimensions:

,⍨ the self-concatenation of n, i.e. [n,n]

2↓ drop the first two rows

1⍪ prepend a row of 1s

 take the following number of rows, padding with all-0 rows:

 n (lit. the identity function applied to n)

\$\endgroup\$
1
\$\begingroup\$

05AB1E, 10 bytes

Same output (based on input) on each run:

Å1āÉI<.Da)

Port of @Somebody's Python answer.

Try it online or verify the first ten sizes.

05AB1E, 73 bytes

Random valid output on each run:

1ÝIãIãʒ€ü2ü2εøOO4Ö≠}˜P}ʒD_‚ε©˜ƶ®gäΔ2Fø0δ.ø}2Fø€ü3}®*εεÅsyøÅs«à}}}˜Ùg<}P}Ω

Try it online. (Slightly modified ʒ...}Ω to .r.Δ... to speed it up, but unfortunately it'll still time out for \$n\geq4\$.)

Explanation:

Å1            # Push a list of the (implicit) input amount of 1s
  ā           # Push a list in the range [1,length] (without popping)
   É          # Check for each whether it's odd: [1,0,1,0,1,0,...]
    I<        # Push the input-1
      .D      # Duplicate the [1,0,1,0,1,0,...]-list that many times
        a     # Change the top list to a list of 0s instead with an is_letter check
         )    # Wrap all lists on the stack into a list
              # (after which this matrix is output implicitly as result)

Step 1: Create a list of all possible input by input matrices with 0s and 1s:

1Ý            # Push pair [0,1]
  Iã          # Take the cartesian power of the input to get all input-sized lists using
              # 0s and 1s
    Iã        # And again, to get all possible matrices

Step 2: Filter this list of matrices to keep all where each overlapping 2x2 block contains at least one 1 and at least one 0:

ʒ             # Filter this list of matrices by:
 €ü2ü2εø      #  Get all overlapping 2x2 blocks:
 €            #   Map over each row:
  ü2          #    Get its overlapping pairs
    ü2        #   Get the overlapping lists of lists of pairs
      ε       #   Map over each:
       ø      #    Zip/transpose; swapping rows/columns
 OO           #    Sum each inner 2x2 block
   4Ö≠        #    Check for each sum whether it's NOT divisible by 4
              #    (aka it's 1, 2, or 3, and NOT 0 or 4)
      }       #   Close the map
       ˜      #   Flatten this matrix of 2x2 block sum checks
        P     #   Check that all are truthy by taking the product
}             # Close the filter

Step 3: Filter it again to keep those where all matrices where both the 1s and 0s form a single island:

ʒ             # Filter this list of matrices again:
 D            #  Duplicate this matrix
  _           #  Invert each 0s to 1 and vice-versa in the copy
   ‚          #  Pair the two inverted matrices together
 ε            #  Map over this pair:
  ©           #   Store the current matrix in variable `®` (without popping)
   ˜          #   Flatten it to a single list
    ƶ         #   Multiply each value by its 1-based index
     ®gä      #   Convert it back to a matrix of size `®`
  Δ           #   Loop until the result no longer changes to flood-fill:
   2Fø0δ.ø}   #    Add a border of 0s around the matrix:
   2F     }   #     Loop 2 times:
     ø        #     Zip/transpose; swapping rows/columns
       δ      #     Map over each row:
      0 .ø    #      Add a leading/trailing 0
   2Fø€ü3}    #    Convert it into overlapping 3x3 blocks: 
   2F    }    #     Loop 2 times again:
     ø        #      Zip/transpose; swapping rows/columns
      €       #      Map over each inner list:
       ü3     #       Convert it to a list of overlapping triplets
   ®*         #    Multiply each 3x3 block by the value in matrix `®`
              #    (so the 0s remain 0s)
   εεÅsyøÅs«à #    Get the largest value from the horizontal/vertical cross of each
              #    3x3 block:
   εε         #     Nested map over each 3x3 block:
     Ås       #       Pop and push its middle row
       y      #       Push the 3x3 block again
        ø     #       Zip/transpose; swapping rows/columns
         Ås   #       Pop and push its middle rows as well (the middle column)
           «  #       Merge the middle row and column together to a single list
            à #       Pop and push its maximum
   }}         #     Close the nested maps
  }           #   Close the changes-loop
   ˜          #   Flatten the flood-filled matrix
    Ù         #   Uniquify this list
     g<       #   Pop and push its length-1
              #   (only 1 is truthy in 05AB1E, so this checks if two islands remain)
 }            #  Close the map
  P           #  Check that both are truthy, for the regular and inverted matrix
}             # Close the filter
 Ω            # Pop and push a random matrix from this filtered list
              # (which is output implicitly as result)
\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.