示例#1
0
        public NumberMove[] Solve()
        {
            if (_totalSum % _k != 0)
            {
                return(null);
            }

            for (BoxId boxId = 0; boxId < _k; boxId++)
            {
                _moveGroups[boxId] = FindMoveGroups(_boxes[boxId]);
            }

            var resultMoveGroups = new List <MoveGroup>(_k);

            if (!TryFindResultMoveGroups(BoxGroup.Empty, resultMoveGroups))
            {
                return(null);
            }

            var result = new NumberMove[_k];

            foreach (var moveGroup in resultMoveGroups)
            {
                ExpandMoveGroup(moveGroup, result);
            }

            return(result);
        }
示例#2
0
        private void ExpandMoveGroup(MoveGroup moveGroup, NumberMove[] result)
        {
            var targetBoxId = moveGroup.BoxId;
            var searchFor   = moveGroup.InitialMoveNumber;

            while (true)
            {
                searchFor = (int)(_targetSum - (_boxes[targetBoxId].Sum - searchFor));
                if (searchFor == moveGroup.InitialMoveNumber)
                {
                    break;
                }

                var sourceBoxId = _numberToBoxMap[searchFor];
                result[sourceBoxId] = new NumberMove(searchFor, targetBoxId);
                targetBoxId         = sourceBoxId;
            }

            result[moveGroup.BoxId] = new NumberMove(moveGroup.InitialMoveNumber, targetBoxId);
        }