Solving a CSP using AC-3 Peter (P), Mary (M), Otto (O) and Dicky (D) would like to rent an apartment house. The house has three floors: G/F, 1/F, 2/F. Every floor has only one apartment. P, M, O and D must be assigned to exactly one floor. Note that more than one person can live in the same apartment. Consider the following constraints.

P and M cannot live in the same apartment.
D must live on a higher floor than O.
D must live alone.
If P and O live together, they must be on 1/F.
If P and O live separately, one of them must live on the 2/F.


How do I use AC-3 to solve this question?
I found two possible solutions: P,M,O,D = 2,0,0,1 or 1,0,1,2
AC-3 is an algorithm for solving CSPs. In order to use it solve a question, you need to present the question as solving a CSP.
In your case, the CSP is on four variables $P,M,O,D$ ranging over $\{0,1,2\}$. The constraints are:

$P \neq M$.
$D &gt; O$.
$D \neq P$.
$D \neq M$.
$(P = O) \Rightarrow P = 1$.
$(P \neq O) \Rightarrow (P = 2 \lor O = 2)$.

We can convert them into the following explicit constraints:

$(P,M),(D,P),(D,M) \in \{(0,1),(0,2),(1,0),(1,2),(2,0),(2,1)\}$.
$(D,O) \in \{(1,0),(2,0),(2,1)\}$.
$(P,O) \in \{(0,2),(1,1),(1,2),(2,0),(2,1)\}$.

You can now take any implementation of AC-3 and simply run it on these constraints.