add memorize function to SearchQueenGemsPattern.