"Aha algorithm"-enumeration

Source: Internet
Author: User

Enumeration method, as a very basic method or skill in the programming world, it can also be called the exhaustive method, the violence law, the calendar, in-depth understanding of some algorithms, you will find it in the algorithm world is very extensive use.

Generally speaking, this method is very simple, we abstract point, for a problem of the solution X, the solution satisfies the constraints f (x), enumeration method to solve the problem is to enumerate all the possible cases of X, and then determine whether to meet the constraints F (x), if satisfied, then X is the solution of the problem;

Although the enumeration method is simple, it is often accompanied by clever optimization and screening in solving practical problems, and "finding all possible cases of X" in the enumeration calculation is also useful in some other algorithms (such as computational geometry). So the so-called violence law seems to be violent without brain, in fact there are still many places to be considered.

Let's take a look at an example: for () () () () () () () () () () (), 9 empty fill in the 1~9 the 9 numbers each time, how many kinds of conditions will be met?

Obviously we enumerate each () possible value, that is [1,9], we need to set a 9-layer loop, and then determine whether the 9 numbers are equal, and then verify that the equation is set up, but based on this idea of programming we find it too troublesome to determine whether the 9 numbers are the same, there is no more convenient encoding method?

In fact, similar to the way we introduced in the first chapter of the bucket, we use a[i] to denote the number of the first (), and then we use Book[a[i]] "to mark the number A[i], if ∑book[a[i]] = 9, it indicates that 9 numbers appear once, is to meet the constraints.

The simple reference code is as follows.

#include <cstdio>intMain () {inta[Ten], I, total =0, book[Ten], sum;  for(a[1] =1; a[1]<=9; a[1]++)          for(a[2]=1; a[2]<=9; a[2]++)            for(a[3]=1; a[3]<=9; a[3]++)              for(a[4]=1; a[4]<=9; a[4]++)                for(a[5]=1; a[5]<=9; a[5]++)                  for(a[6]=1; a[6]<=9; a[6]++)                    for(a[7]=1; a[7]<=9; a[7]++)                      for(a[8]=1; a[8]<=9; a[8]++)                        for(a[9]=1; a[9]<=1; a[9]++)      {             for(i =1; I <=9; i++) Book[i]=0;  for(i =1; I <=9; i++) Book[a[i]]=1; Sum=0;  for(i =1; I <=9; i++) Sum+=Book[a[i]]; if(sum==9&& -*a[1] +Ten*a[2] + a[3]                      + -*a[4] +Ten*a[5] + a[6]                    == -*a[7] +Ten*a[8] + a[9]) { total++; }} printf ("Total =%d", total/2);//by the addition of the law of exchange we can not be difficult to understand that this should be in addition to 2, for the results of test instructions-compliant filtering calculation is often the key to enumeration}

"Aha algorithm"-enumeration

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.