The garlic is close to the height of the guests

Source: Internet
Author: User

Matrix67 It seems that people who are close to each other seem to be more fit. Matrix67 held a party with N (1< =n< =10) individual participation,Matrix67 need to arrange them on the table. the Matrix67 arrangement principle is that the difference in height of any two adjacent persons on the table cannot exceed K. Please tell Matrix67 How many kinds of arrangements he has in common.

Input Format :

enter two spaces separated by a number in the first line N and the K , where 1< =n< =10 , 1< =k< =1 . The second line to line n+1 Enter a person's height value per line. The height of all people is not more than 1 . positive integers

Output format:

Total number of outputs that meet the requirements

Sample Example 1

Input:

4 10

2

16

6

10

Output:

2

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define N 11
  4. #define ABS (a) ((a) >=0? ( A):(-(a)))
  5. int A[n];
  6. int Vis[n]; //Flag array Indicates whether a person has joined the round table
  7. int res=0,k,n;
  8. void dfs (int cur,int use) {
  9. ???? if (use==n) {
  10. ???????? if (ABS (a[cur]-a[0]) <=k)
  11. ???????????? res++; //Record results, when the test instructions ring sequence appears, result +1
  12. ???????? return ;
  13. ????}
  14. ???? int i;
  15. ???? Vis[cur]=1;
  16. ???? for (i=0;i<n;i++) {
  17. ???????? if (Vis[i]==0&&abs (A[cur]-a[i]) <=k) {//choose to join with the current person's high difference in the non-join group
  18. ???????????? DFS (I,USE+1);
  19. ???????????? vis[i]=0;
  20. ????????}
  21. ????}
  22. }
  23. int Main () {
  24. ???? int i;
  25. ???? scanf ("%d%d", &n,&k);
  26. ???? for (i=0;i<n;i++) scanf ("%d", &a[i]);
  27. ???? memset (vis,0,sizeof(VIS));
  28. ???? DFS (0,1);
  29. ???? printf ("%d", res);
  30. }

The enumeration of Dfs methods, or search, is similar to the algorithm of the full permutation generation, which can be done in this way for the question of selecting a qualifying arrangement from a collection, if we use an array to record, we can even print out the order in the dictionary order in the res++ place, but this question is not required, So a lot of simple, but this is a special point is the ring arrangement, a start without the brain, directly to the DFS, and then found that is the ring (round table), it was frightened, thought to record the results of what, the solution of the problem is factorial this monster, feel simply not realistic, calm down to think, as long as the opening fixed, There is no repeating ring, because the start is fixed, the problem becomes a non-circular permutation problem, but the value (height) of the first and the first is required, so that the beginning of an element, the possible solution. Then there is a special point is that a ring formed by an element, can always be formed by the other elements to the beginning of the ring rotation to get, or any one of the rings we can put any element when the beginning, so the solution of the problem is to choose a number to do the beginning (here to facilitate our a[0] to do the beginning, In fact, you can choose 0~n-1 elements to do the beginning, the result is also correct, and then a reasonable enumeration (here we use the Dfs method, with the problem set conditions to limit), each get a reasonable length of N, will be res+1 until the enumeration, the result is obtained. Finally, if the a array is ordered in dictionary order, we can also print out the dictionary order scheme.

Summary: The use of Dfs used to recursion, which makes its code implementation is often very concise, but because of the recursive inherent burst stack vulnerability, generally can not deal with big data, DFS may at present I mastered a better piece of, recently done a few questions are quite relaxed, also may be just the topic of water, Yesterday want to understand the next KMP algorithm related knowledge, find a problem, read the next article, the result is long and obscure, see the heart tired, want to relax, the result brush an afternoon of know ... , evening want to accompany the mother to watch TV, and then go in to continue to see, the results of the TV drama unexpectedly good-looking, and finally yesterday did not dry, even the words are not back--__--!!, this morning was called to the big cleaning mother, finally had the opportunity to walk back, want to continue to see the KMP, the result, finally gave up Or down-to-earth good, silently brush up a large number of nights, before going to sleep today should write a report on the problem, the problem turned out to write, in fact, the problem so water, I am embarrassed to write, my main purpose is here to do the title summary

(summed up your sister Ah, more than half are not related to the topic, is completely your personal journal good! , I think in the future should focus on their weaknesses, such as my large number has been a template, Dfs is very familiar with, but BFS has not done a problem, a number of topics and game topics automatically ignored, always do their own problems are not, people to abuse themselves is right, and he also entered the bottleneck period, Have to pick up the petition, said C++prime these two days have not turned over, good worry about whether they can be finished before the end of winter vacation. In short, try to squeeze the time.

The garlic is close to the height of the guests

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.