Click here to Skip to main content
16,005,437 members
Home / Discussions / C#
   

C#

 
QuestionDivide an array of numbers into groups [SOLVED] Pin
Bart Van Eyndhoven28-Apr-10 3:46
Bart Van Eyndhoven28-Apr-10 3:46 
GeneralRe: Divide an array of numbers into groups Pin
harold aptroot28-Apr-10 3:49
harold aptroot28-Apr-10 3:49 
GeneralRe: Divide an array of numbers into groups Pin
Bart Van Eyndhoven28-Apr-10 4:01
Bart Van Eyndhoven28-Apr-10 4:01 
GeneralRe: Divide an array of numbers into groups Pin
harold aptroot28-Apr-10 4:28
harold aptroot28-Apr-10 4:28 
GeneralRe: Divide an array of numbers into groups Pin
Bart Van Eyndhoven28-Apr-10 5:00
Bart Van Eyndhoven28-Apr-10 5:00 
GeneralRe: Divide an array of numbers into groups Pin
harold aptroot28-Apr-10 5:03
harold aptroot28-Apr-10 5:03 
GeneralRe: Divide an array of numbers into groups Pin
Bart Van Eyndhoven28-Apr-10 5:08
Bart Van Eyndhoven28-Apr-10 5:08 
AnswerRe: Divide an array of numbers into groups [modified] Pin
harold aptroot28-Apr-10 4:52
harold aptroot28-Apr-10 4:52 
Tested, works (for the problem as I understood it, anyway):
static class Solver
{
    static int[] RoofSurfaces, Connectables, Sums, AssignedTo;
    public static int[] Solve(int[] connectables, int[] roofSurfaces)
    {
        RoofSurfaces = roofSurfaces;
        Connectables = connectables;
        Sums = new int[roofSurfaces.Length];
        AssignedTo = new int[connectables.Length];
        for (int i = 0; i < AssignedTo.Length; i++)
            AssignedTo[i] = -1;
        if (solve(0))
            return AssignedTo;
        return null;
    }
    static bool solve(int i)
    {
        if (i == Connectables.Length)
            return true;
        for (int k = 0; k < Sums.Length; k++)
        {
            Sums[k] += Connectables[i];
            AssignedTo[i] = k;
            if (Sums[k] <= RoofSurfaces[k])
            {
                if (solve(i + 1))
                    return true;
            }
            Sums[k] -= Connectables[i];
            AssignedTo[i] = -1;
        }
        return false;
    }
}


If there is a solution, it returns an array with the number of the RoofSurface that each Connectable is in, otherwise it returns null.

edit: it may not be the coding style you like etc etc, I'm not trying to set a good example here, I just made this in a couple of minutes to Just Work without looking at the style too closely.
The -1's were just useful for debugging - you don't actually need them (the variable i already determines which entries are valid)

modified on Wednesday, April 28, 2010 11:03 AM

GeneralRe: Divide an array of numbers into groups Pin
Bart Van Eyndhoven28-Apr-10 5:05
Bart Van Eyndhoven28-Apr-10 5:05 
GeneralRe: Divide an array of numbers into groups Pin
harold aptroot28-Apr-10 5:10
harold aptroot28-Apr-10 5:10 
AnswerRe: Divide an array of numbers into groups Pin
V.28-Apr-10 5:06
professionalV.28-Apr-10 5:06 
Questionhow to set default value of combobox in c# Pin
tanzeel8528-Apr-10 3:45
tanzeel8528-Apr-10 3:45 
AnswerRe: how to set default value of combobox in c# Pin
Dan Mos28-Apr-10 3:55
Dan Mos28-Apr-10 3:55 
AnswerRe: how to set default value of combobox in c# [modified] Pin
PIEBALDconsult28-Apr-10 4:33
mvePIEBALDconsult28-Apr-10 4:33 
GeneralRe: how to set default value of combobox in c# Pin
tanzeel8528-Apr-10 4:43
tanzeel8528-Apr-10 4:43 
GeneralRe: how to set default value of combobox in c# Pin
Dan Mos28-Apr-10 4:58
Dan Mos28-Apr-10 4:58 
GeneralRe: how to set default value of combobox in c# Pin
V.28-Apr-10 5:00
professionalV.28-Apr-10 5:00 
Questionrun , show and application in my project and handle its forms Pin
iman_kh28-Apr-10 3:43
iman_kh28-Apr-10 3:43 
AnswerRe: run , show and application in my project and handle its forms Pin
Henry Minute28-Apr-10 11:05
Henry Minute28-Apr-10 11:05 
Questionproject n-tier Pin
genieabdo28-Apr-10 2:20
genieabdo28-Apr-10 2:20 
AnswerRe: project n-tier Pin
J4amieC28-Apr-10 2:25
J4amieC28-Apr-10 2:25 
GeneralRe: project n-tier Pin
genieabdo28-Apr-10 4:00
genieabdo28-Apr-10 4:00 
AnswerRe: project n-tier Pin
Arun Jacob28-Apr-10 2:25
Arun Jacob28-Apr-10 2:25 
AnswerRe: project n-tier Pin
Abhinav S28-Apr-10 6:16
Abhinav S28-Apr-10 6:16 
QuestionVideo playback help Pin
TimSWatson28-Apr-10 2:00
TimSWatson28-Apr-10 2:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.