Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //viet chuong trinh dat 8 quan hau trong ban co sao cho 8 con khong an nhau
- #include <graphics.h>
- #include <stdio.h>
- #include <conio.h>
- #include <stdlib.h>
- #include <dos.h>
- int a[9];
- int b[16];
- int c[16];
- int save[13][9];
- int x[9];
- int count=1;
- //tam cua cac hinh tron bieu thi cho quan hau
- struct tam{
- int x;
- int y;
- };
- tam Tam[8][8];
- //luu vi tri cua cac cach dat quan hau
- void save_()
- {
- for(int i=1;i<=8;i++)
- save[count][i] = x[i];
- count++;
- }
- //giai thuat tim cach dat quan hau
- void Try(int j)
- {
- for(int i=1;i<=8;i++)
- {
- if(a[i] ==1 &&b[i+j]==1 && c[i-j]==1)
- {
- x[j] = i;
- a[i] = b[i+j] = c[i - j]=0;
- if(j<8)
- Try(j+1);
- else
- save_();
- a[i] = b[i+j] = c[i-j]= 1;
- }
- }
- }
- //ham khoi tao ban co
- void create()
- {
- for(int i=1;i<=8;i++)
- a[i] = 1;
- for (int j=2;j<=16;j++)
- b[j] = 1;
- for (int k=-7;k<=7;k++)
- c[k] = 1;
- }
- int main(void)
- {
- create();
- //tao man hinh do hoa ban co trang den
- int mh = DETECT,mode;
- initgraph(&mh,&mode,"C:\\BC5\\BGI");
- setbkcolor(BLACK);
- setcolor(RED);
- rectangle(50,50,450,450);
- setcolor(BLUE);
- for(int j=50;j<=400;j=j+100)
- {
- for(int i =50;i<=400;i=i+100)
- {
- bar(i,j,i+50,j+50);
- }
- }
- for(int m =100;m<=400;m=m+100)
- {
- for(int n =100;n<=400;n=n+100)
- {
- bar(n,m,n+50,m+50);
- }
- }
- // dat gia tri cho cac tam hinh tron
- for(int p=1;p<=8;p++)
- for(int q=1;q<=8;q++)
- {
- Tam[p][q].x = 50*p+25;
- Tam[p][q].y = 50*q+25;
- }
- //chay giai thuat
- Try(1);
- // ve hinh tron
- //for(int t=1;t<=12;t++)
- for(int u=1;u<=8;u++)
- {
- //in mot cach
- int c = Tam[u][save[1][u]].x;
- int d = Tam[u][save[1][u]].y;
- setcolor(BLUE);
- setfillstyle(SOLID_FILL, RED);
- circle(c,d,25);
- floodfill(c,d,BLUE);
- }
- outtextxy(50,470,"CHUONG TRINH DAT QUAN HAU");
- getch();
- closegraph();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement