Moderators: phlip, Larson, Moderators General, Prelates
Kirby wrote:The easiest thing that comes to mind is to pre-compute the solution path from the sorted state to each unsorted state. Then look up the input unsorted state, and the corresponding path to the sorted state. There are n! such states. is that fast enough?
Foamy wrote:Thank you very much for the response but what does the Y move exactly do? What rearrangement does it make to three balls instead of the three we're focusing on that moment? Can't figure it out from the example...
Foamy wrote:OK.. So from what I see the algorithm should look like this:
Look for a triple containing 1 by using specific X's. When found, move it to the left using Y's until row[0]=1.
Set a "safe zone" on row[0] meaning the algorithm can't include 1 in any of its searches since it's already set properly.
Repeat until all is sorted or it's proven that it can't be.
Is it OK or I didn't get sth?
134562
^^^
As safe zone is set to "protect" row[0], we make our see[0]=row[1], see[1]=row[2], see[2]=row[3]. Check if see[0], [1], [2]==0. Here, however, it's not so let's move:
134562
^^^
To move, we must have made see[0]=row[2], see[1]=row[3], see[1]=row[4]
And so on... 134562
^
1 is in the safe zone so we start from 3.
134562
^
134562
^
and so on till:
134562
^
Then, we check that we're on the index 5 so we moved t times and therefore made some X's. Then, we can highliht two previous numbers:
134562
^^^
And go like you did.Foamy wrote:jaap - Hmm.. I came across a problem. How should I make my algorithm "see" a triplet? I mean: saving three elements to some array and then (if the next number we're looking for, say 2) if 2's not there, changing all these variables to the next triplet is extremely time consuming.
// array = array containing the current order of the balls.
mypos = 0; // array index of first of 3 balls in front of us.
for( ballnumber = 0 to array.length-2 )
solveBall(ballnumber)
//todo swap last two balls if necessary and possible
function solveBall( ballnumber ){
ballpos = array.indexOf(ballnumber) // find ball in array
while( ballpos > ballnumber ){ // ball is not yet solved
// move so that ball is the middle one in front of us
printmove( mypos-(ballpos-1), "X")
mypos = ballpos-1
// do 2Y to move ball left one step
array[mypos+1] = array[mypos+2]
array[mypos+2] = array[mypos]
array[mypos] = ballnumber
ballpos = ballpos - 1
printmove( 1, "Y")
}
}jaap wrote:I really don't understand why you would ever want to do it that way.
Foamy wrote:jaap wrote:I really don't understand why you would ever want to do it that way.
Honestly - neither do I but couldn't think of a better algorithm.
In your code you stated that we should move so that the ball we're looking for is in the middle of our sight. But isn't looking for it to be in the third place better? Then, we move it to the beggining by just one Y.
Foamy wrote:Yakk - I don't think merge sort is a good idea here. How do we know how many Ys we should do if we simply merge sort a triple?
jaap wrote:The problem doesn't require you to use a short way to solve it
jaap wrote:I couldn't be bothered to describe code that does that because it would be twice as long
jaap wrote:(a loop with steps of 2, followed by almost the same code for a possible final step of 1).
jaap wrote:Yakk mentioned bubble sort, not merge sort which is something entirely different and inapplicable to this situation.
Foamy wrote:jaap wrote:The problem doesn't require you to use a short way to solve it
And what about time? I mean: would this looking for the ball on the third place instead of the middle make the code faster or just the results shorter?
Foamy wrote:jaap wrote:(a loop with steps of 2, followed by almost the same code for a possible final step of 1).
Hmm.. I thought of another way of implementing it. Namely: we look for our ball, then we check what (its_index-safe_zone)%3 would give us and, depending on the result, we know which position in the triple it holds.
Yakk wrote:The parity problem is interesting. The fix (rotating an element from front to back) is also interesting.
I wonder if there is an easy way to detect the parity problem? And efficiently change the parity of the proggy.
Goplat wrote:Swapping any two elements inverts the parity, as I understand it, so it should be possible to
- scan through the permutation and build a table that gives the position of each item (this is just an "indexof[permutation[i]] = i" loop)
- do a selection-sort, using the table so each element can be found in constant time rather than the usual linear time
- return whether it took an even or odd number of swaps to sort
Foamy wrote:Erm.. But what parity are you talking about and how it should help me solve the problem?
Foamy wrote:BTW: Was the algorithm I wrote above bad?
Foamy wrote:Hmm.. but would calculating the parity in the beggining make the algorithm noticeably slower? It's sure it'd make the algorithm faster for the -1 cases but what about the "normal" ones?
Users browsing this forum: No registered users and 2 guests