em code bài 8 hậu, mà chưa ra được hướng, mấy anh coi, rùi hướng dẫn giúp em nha cảm ơn nhiều
code:
GIÚP EM VỚI NHA !!!C Code:
# include <graphics.h> # include <conio.h> # include <stdio.h> # include <stdlib.h> # include <math.h> void khoitao(int a[][8]) { int i,j; for(i=0;i<8;i++) for(j=0;j<8;j++) a[i][j]=0; } void dathau(int a[][8],int x,int y) { int i,j,i1,j1,f1,f2; f1=f2=0; for(j=0;j<8;j++) a[x][j]=1; for (i=0;i<8;i++) a[i][y]=1; i=i1=x; j=j1=y; while (f1!=1 || f2!=1) { if(i>0 && j>0) a[--i][--j]=1; else f1=1; if (i1<8 && j1<8) a[++i1][++j1]=1; else f2=1; } i=i1=x; j=j1=y; f1=f2=0; while (f1!=1 || f2!=1) { if(i>0 && j<8) a[--i][++j]=1; else f1=1; if (i1<8 && j1>0) a[++i1][--j1]=1; else f2=1; } a[x][y]=2; } void gohau(int a[][8],int x) { int i,j,i1,j1,f1,f2,y; for (j=0;j<8;j++) if(a[x][j]==2) y=j; f1=f2=0; for(j=0;j<8;j++) { if (a[x][j]==-1) j=j+1; a[x][j]=0; } for (i=0;i<8;i++) { if (a[i][y]==-1) i=i+1; a[i][y]=0; } i=i1=x; j=j1=y; while (f1!=1 || f2!=1) { if(i>0 && j>0) { if (a[i][j]==-1) {i--;j--;} a[--i][--j]=0; } else f1=1; if (i1<8 && j<8) { if (a[i][j]==-1) {i1++;j1++;} a[++i1][++j1]=0; } else f2=1; } i=i1=x; j=j1=y; f1=f2=0; while (f1!=1 || f2!=1) { if(i>0 && j<8) { if (a[i][j]==-1) {i--;j++;} a[--i][++j]=0; } else f1=1; if (i1<8 && j>0) { if (a[i][j]==-1) {i1++;j1--;} a[++i1][--j1]=0; } else f2=1; } a[x][y]=-1; for(i=0;i<8;i++) for(j=0;j<8;j++) if(a[i][j]==2) dathau(a,i,j); } void xuat(int a[][8]) { int i,j; for(i=0;i<8;i++) { for(j=0;j<8;j++) } } int sapxephau(int a[8][8],int hau,int i) { int j,kt; kt=0; xuat(a); if (hau==8) return 9; if (i==3) i++; for(j=0;j<8;j++) { if(a[i][j]==0) { dathau(a,i,j); hau=hau+1; kt=sapxephau(a,hau,i+1); if (kt==1) { hau=hau-1; gohau(a,i); xuat(a); } } } for(j=0;j<8;j++) { if (a[i][j]==2) return 0; else return 1; } } void main() { clrscr(); int a[8][8]; khoitao(a); dathau(a,3,4); sapxephau(a,1,0); xuat(a); getch(); }
THANKS NHIỀU
Đã được chỉnh sửa lần cuối bởi iamvtn : 16-11-2007 lúc 01:18 PM. Lý do: Không để thẻ code vào trong code
Bài này bạn nên làm bằng quay lui(sử dụng đệ quy) chứ làm vậy không ổn lắm.Với lại khi truyền đối số vào các hàm phải truyền địa chỉ (tham chiếu) như vậy mới làm thay đổi được vị trí của ô cờ đã hay chưa đặt hậu.
Để mình xem kỹ hơn đã rồi trao đổi tiếp.
Bạn có thể tham khảo bài của mình.(Bạn tạo tệp project rồi add 2 cái vô).
Mình định cho Mouse vô, với nhiều kích thước bàn cờ.Nhưng chưa có thời gian nên vẫn để vậy.
C Code:
#include <iostream.h> #include <stdlib.h> #include <string.h> #include <conio.h> #include <stdio.h> #include <dos.h> #include <math.h> #include <alloc.h> #define o_free 0 #include <graphics.h> #define SIZE 50 void khung(); void ban_co(); int Pane[8][8];//ban co 8*8 int sc,state=0; struct tao_do{ int x[8],y[8]; }; struct save{ tao_do Hau; save *next; }; save *dau,*temp,*cuoi; void luu(){ sc++; if(dau==NULL) { cuoi=dau; cuoi->Hau=temp->Hau; cuoi->next=NULL; }else{ cuoi=dau; while(cuoi->next!=NULL) cuoi=cuoi->next; cuoi=cuoi->next; cuoi->Hau=temp->Hau; cuoi->next=NULL; }//eoE return; } void khoi_tao(){ for(int i=0;i<8;i++) for(int j=0;j<8;j++) Pane[i][j]=o_free; sc=0; dau=NULL; return ; } void xoa(int hn){ for(int i=0;i<8;i++) for(int j=0;j<8;j++) if(Pane[i][j]==hn+1) Pane[i][j]=o_free; return ; } void write_result(){ char *ch; int J=1; setcolor(RED); outtextxy(100,10,itoa(sc,ch,10)); setcolor(WHITE); cuoi=dau; while(cuoi!=NULL){ setfillstyle(SOLID_FILL,YELLOW); floodfill(97,25,WHITE); setcolor(CYAN); if( J <10 ){ outtextxy(100,24,"0"); outtextxy(110,24,itoa(J,ch,10)); }else outtextxy(100,24,itoa(J,ch,10)); ban_co(); setcolor(YELLOW); for(int i=0;i<8;i++) circle((cuoi->Hau.x[i]-1)*SIZE+10+SIZE/2,getmaxy()-(cuoi->Hau.y[i]-1)*SIZE-10-SIZE/2,14); cuoi=cuoi->next; ++J; getch(); } return ; } void da_di(int hn,int hd){ for(int i=0;i<8;i++) for(int j=0;j<8;j++) { if(i==hd && Pane[i][j]==o_free) Pane[i][j]=hn+1; if(j==hn && Pane[i][j]==o_free) Pane[i][j]=hn+1; } return ; } void run(int tt) { for(int i=0;i<8;i++) { if(tt==8) { luu();break;} else{ if(Pane[i][tt]==o_free) { temp->Hau.x[tt]=tt+1; temp->Hau.y[tt]=i+1; da_di(tt,i); run(tt+1); xoa(tt); } }//eoe }//eoi 1 } main(){ int mh=DETECT,kieu; initgraph(&mh,&kieu,"c:\\tc\\bgi"); khung(); khoi_tao(); run(0); write_result(); closegraph(); return 0; }
đó là tệp chính, còn tệp sau là tệp đồ họa(vì mình làm bên đồ họa nên ở đây mình up luôn và không có thời gian sửa lại)
C Code:
#include "conio.h" #include "stdio.h" #include "stdlib.h" #include "graphics.h" #define CL_khung RED #define MAXX getmaxx() #define MAXY getmaxy() #define SIZE 50 void Fill(int i,int j)//to o ban co { if((i+j)%2==0) { setfillstyle(SOLID_FILL,MAGENTA); floodfill(j*SIZE+35,MAXY-i*SIZE-35,WHITE); }else{ setfillstyle(SOLID_FILL,CYAN); floodfill(j*SIZE+35,MAXY-i*SIZE-35,WHITE); }//E of E } extern void ban_co() //ve ban co { int i,j; setcolor(WHITE); for(i=0;i<8;i++) for( j=0;j<8;j++) { rectangle(j*SIZE+10,MAXY-i*SIZE-10,(j+1)*SIZE+10,MAXY-(i+1)*SIZE-10); Fill(i,j);//fill pane } } extern void khung()//ve khung { //khung va nen setcolor(CL_khung); rectangle(0,0,MAXX,MAXY); setfillstyle(SOLID_FILL,BLACK+BLUE); floodfill(1,1,CL_khung); //bong do 3D cho ban co setfillstyle(1,BLACK+DARKGRAY); bar(8*SIZE+10,MAXY-5,8*SIZE+15,MAXY-8*SIZE-5);//doc bar(15,MAXY-5,8*SIZE+10,MAXY-10);//ngang ban_co(); }
Trao đổi kiến thức sẽ giúp ta tiếp cận nhanh với kiến thức.