Classical backtracking algorithm--eight problems

Source: Internet
Author: User

/************************************************************************//*  after eight questions                                                                  *//************************************************************************/#include   <stdio.h>int count = 0;//judge whether the current position can put Queen Int iscorrect (Int i, int j,  int  (*Q) [8]) {int s,t;//determines if Queen for (s=i,t=0;t<8;t++) if  can be placed on a line (q[s][t]==1 && &NBSP;T!=J) return 0;//Determine if a column can put Queen for (t=j,s=0;s<8;s++) if (q[s][t]==1 && s!=i) return  0;//judge if the left can put Queen for (S=i-1,t=j-1;s>=0&&t>=0;s--, t--) if (q[s][t]==1) return 0;// Judge if the right can put queen for (s=i+1,t=j+1;s<8&&t<8;s++,t++) if (q[s][t]==1) return 0;//determine if the Queen for (S=i-1,t=j+1;s>=0&&t<8;s--, t++) can be placed on the right (q[s][t]==1) return 0;//determine if the lower left can put Queen for (s=i+1,t=j-1;s<8&&t>=0;s++,t--) if (q[s][t]==1) return  0;//other conditions return 1;} Place Queen Void queen (int j, int  (*Q) [8]) {int i,k;if (j==8)//If all 8 queens are completely placed {for (i=0;i<8;i+ +) {for (k=0;k<8;k++) printf ("%d ", Q[i][k]);p rintf ("\ n");} printf ("\ n"); Count++;return;} for (i=0;i<8;i++) if (Iscorrect (i,j,q)) {q[i][j]=1; Queen (J+1,Q); q[i][j]=0;}} Int main () {int q[8][8];int i,j;for (i=0;i<8;i++) for (j=0;j<8;j++) q[i][j]=0; Queen (0,q);p rintf ("%d\n", count); return 0;}


Classical backtracking algorithm--eight problems

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.