C # achieve full depth-first traversal arrangement

If you are asked to 123 full array of three numbers you can say it 123,132,213,231,312,321 soon, but let's say you full array of 1 to 20 a total of 20 numbers is not not so simple it? This article, we will use a depth-first algorithm to achieve full array C #

Legend algorithm

If there are three cards numbered 1, 2 and three boxes numbered 1, 2, will now need three cards, respectively, into three boxes.
Here Insert Picture Description
We put this problem into a fundamental question: how to put the cards in a small box. Each small box could put 1,2,3 playing cards, which need to try out, we need a for loop to solve.

   for (int i = 1; i < = n; i++)
   {
       a[step] =i; //将i号扑克牌放入第step个盒子中
   }

A digital recording which card is used to put each box, step indicates that the current is the first several boxes, a [step] = i is the i-th step of playing cards into a box. At this point there is a problem is that a card has been placed in a box in the first step you can not move to another box, and so also need an array book to mark what cards have been used.

            for (int i = 1; i < = n; i++)
            {
                if(book[i]==0)  //book[i]==0表示第i号牌仍然在手中
                {
                    a[step] =i; //将i号扑克牌放入第step个盒子中
                    book[i] =1; //设为1,表示第i号牌已经不在手中
                }
                
            }

Now the first step in a box of cards have been put away, you need to look in a box step + 1 put any cards, processing methods and on a box is the same, where we can put the package into a step-by-step function dfs, next to continue to call, as follows:

void dfs(int step) { 
            for (int i = 1; i < = n; i++)
            {
                if(book[i]==0)  //book[i]==0表示第i号牌仍然在手中
                {
                    a[step] =i; //将i号扑克牌放入第step个盒子中
                    book[i] =1; //设为1,表示第i号牌已经不在手中
                }
                
            }
        }

Then we can call the dfs function directly in dealing with step + 1, as follows :( Note that the code marked emphasis)

        void dfs(int step) { 
            for (int i = 1; i < = n; i++)
            {
                if(book[i]==0)  //book[i]==0表示第i号牌仍然在手中
                {
                    a[step] =i; //将i号扑克牌放入第step个盒子中
                    book[i] =1; //设为1,表示第i号牌已经不在手中
                    dfs(step+1);//(重点)处理下一步放什么牌
                    book[i] =0; //(重点)在尝试完一轮之后,需要将牌都收回才能进行下一轮的尝试
                }
                
            }
        }

The above code book [i] = 0; very important role in this sentence is to recover a box of playing cards, because in the first place to complete the end, if you do not put the box of cards will not be able to recover once placed under put. One remaining question is when to output a sequence required to meet it? In fact, when we deal with boxes of the n + 1 (i.e., step = n + 1) n represents a box has been put in front of, and at this time a playing card sequence in the array is the result satisfies the condition, the array will be a the element can be printed in the order. note! Printing the need to perform return, otherwise the program will continue down the implementation.

        void dfs(int step) { 
            if (step==n+1)  //如果到了第n+1个盒子,说明前面n个盒子都已经放好
	        {
		        //输出一种结果
                for (int i = 0; i < a.length; i++)
			    {
			        Console.Write(a[i]);
			    }
                return; //返回到最近一次调用dfs函数的地方
	        }
            for (int i = 1; i < = n; i++)
            {
                if(book[i]==0)  //book[i]==0表示第i号牌仍然在手中
                {
                    a[step] =i; //将i号扑克牌放入第step个盒子中
                    book[i] =1; //设为1,表示第i号牌已经不在手中
                    dfs(step+1);//处理下一步放什么牌
                    book[i] =0; //在尝试完一轮之后,需要将牌都收回才能进行下一轮的尝试
                }
                
            }
        }

Well, that's full of ideas, let's look at the complete code and call the procedure:

    class Program
    {
        static int n = 0;
        static int[] a;
        static int[] book;
        static void Main(string[] args)
        {
            Console.WriteLine("请输入数字N:");
            n = int.Parse(Console.ReadLine());
            Console.WriteLine("数字1~"+n+"的全排列为:");
            a = new int[n+1];
            book = new int[n+1];
            dfs(1);
            Console.ReadLine();
        }
        static void dfs(int step)
        {
            if (step == n + 1)  //如果到了第n+1个盒子,说明前面n个盒子都已经放好
            {
                //输出一种结果
                for (int i = 1; i < a.Length; i++)
                {
                    Console.Write(a[i]);
                }
                Console.WriteLine();
                return; //返回到最近一次调用dfs函数的地方
            }
            for (int i = 1; i <= n; i++)
            {
                if (book[i] == 0)  //book[i]==0表示第i号牌仍然在手中
                {
                    a[step] = i; //将i号扑克牌放入第step个盒子中
                    book[i] = 1; //设为1,表示第i号牌已经不在手中
                    dfs(step + 1);//处理下一步放什么牌
                    book[i] = 0; //在尝试完一轮之后,需要将牌都收回才能进行下一轮的尝试
                }

            }
        }
    }

Look at the results:
Full array
In this part this is over, welcome attention to my blog algorithm Series:
C # algorithm https://blog.csdn.net/leaderxin/article/category/9434286

Published 12 original articles · won praise 27 · views 20000 +

Guess you like

Origin blog.csdn.net/Leaderxin/article/details/102818042