Page 2 of 4 FirstFirst 1234 LastLast
Results 11 to 20 of 36

Thread: Family Crossing a BRIDGE

  1. #11
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    I will try to meet that limit, so far I am at 415, seen as how I just want it to work first.
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  2. #12
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    I know I will never beat you guys in LOC, so I bulk up my code with features MS Tactics 101.

    I added variables for
    - max pulley capacity
    - min people needed to return the pully

    Did not test them and I doubt that they will work.

    I also added calculating the shortest time.

    LOC now at 410

    Over the next few days I will try to optimise the code. It also runs slowly when finding the shortest time. I know that there is serious work to be done to achieve this. And it will take me a lot time to math the optimisation.

    Have not slept yet
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  3. #13
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Code:
    #include <list>
    #include <stdlib.h>
    
    using namespace std;
    
    
    
    struct	FamilyMember
    {
    	int		Name;	//Represents an enumerated name eg 1 can represent "Mother"
    	int		Time;
    };
    
    struct	TripRecord
    {
    	int	*	Names;
    	int		NNames;
    };
    
    int GetSelectionCount(int SetSize, int SelectionSize)
    {
    	//5C2 = 5*4 /2 where order is irrelevant 5!/3!2!
    	int		Count	=	1;
    	int		Count2	=	1;
    	for(int i = 0; i < SelectionSize; i++)
    	{
    		Count	*=	(SetSize - i);	//5,4
    		Count2	*=	(i + 1);		//1,2
    	}
    	return	Count/Count2;
    }
    
    
    void GotoNextSelectionState(int SetSize, int SelSize, int ElementsSelected[])
    {
    	/*
    	That is
    	0,1
    	0,2
    	0,3
    	0,4
    	1,2
    
    	1,3
    	1,4
    	2,3
        2,4
    	3,4
    	*/
    
    	//increase the last number until it reaches the max - 1 then icrease the previous number by 1
    	//and assign the latter to be 1 greater. Repeat upwards. that last state is 4,5,6 in 7C3
    
    	/*
    	//eg 7C3
    	0,1,2
    	...6
    	0,1,6
    	0,2,3
    	...5
    	0,2,6
    	0,3,4
    	...4
    	0,3,6
    	*/
    
    
    	for(int i = 1; i <= SelSize; i++)
    	{
    		if(ElementsSelected[SelSize - i] < (SetSize - i))
    		{
    			ElementsSelected[SelSize - i]++;
    		}
    		else
    		{
    			for(int c = SelSize - i + 1; c < SelSize; c++)
    			{
    				ElementsSelected[c]	=	ElementsSelected[c - 1] + 1;
    			}
    		}
    	}
    
    }
    
    
    
    
    
    class Paths
    {
    public:
    	int *	(*s);			//Array of (Indices of the Elements Selected)
    	int *	sSelSize;
    	int *	sSetSize;
    	int *	p;
    	int *	pmax;
    	int		l;
    	Paths(int FamilySize, int PulleyLimit, int PulleyReturn = 1)
    	{
    		std::list<int>				TripSelection;				//A list of the maximun branch that can be tried
    		std::list<int>::iterator	itTripSelection, itSelSize, itSetSize;
    
    		std::list<int>				SelectionParameterSelectionSize;		//<edit>This suppose to be not neccessary
    		std::list<int>				SelectionParameterSetSize;			//<edit>This suppose to be not neccessary
    
    		int		right			=	FamilySize;
    		//int		trip			=	0;//forward_trips	=	0;
    		//int		return__trips	=	0;
    		while(true)
    		{
    			if(right <= PulleyLimit)//if this is the last trip, considering that the pulley can have empty seats now
    			{
    				TripSelection.push_back(1);	//There is only one possible path
    				SelectionParameterSelectionSize.push_back(right);
    				SelectionParameterSetSize.push_back(right);
    			}
    			else
    			{
    				TripSelection.push_back(GetSelectionCount(right, PulleyLimit));
    				SelectionParameterSelectionSize.push_back(PulleyLimit);
    				SelectionParameterSetSize.push_back(right);
    			}
    			right	-=	PulleyLimit;		//<edit>optimise
    			//trip++;
    			if(right <= 0)
    				break;
    
    			TripSelection.push_back(GetSelectionCount(FamilySize - right, PulleyReturn));
    			SelectionParameterSelectionSize.push_back(PulleyReturn);
    			SelectionParameterSetSize.push_back(FamilySize - right);
    
    			right	+=	PulleyReturn;
    			//trip++;
    		}
    
    		//load array of counters
    		l		=	TripSelection.size();		//l		=	trip;
    		//l		=	2 * (FamilySize - PulleyLimit) - 1;//<edit> this has not be check with varying pulley size
    		pmax	=	new int[l];
    		p		=	new int[l];
    
    		s		=	new int *[l];
    		sSelSize=	new int[l];
    		sSetSize=	new int[l];
    
    		itTripSelection		=	TripSelection.begin();
    		itSelSize			=	SelectionParameterSelectionSize.begin();
    		itSetSize			=	SelectionParameterSetSize.begin();
    		for(int i = 0; itTripSelection != TripSelection.end(); itTripSelection++, i++, itSelSize++, itSetSize++)//int i = 0; i < l; i++)
    		{
                pmax[i]	=	(*itTripSelection);//l - i;
    			p[i]	=	0;
    
    			sSelSize[i]	=	(*itSelSize);
    			sSetSize[i]	=	(*itSetSize);
    			s[i]		=	new int[sSelSize[i]];
    			for(int c = 0; c < sSelSize[i]; c++)
    			{
    				s[i][c]	=	c;						//0,1
    			}
    
    		}
    		//pmax[l - 1]	=	1;		//For the last trip the is just one possible path???????????
    	};
    	~Paths()
    	{
    		delete []	p;
    		delete []	pmax;
    		//<edit>
    	}
    
    	bool GotoNextPath()			//return true if Path is all zero
    	{
    		int	l2	=	l - 1;		//ignore the last array member because it represents the last trip <edit> there is a cleaner way to do this</edit>
    		for(int i = 0; i < l2; i++)	//<edit> Make this include the last member ie use proper logic
    		{
    			if(p[i] == pmax[i])
    			{
    				p[i]	=	0;
    
    				//<edit>optimize
    				for(int c = 0; c < sSelSize[i]; c++)
    				{
    					s[i][c]	=	c;						//0,1
    				}
    
    				continue;
    			}
    			else
    			{
    				p[i]++;
    
    				//<edit>need optimization
    				//Change which members is selected
    				GotoNextSelectionState(sSetSize[i], sSelSize[i], s[i]);
    
    				break;
    			}
    		}
    		return	is_zero();		//<edit> only needed if there is no soultion
    	};
    	bool is_zero()				//<edit> only needed if there is no soultion
    	{
    		for(int i = 0; i < l; i++)
    		{
    			if(p[i] != 0)
    				return	false;
    		}
    		return	true;
    	}
    };
    
    
    
    
    inline int MakeTrip(
    					list<FamilyMember> & MembersBoarding,
    					list<FamilyMember> & MembersAcross,
    					int IndicesMembersBoarding[],			/*Indices of the boarding members in the MembersRight list*/
    					int MembersBoardingCount,					/*Number of members boarding*/
    					list<TripRecord> & RecordOfCurrentPath
    					)
    {
    	int		MaxTime		=	0xFFFFFFFF;
    
    	//Calculate the time by the max time of all the Boarding members
    
    	std::list<FamilyMember>::iterator	i;
    
    	std::list<FamilyMember>::iterator *		iMembersTranfer		=	new std::list<FamilyMember>::iterator[MembersBoardingCount];
    	int										iMTCount			=	0;
    
    	//<edit> it wouldbe better as a table with name as the primary key
    
    	for(int c = 0; c < MembersBoardingCount; c++)
    	{
    		int		CurrentIndex	=	IndicesMembersBoarding[c];
    		int		c2				=	0;
    
    		for(i = MembersBoarding.begin(); i != MembersBoarding.end(); i++, c2++)
    		{
    			if(c2 == CurrentIndex)
    			{
    				MaxTime	=	max((*i).Time, MaxTime);
    				iMembersTranfer[iMTCount]	=	i;
    				iMTCount++;
    				break;
    			}
    		}
    	}
    
    	////////////
    	TripRecord	trNewRecord;
    	trNewRecord.Names		=	new int [MembersBoardingCount];//<edit> this is not a good move unless I use a list
    	trNewRecord.NNames		=	0;
    
    	//Do the Transfer
    	for(c = 0; c < iMTCount; c++)
    	{
    		trNewRecord.Names[trNewRecord.NNames]	=	(*(iMembersTranfer[c])).Name;
    		trNewRecord.NNames++;
    
    		MembersAcross.push_back(*(iMembersTranfer[c]));
    		MembersBoarding.erase(iMembersTranfer[c]);
    	}
    
    	// and record it
    	RecordOfCurrentPath.push_back(trNewRecord);
    
    
    	return	MaxTime;//(max(MemberTime, MinTime) + min(MemberTime, MinTime));
    }
    
    
    
    
    
    
    int main()
    {
    /*
    Send any two of the right members across
    Store the time
    Send any one of the left members basck across
    Store the time
    Repeat until done and calulate the total time
    See if it is less than the time limit
    
    Try the next possible path
    (There are Select(2 from n)
    */
    
    	/*
    	Generic Information
    	*/
    	const int	Family[]	=	{12, 8, 6, 3, 1};
    	const int	FamilySize	=	sizeof(Family)/sizeof(int);
    	/*const*/int	TimeLimit	=	30;
    	const int	PulleyLimit	=	2;			//<edit> heve not uaed this variable yet
    
    	const int	PulleyReturn	=	1;			//one person is needed to pull back the pulley
    
    	char	FamilyNames[][64]	=	{	"Grandpa",
    										"Father",
    										"Mother",
    										"Daugther",
    										"Son"	};
    	/*
    	Variables
    	*/
    	int								TotalTime	=	0;
    	list<FamilyMember>				MembersRight;
    	list<FamilyMember>				MembersLeft;
    	//int							TimesOfMembersLeftToCross;
    	list<FamilyMember>::iterator	itMember;
        //int						PathsTaken[FamilySize];
    	//int						NBranches	=	FamilySize;
    
    
    	//Loop through all the possible paths. Eg of a path is Mother Sent --> Then Brother Sent --> Then Sister Sent --> ...
    	Paths		pathCurrent(FamilySize, PulleyLimit);		//Minus 1 because there is no trip back for the last person
    	list<TripRecord>	RecordOfCurrentPath;
    	list<TripRecord>::iterator	itRecordOfCurrentPath;
    
    	list<TripRecord>	RecordOfPathWithLowestTime;
    	while(true)
    	{
    		//Initailize Members to Cross
    		MembersRight.clear();		//<edit> It may already be cleared
    		for(int i = 0; i < FamilySize; i++)
    		{
    			FamilyMember	MemberToAdd;
    			MemberToAdd.Name	=	i;
    			MemberToAdd.Time	=	Family[i];
    			MembersRight.push_back(MemberToAdd);
    		}
    		MembersLeft.clear();
    		RecordOfCurrentPath.clear();
    
    		//For the current Path Calulate the Total time
    		TotalTime	=	0;
    		i = 0;
    		while( true)
    		//for(int i = 0; i < pathCurrent.l;)
    		{
    			//<edit I think the max exception is taken care of in the timing function
    
    			TotalTime	+=	MakeTrip(MembersRight, MembersLeft, pathCurrent.s[i]/*MembersRightIndices*/, min(int(MembersRight.size()), PulleyLimit), RecordOfCurrentPath);//(pathCurrent.p[i], TimesOfMembersLeftToCross);
    			i++;
    			if(i == pathCurrent.l)
    				break;
    
    			TotalTime	+=	MakeTrip(MembersLeft, MembersRight, pathCurrent.s[i]/*MembersRightIndices*/, PulleyReturn, RecordOfCurrentPath);//(pathCurrent.p[i], TimesOfMembersLeftToCross);
    			i++;
    			if(i == pathCurrent.l)
    				break;
    
    		}
    
    		/*
    		if(TotalTime <= TimeLimit)
    			goto	SOLUTION_FOUND;
    		if(pathCurrent.GotoNextPath())				//If all possible paths are tested
    			goto	SOLUTION_NOTFOUND;
    		*/
    
    		//Only use this line when getting the shortest time
    		if(TotalTime <= TimeLimit)
    		{
    			TimeLimit	=	TotalTime;
    			RecordOfPathWithLowestTime.swap(RecordOfCurrentPath);
    		}
    		if(pathCurrent.GotoNextPath())				//If all possible paths are tested
    		{
    			break;
    		}
    
    	}
    
    SOLUTION_FOUND:
    	//Diplay stuff
    
    	//Only use this line when getting the shortest time
    	RecordOfCurrentPath.swap(RecordOfPathWithLowestTime);
    
    	printf("Total time taken was %d\n", TotalTime);
    	for(int i = 0; i < pathCurrent.l; i++)
    	{
    		printf("%d\n", pathCurrent.p[i]);
    	}
    
    	for(itRecordOfCurrentPath = RecordOfCurrentPath.begin(); itRecordOfCurrentPath != RecordOfCurrentPath.end(); itRecordOfCurrentPath++)
    	{
    		printf("Move ");
    		for(int i = 0; i < (*itRecordOfCurrentPath).NNames; i++)
    		{
    			printf("%s (%d) ", FamilyNames[(*itRecordOfCurrentPath).Names[i]], Family[(*itRecordOfCurrentPath).Names[i]]);
    		}
    		printf("\n");
    	}
    
    SOLUTION_NOTFOUND:
    	return 0;
    }
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  4. #14
    Join Date
    Mar 2004
    Posts
    123
    Rep Power
    0

    Default

    Quote Originally Posted by Mr. Unstoppable
    You solved it for everyone, that is not cool.
    My bad. I deleted it so those who didn't see it won't have a problem. Still wont have the time to code the solution but I'll give it a try once I'm free
    There is no wrong or right, only consequencies to action

    "It is the duty of man to make his knowledge so complete in life, so as to make it impossible for any other to take advantage of him" - Marcus Garvey

  5. #15
    Join Date
    Jul 2005
    Posts
    75
    Rep Power
    0

    Default

    Well no stress i think it going to be the toughest one to code so maybe they need a solution then they can go forward.

    The problem is to get a generic algorithm. I have a algorithm that will kind of cheat the process but it will be minimal and still can be made optimal to about the 100LOC if not less. Just have to solve one more issue and the code finish.
    "A computer once beat me at chess, but it was no match for me at kick boxing." - Emo Philips -

  6. #16
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    mi nuh rate dat, TJRAK whe yu move di ting fah. now mi cyann even get a start.
    anyway still mi have a little ting, mi soon start code it. as usual mi go complex first den si di pattern and optimise. Dun know A* Search again.
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  7. #17
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    I am going to post a problem. It is not my problem so I will link it. Some of you here may have seen the problem already, if not then good.

    I am cleaning up the code on the pulley problem, so I will done that first, then find the link, and bust it.

    By the way, the pulley problem was simply enough to code once its was analysed. Hmmmm thanks for the example TJRAKS. The challenge for me still is to optimise. I will be attempting that now, but first I will try to strip it down and simplify it.
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  8. #18
    Join Date
    Jul 2005
    Posts
    75
    Rep Power
    0

    Default

    Any problem is simple enuff to code once properly analysed. The thing is the LOC required to generate a solution. I think this problem really requires too much LOC at first sight, however as i look closer i see promising prospects.
    "A computer once beat me at chess, but it was no match for me at kick boxing." - Emo Philips -

  9. #19
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    OK I parked my old code for now. Starting something mad-sick and hairy.

    What I wonder is how beneficial is shorter code, outside of this competition.

    My view is that is does not always run faster, takes longer to find, and you are more likely to modify the longer version when upgrading.

    My head will only allow me to write code that fits the problem domain. So I could code shorter code here. But outside of this competition, my style will adhere to running speed and/or memory usage and/or development time and/or modify-ability. Which ever one is dominant.

    No offence to the competition, I just dont see the purpose. If you can prove to me that shorter code is faster then I would see the purpose.
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  10. #20
    Join Date
    Jul 2005
    Posts
    75
    Rep Power
    0

    Default

    Well i agree with you crosswire, LOC does'nt necessarily translate to good programming practice. It might not even be more useful outside this forum.

    It all depends though as LOC could translate into efficiency and Icymint3 proved that in the looping challenge. Readability and upgradability mostly depend on documentation.

    My point is LOC can fit into the scheme of things, it teaches you innovative ways to approach a problem and help you to think outside the box.

    Not to worry as even though i set the LOC limits i still make sure my code is traceable and put in comments even though they consume LOC and make it bigger.
    "A computer once beat me at chess, but it was no match for me at kick boxing." - Emo Philips -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •