A variational principle for domino tilings