I am going to assume that the grid must be at least 2-by-2
This avoids the Trivial Case of $K=3$ using the 4-by-1 pattern ABCA
Every colour must connect to every other colour once, and only once. As such, the number of Connections for $K$ colours must be the $(K-1)^{th}$ Triangular number, or $\frac{(K-1)^2+(K-1)}{2}$, which we can rewrite as $\frac{K^2-K}{2}$
For any X by Y grid (for X>1 and Y>1), each cell has 3 possibilities:
- $C$orner: Adjacent to 2 other colours
- $E$dge: Adjacent to 3 other colours
- $M$iddle: Adjacent to 4 different colours
An $X$ by $Y$ grid has $4$ Corner squares, $2(X-2)+2(Y-2)$ Edge squares and $(X-2)(Y-2)$ Middle squares. Between them, these contribute $8$, $6(X+Y-4)$, and $4(XY-2X-2Y+4)$ half-connections (since connections are paired)
Rearrange that, and you get $2XY-(X+Y)$ connections.
So, as a first rule, we can only fit $K$ colours into an $X$ by $Y$ grid, if $\frac{K^2-K}{2}=2XY-(X+Y)$
Next comes the types of combinations. Every Colour must make exactly $K-1$ connections. If we take your example of $K=10$, then we can make $9$ in 3 different ways:
- $3E=3*(3)$
- $C+E+M=(2)+(3)+(4)$
- $3C+E=3*(2)+(3)$
We can immediately made several deductions from this:
First, we see that every colour must appear on at least 1 Edge Piece (of which there are 14), and, second, that either all 4 Corners are different, or 3 of them are the same colour.
However, we also see that there is only 1 method for using each of our 10 $M$iddle pieces - and this requires 1 Corner per Middle piece. But, there are only 4 Corners!
As such:
It is impossible to solve the puzzle for 10 Colours in a 4*7 grid.
After messing around for a while, going in circles (Literally - I thought I was managing to get somewhere, but all I proved was that the $(K-1)^{th}$ triangular Number was, in fact, a triangular number), I got bored, and brute-forced some Integer Solutions to $\frac{K^2-K}{2}=2XY-(X+Y)$ in Excel. Sorry.
The lowest match integer match to the First Rule that neither subrunner nor myself have proven impossible is $K=12$, for $X=4$ and $Y=10$.
This gives us
- 4 Corners
- 20 Edges
- 16 Middles
How many ways to make $11$?
$4C+1E = 4*(2)+1*(3)$
$2C+1E+1M = 2*(2)+1*(3)+1*(4)$
$1C+3E = 1*(2)+3*(3)$
$1E+2M = 1*(3)+2*(4)$
Excellent, this looks promising. Let's start by eliminating all 4 Corners, and cutting down the Edges:
4 number, each costing 1 Corner and 3 Edges.
The total is then 4 Corners, 12 Edges. This leaves us:
- 0 Corners
- 8 Edges
- 16 Middles
Note: We can't use $4C+1E$, because that leaves 19 Edges and 16 Midles, which are not in a 1:2 ratio. Similarly, we can't use $2C+1E+1M$, because that leaves either 18 Edges and 14 Midles (2 numbers, each in 2 corners) or 13 Edges and 15 Midles (3 numbers, 1 in 2 corners and 2 in 1 corner each), because - again - we do not have the 1:2 ratio
This is absolutely perfect, because:
Our remaining 8 numbers will each cost 1 Edge and 2 Middles.
This adds up to 8 Edges, and 16 Middles - exactly what we have left!
The second lowest answer to our First Rule is $K=17$, for $X=11 \lor 20$ and $Y=7 \lor 4$
This gives us
- 4 Corners
- 28 or 40 Edges
- 45 or 36 Middles
So, how many ways can we make $16$?
- $2C+4E=2*(2)+4*(3)$
- $2C+3M=2*(2)+3*(4)$
- $1C+2E+2M=1*(2)+2*(3)+2*(4)$
- $4E+1M=4*(3)+1*(4)$
- $4M=4*(4)$
This also looks promising - if we allocate a different number to each Corner:
4 numbers, costing 4 Corners, 8 Edges, and 8 Middles
- 0 Corners
- 20 or 32 Edges
- 37 or 28 Middles
Next, we allocate all of the remaining Edges:
5 numbers, costing 20 Edges and 5 Middles or
8 numbers, costing 32 Edges and 8 Middles
- 0 Corners
- 0 or 0 Edges
- 32 or 20 Middles
Which leaves us with
8 numbers, costing 32 Middles or
5 numbers, costing 20 Middles
This gives us
3 theoretical grids to test:
$K=12$, 4*10
$K=17$, 7*11
$K=17$, 4*20