sandipan

C code: display all solutions of 8-queen prob (BGI graphics)

Sep 6th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.66 KB | None | 0 0
  1. #include<graphics.h>
  2. #include<stdio.h>
  3. #include<conio.h>
  4.  
  5. int check(int,int,int,int);
  6. void drawqueen(int,int);
  7.  
  8. void main() {
  9.    
  10.     int i,j,k,l,m,n,p,q,c=0;
  11.     char s[50];
  12.     int gd=DETECT,gm;
  13.     initgraph(&gd,&gm,"c:\\tc\\bgi");
  14.  
  15.     for(i=0;i<8;++i) {
  16.         for(j=0;j<8;++j) {
  17.             if(!check(0,i,1,j)) continue;
  18.             else
  19.                 for(k=0;k<8;++k) {
  20.                     if(!check(0,i,2,k) || !check(1,j,2,k))continue;
  21.                     else
  22.                         for(l=0;l<8;++l) {
  23.                             if(!check(0,i,3,l) || !check(1,j,3,l) || !check(2,k,3,l))continue;
  24.                             else
  25.                                 for(m=0;m<8;++m) {
  26.                                     if(!check(0,i,4,m) || !check(1,j,4,m) || !check(2,k,4,m) || !check(3,l,4,m))continue;
  27.                                     else
  28.                                         for(n=0;n<8;++n) {
  29.                                             if(!check(0,i,5,n) || !check(1,j,5,n) || !check(2,k,5,n) || !check(3,l,5,n) || !check(4,m,5,n))continue;
  30.                                             else
  31.                                                 for(p=0;p<8;++p) {
  32.                                                     if(!check(0,i,6,p) || !check(1,j,6,p) || !check(2,k,6,p) || !check(3,l,6,p) || !check(4,m,6,p) || !check(5,n,6,p))continue;
  33.                                                     else
  34.                                                         for(q=0;q<8;++q) {
  35.                                                             if(!check(0,i,7,q) || !check(1,j,7,q) || !check(2,k,7,q) || !check(3,l,7,q) || !check(4,m,7,q) || !check(5,n,7,q) || !check(6,p,7,q))continue;
  36.                                                             if(check(0,i,7,q) && check(1,j,7,q) && check(2,k,7,q) && check(3,l,7,q) && check(4,m,7,q) && check(5,n,7,q) && check(6,p,7,q)) {
  37.                                                                 int hl,vl,kl=1;
  38.                                                                 sprintf(s,"Positions=%d %d %d %d %d %d %d %d",i,j,k,l,m,n,p,q);
  39.                                                                 outtextxy(10,10,s);
  40.                                                                 setcolor(15);
  41.                                                                 for(hl=100;hl<500;hl+=50)
  42.                                                                     for(vl=50;vl<450;vl+=50)
  43.                                                                         rectangle(hl,vl,hl+50,vl+50);
  44.                                                                 setfillstyle(SOLID_FILL,15);
  45.                                                                 for(hl=120;hl<500;hl+=100)
  46.                                                                     for(vl=70;vl<450;vl+=100)
  47.                                                                         floodfill(hl,vl,15);
  48.                                                                 for(hl=170;hl<500;hl+=100)
  49.                                                                     for(vl=120;vl<450;vl+=100)
  50.                                                                         floodfill(hl,vl,15);
  51.                                                                 setcolor(6);
  52.                                                                 setfillstyle(SOLID_FILL,6);
  53.                                                                 for(hl=120;hl<500;hl+=100)
  54.                                                                     for(vl=120;vl<450;vl+=100)
  55.                                                                         floodfill(hl,vl,15);
  56.                                                                 for(hl=170;hl<500;hl+=100)
  57.                                                                     for(vl=70;vl<450;vl+=100)
  58.                                                                         floodfill(hl,vl,15);
  59.                                                                 drawqueen(findpos(i),50);
  60.                                                                 drawqueen(findpos(j),100);
  61.                                                                 drawqueen(findpos(k),150);
  62.                                                                 drawqueen(findpos(l),200);
  63.                                                                 drawqueen(findpos(m),250);
  64.                                                                 drawqueen(findpos(n),300);
  65.                                                                 drawqueen(findpos(p),350);
  66.                                                                 drawqueen(findpos(q),400);
  67.                                                                 ++c;
  68.                                                                 getch();
  69.                                                                 getch();
  70.                                                                 cleardevice();
  71.                                                             }
  72.                                                         }
  73.                                                     }
  74.                                             }
  75.                                     }
  76.                             }
  77.                     }
  78.             }
  79.     }
  80.     getch();
  81.     closegraph();
  82.     gotoxy(getmaxx()/2,getmaxy()/2);
  83.     printf("    No of solutions=%d",c);
  84.     getch();
  85. };
  86.  
  87. int check(int h1,int v1,int h2,int v2) {
  88.    
  89.     int i,k=v1,f=1;
  90.     if(v1==v2)
  91.         f=0;
  92.     for(i=h1;i<h2;++i)
  93.         ++k;
  94.     if(v2==k)
  95.         f=0;
  96.     k=v1;
  97.     for(i=h1;i<h2;++i)
  98.         --k;
  99.     if(v2==k)
  100.         f=0;
  101.     return(f);
  102. }
  103.  
  104. void drawqueen(int x,int y) {
  105.    
  106.     setcolor(14);
  107.     arc(x,y,270,360,25);
  108.     arc(x+50,y,180,270,25);
  109.     line(x+10,y+50,x+40,y+50);
  110.     line(x,y+25,x+10,y+50);
  111.     line(x+50,y+25,x+40,y+50);
  112.     circle(x+25,y,4);
  113.     setfillstyle(SOLID_FILL,14);
  114.     floodfill(x+20,y+40,14);
  115.     setcolor(4);
  116.     circle(x+25,y,2);
  117.     line(x+5,y+40,x+45,y+40);
  118. }
  119.  
  120. int findpos(int i) {
  121.    
  122.     switch(i) {
  123.         case 0:return(100);
  124.         case 1:return(150);
  125.         case 2:return(200);
  126.         case 3:return(250);
  127.         case 4:return(300);
  128.         case 5:return(350);
  129.         case 6:return(400);
  130.         case 7:return(450);
  131.     }
  132. }
Add Comment
Please, Sign In to add comment