algorithm - Function to generate permutations of sequence with elements limited to 1 adjacent move -


Let's assume that there is a list of length n, which includes the letter A - char (n). I find those movements I want in which each letter can move only adjacently or where it remains. So in each seq, there are three "set" permutations, cycling on the left, right bicycle (the list is considered cyclical), and the original list. I'm having problems with an algorithm to handle potential swaps in the middle of the list. Primarily, how to turn off the characters can be left on the coupling alphabet. Preceded for the list: where n = 3 [ABC, CAB, BCA, ACB, CBA, BAC] In addition to this, ABC and CAB are interchangeable Regardless of the elements with relative similar positions, it is considered unique. Mainly looking for algorithms and not any specific language. The basic rule is that elements can have a maximum position of 1 position from their original position in the list.

I will briefly describe a skilled counting strategy.

Before we solve it easily, where there is no wrapped left, there are two possibilities for the left most element or it keeps right or if it moves correctly, then move that element Should be left for We can calculate the possibilities randomly for the remaining order. This code is for simple problem, algorithm for problem with wraparound # wraparound is defined in the DAR defs_line below the prose (lst, j): if lane (LST) - J. Lt; 2: print (LCT) other: perms_line (lst, j + 1) lst [j], lst [j + 1] = lst [j + 1], lst [j] perms_line (lst, j + 2) lst [j] ]], List [J + 1] = LST [J + 1], LST [J] permit_line ([1, 2, 3, 4, 5], 0)

Let's wrap around. If two consecutive elements are transferred right then each element will take the right action. If two consecutive elements are left, then each element should be left. Handle these extraordinary permutations separately.

Initially, look at the element on the left. Either it is placed, swaps its right along with the element, or is swaped with the element on the right, for each of these three possibilities (note: two if n = 2 or just one if n = 1), then use the non-rapperband calculation strategy on the other hand. I will pass to write the code, because it will take something neat to correct.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -