Пример #1
0
        public void FindAllSolutions()
        {
            int permutationLength = 5;
            int sum   = 75;
            int level = 1;
            int index = 1;

            FixFigurePositions();
            Dictionary <int, int> currentValuesForPositionsInFigure = new Dictionary <int, int>();

            currentValuesForPositionsInFigure.Add(19, 6);
            currentValuesForPositionsInFigure.Add(20, 5);
            currentValuesForPositionsInFigure.Add(21, 4);
            currentValuesForPositionsInFigure.Add(22, 3);
            currentValuesForPositionsInFigure.Add(23, 2);
            currentValuesForPositionsInFigure.Add(24, 1);

            List <int> valuesForPermutationsGenerations = new List <int>();

            for (int j = 1; j < initialCount + 1; j++)
            {
                valuesForPermutationsGenerations.Add(j);
            }
            permutationGenerator = new PermutationsGenerator(permutationLength);

            List <List <int> > permutationList = permutationGenerator.FindPermutations(
                valuesForPermutationsGenerations, level, index, new Stack <int>(), sum).ToList();
            int key = 1;

            foreach (var permutation in permutationList)
            {
                FindPositionsValuesWithoutFixedPositions(permutation, valuesForPermutationsGenerations, initialCount, key, currentValuesForPositionsInFigure);
            }
        }
Пример #2
0
        private void FindPositionsValuesWithoutFixedPositions(List <int> permutation, List <int> valuesForPermutationsGenerations, int initialCount, int key, Dictionary <int, int> currentValuesForPositionsInFigure)
        {
            Dictionary <int, int> currentValuesForPositions = new Dictionary <int, int>(currentValuesForPositionsInFigure);
            var lastRawPositions = positionsInFigure[key];
            int i = 0;

            foreach (var value in lastRawPositions)
            {
                if (!currentValuesForPositions.ContainsKey(value))
                {
                    currentValuesForPositions.Add(value, permutation[i]);
                    i++;
                }
            }

            valuesForPermutationsGenerations.Clear();
            for (int j = 1; j < 25; j++)
            {
                if (!currentValuesForPositions.ContainsValue(j))
                {
                    valuesForPermutationsGenerations.Add(j);
                }
            }
            initialCount = valuesForPermutationsGenerations.Count;
            var currentRawPositions = positionsInFigure[key + 1];
            int sum = 0;

            foreach (var currentPosition in currentRawPositions)
            {
                foreach (var lastPosition in lastRawPositions)
                {
                    if (currentPosition == lastPosition)
                    {
                        var value = currentValuesForPositions[currentPosition];
                        sum = sum + value;
                    }
                }
            }
            sum = givenSum - sum;
            if (key + 1 == 6)
            {
                CheckLastRawInFigure(valuesForPermutationsGenerations, currentValuesForPositions, currentRawPositions);
                return;
            }
            permutationGenerator = new PermutationsGenerator(3);
            List <List <int> > permutationList = permutationGenerator.FindPermutations(
                valuesForPermutationsGenerations, 1, 1, new Stack <int>(), sum).ToList();

            if (permutationList.Count == 0)
            {
                return;
            }
            foreach (var com in permutationList)
            {
                FindPositionsValuesWithoutFixedPositions(com, valuesForPermutationsGenerations, initialCount, key + 1, currentValuesForPositions);
            }
        }