Advertisement
greatbn

Quân Hậu

Aug 29th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.40 KB | None | 0 0
  1.   //viet chuong trinh dat 8 quan hau trong ban co sao cho 8 con khong an nhau
  2. #include <graphics.h>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <stdlib.h>
  6. #include <dos.h>
  7.         int a[9];
  8.         int b[16];
  9.         int c[16];
  10.         int save[13][9];
  11.    int x[9];
  12.    int count=1;
  13. //tam cua cac hinh tron bieu thi cho quan hau
  14. struct tam{
  15.         int x;
  16.         int y;
  17. };
  18. tam Tam[8][8];
  19. //luu vi tri cua cac cach dat quan hau
  20. void save_()
  21. {
  22.  
  23.         for(int i=1;i<=8;i++)
  24.                 save[count][i] = x[i];
  25.         count++;
  26. }
  27. //giai thuat tim cach dat quan hau
  28. void Try(int j)
  29. {
  30.         for(int i=1;i<=8;i++)
  31.         {
  32.                 if(a[i] ==1 &&b[i+j]==1 && c[i-j]==1)
  33.                 {
  34.                         x[j] = i;
  35.                         a[i] = b[i+j] = c[i - j]=0;
  36.                         if(j<8)
  37.                                 Try(j+1);
  38.          else
  39.                 save_();
  40.                         a[i] = b[i+j] = c[i-j]= 1;
  41.                 }
  42.         }
  43. }
  44. //ham khoi tao ban co
  45. void create()
  46. {
  47.         for(int i=1;i<=8;i++)
  48.                 a[i] = 1;
  49.         for (int j=2;j<=16;j++)
  50.                 b[j] = 1;
  51.         for (int k=-7;k<=7;k++)
  52.                 c[k] = 1;
  53. }
  54. int main(void)
  55. {
  56.         create();
  57. //tao man hinh do hoa ban co trang den
  58.         int mh = DETECT,mode;
  59.    initgraph(&mh,&mode,"C:\\BC5\\BGI");
  60.     setbkcolor(BLACK);
  61.     setcolor(RED);
  62.    rectangle(50,50,450,450);
  63.    setcolor(BLUE);
  64.    for(int j=50;j<=400;j=j+100)
  65.    {
  66.    for(int i =50;i<=400;i=i+100)
  67.         {
  68.  
  69.           bar(i,j,i+50,j+50);
  70.  
  71.       }
  72.     }
  73.    for(int m =100;m<=400;m=m+100)
  74.    {
  75.         for(int n =100;n<=400;n=n+100)
  76.       {
  77.         bar(n,m,n+50,m+50);
  78.       }
  79.    }
  80. // dat gia tri cho cac tam hinh tron
  81.         for(int p=1;p<=8;p++)
  82.         for(int q=1;q<=8;q++)
  83.         {
  84.                 Tam[p][q].x = 50*p+25;
  85.                 Tam[p][q].y = 50*q+25;
  86.  
  87.         }
  88. //chay giai thuat
  89.    Try(1);
  90. // ve hinh tron
  91.     //for(int t=1;t<=12;t++)
  92.         for(int u=1;u<=8;u++)
  93.          {
  94.  
  95. //in mot cach
  96.                     int c = Tam[u][save[1][u]].x;
  97.              int d = Tam[u][save[1][u]].y;
  98.                 setcolor(BLUE);
  99.                 setfillstyle(SOLID_FILL, RED);
  100.  
  101.              circle(c,d,25);
  102.                 floodfill(c,d,BLUE);
  103.  
  104.         }
  105.    outtextxy(50,470,"CHUONG TRINH DAT QUAN HAU");
  106.    getch();
  107.    closegraph();
  108.    return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement