Squares sharing a side have different colors 
Let $n\geq 2$. We have $n^2$ unit squares, each square in one of three different colors (some colors may not appear). What is the largest $f(n)$ such that if there are no more than $f(n)$ squares of each color, then the squares can be put into an $n\times n$ square so that any two unit squares that share a side have different colors?

For $n=2$, obviously $f(n)=2$. With $n=3$ the answer is $5$. We can assume that the $n\times n$ square has a checkerboard pattern with $5$ blacks and $4$ whites.  If two colors combine to $5$ unit squares, place them on the black squares. This covers the cases $(5,4,0),(5,3,1),(5,2,2),(4,4,1),(4,3,2)$. For $(3,3,3)$ we can find an arrangement (for example each color going down the main diagonal direction). However, if one color has $6$ unit squares, either some row must contain $3$ of these (which is clearly bad), or each row contains $2$ (which is again impossible).

Let $n\ge 1$ and $0\le r\le g\le b$ with $r+g+b=n^2$ and $b\le\frac 12 n^2$.
Then $b\ge \frac13n^2\ge r$.
Consider a black/white checkerboard pattern with black in the top left corner. Then there are $k:=\lceil \frac{n^2}2\rceil$ black and $w:=\lfloor  \frac{n^2}2\rfloor$ white fields. By the given restrictions, $b\le w$.
Label the fields first black from top left to bottom right, then white from top left to bottom right:
$$\begin{matrix}{1}&k+1&2&k+2&\cdots &\begin{cases}\lceil\frac n2\rceil\\ k+\lfloor \frac n2\rfloor\end{cases}\\
k+\lfloor \frac n2\rfloor+1& \lceil \frac n2\rceil+1&k+\lfloor \frac n2\rfloor+2&\lceil \frac n2\rceil+2&\cdots&\begin{cases}k+n\\ n\end{cases}\\
n+1&k+n+1&n+2&k+n+2&\cdots&\begin{cases}\lceil\frac n2+n\rceil\\ k+\lfloor \frac n2\rfloor+n\end{cases}\\
\vdots&\vdots&\vdots&\vdots&\ddots&\vdots\\
\cdots&\cdots&\cdots&\cdots&\cdots&k
\end{matrix} $$
Note that going two rows down always increases the label by $n$ and that the labels of adjacent squares differ by 
$k$, $k-1$, $k+1$, $k+\lfloor \frac n2\rfloor$, or $k-\lceil\frac n2\rceil$, hence at least by $$\tag1k-\left\lceil\frac n2\right\rceil=\frac{n^2-n}2=w-\left\lfloor \frac n2\right\rfloor.$$
For $n\ge 3$, we have $\frac {n^2}3\le\frac{n^2-n}2$ and therefore 
$$\tag2 r\le\frac{n^2-n}2.$$
Note that $(2)$ holds also when $n<3$.
Now we place blue squares on the (by label) first $b$ fields, green squares on the last $g$ fields, and red squares on the remaining $r$ fields.


*

*All blue squares are on black fields because $b\le k$, hence no two are adjacent

*All green squares are on white fields because $g\le w$, hence no two are adjacent

*Because of $(2)$, any two red square labels differ by at most $\frac{n^2-n}2-1$. It follows from $(1)$ that they are not adjacent.


We conclude that 
$$f(n)\ge \left\lceil\frac12n^2\right\rceil.$$
On the other hand, we partition the $n\times n$ boards into $\lceil \frac {n^2}2\rceil$ regions, namely  $\lfloor \frac{n^2}2\rfloor $ dominoes and possibly one single field. Within each of these regions, at most one blue square is allowed. Hence,
$f(n)\le \left\lceil\frac12n^2\right\rceil$ and ultimately
$$f(n)= \left\lceil\frac12n^2\right\rceil.$$
