N Queen's question

Source: Internet
Author: User

Because there are only 10 sets of data, to prevent a data occurrence multiple times resulting in timeouts so hit the table

It's quite simple ....

1 /* 2 is to consider where the Queen is placed, and for each row we need to enumerate each place where we can place the Queen,3 It is also necessary to determine whether the current position (line i) satisfies the condition, that is, to determine whether this position conflicts with the position of the Queen placed in the former i-1 line,4 If there is a conflict, the location is inappropriate; otherwise, you can enumerate the position of the next row of Queens until the nth row. 5 */6#include <stdio.h>7#include <string.h>8 9 intn,tmp;Ten intmap[ One]; One  A voidDFS (intk) - { -     intI,j,flag; the     if(k==n+1) -     { -tmp++; -         return; +     } -     Else +     { A          for(i=1; i<=n;++i) at         { -map[k]=i; -flag=1; -              for(j=1; j<k;++j) -             { -                 if(map[j]==i| | i-k==map[j]-j| | I+K==MAP[J]+J)//Note: 1, i=map[k] 2, not the same slash two point meaning is the line marked to the diagonal distance is not equal in                 { -flag=0; to                      Break; +                 } -             } the             if(flag) *DFS (k +1); $         }Panax Notoginseng     } - } the  + intMain () A { the     inti,m; +     intans[ One]; -      for(n=1; n<=Ten;++N) $     { $tmp=0; -DFS (1); -ans[n]=tmp; the     } -      while(SCANF ("%d",&m), m)Wuyi     { theprintf"%d\n", Ans[m]); -     } Wu     return 0; -}

The key is the record and expression of the used rows and columns

N Queen's question

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.