#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <alloc.h>
#include <string.h>
#include <iostream.h>
#include <graphics.h>
#define MAXX getmaxx()
#define MAXY getmaxy()
#define SIZE 50
int a[12][12];
int hn,hd,x,y;
int O_FREE=0;
unsigned socach;
struct save{
int A[8][8];
save *tiep;
};
save *luu1,*luu2;
int state=0;
extern void luu_ban_co(){
socach++;
if(state==0) {
luu1=(struct save *) malloc(sizeof(save));
luu2=luu1;
for(int i=0;i<hd;i++)
for(int j=0;j<hn;j++)
luu1->A[i][j]=a[i+2][j+2];
state=1;
luu1->tiep=NULL;
}
luu1=luu2;
while(luu1->tiep!=NULL) luu1=luu1->tiep;
luu1->tiep=(struct save*)malloc(sizeof(save));
luu1=luu1->tiep;
for(int i=0;i<hd;i++)
for(int j=0;j<hn;j++)
luu1->A[i][j]=a[i+2][j+2];
luu1->tiep=NULL;
}
extern void ban_co(int a,int b);
//not graphics
int HC[8][2]={-2,1,-1,2, 1, 2,2,1,2,-1,1,-2,-1,-2,-2,-1};
void khoi_tao(){
int i,j;
for(i=0;i<hd+4;i++)
for(j=0;j<hn+4;j++)
a[i][j]=-1;
for(i=2;i<hd+2;i++)
for(j=2;j<hn+2;j++)
a[i][j]=O_FREE;
a[y+1][x+1]=1;
}
void WriteResult()//viet ket qua
{
luu1=luu2;
char *ch,*ch1;
int i,j;
unsigned sc=1;
setcolor(RED);
outtextxy(20,50,"So cot :");
outtextxy(130,50,itoa(hn,ch1,10));
outtextxy(20,60,"So hang:");
outtextxy(130,60,itoa(hd,ch1,10));
outtextxy(20,70,"Vi tri dat ma:[ ][ ]");
setcolor(YELLOW);
outtextxy(148,70,itoa(x,ch1,10));
setcolor(CYAN);
outtextxy(188,70,itoa(y,ch1,10));
setcolor(WHITE);
outtextxy(20,20,"Ban hay bam phim bat ky de xem ket qua");
outtextxy(20,30,"Co tat ca:");
outtextxy(110,30,itoa(socach,ch1,10));
setcolor(WHITE);
outtextxy(20,40,"Cach thu:");
while(luu1->tiep!=NULL){
getch();
ban_co(0,1);//xoa khi nhay sang buoc khac
// j*SIZE+10,MAXY-i*SIZE-10 j*SIZE+10+SIZE/2,MAXY-i*SIZE-10-SIZE/2
setfillstyle(SOLID_FILL,RED);//tao vi tri con ma dau tien
floodfill((x-1)*SIZE+10+SIZE/2,MAXY-(hd-y)*SIZE-10-SIZE/2,YELLOW);
setfillstyle(SOLID_FILL,BLUE);
bar( 108,39,108+strlen(itoa(sc,ch1,10))*8+2,49);
outtextxy(110,40,itoa(sc,ch1,10));//hien cach chay thu..n..cua con ma
setcolor(BLACK);
for(i=0;i<hd;i++)
for(j=0;j<hn;j++)
outtextxy(j*SIZE+SIZE/2+5,MAXY-(hd-i)*SIZE-15+SIZE/2,itoa(luu1->A[i][j],ch,10));
luu1=luu1->tiep;
sc++;
}
}
void Try(int sb,int u,int v)//thu cac buoc chay cua Ma
{
int i,uu,vv;
for(i=0;i<8;i++)
{
uu=u+HC[i][0];
vv=v+HC[i][1];
if(a[uu][vv]==O_FREE){
a[uu][vv]=sb;
if(sb==hn*hd) luu_ban_co();
else Try(sb+1,uu,vv);
a[uu][vv]=O_FREE;
}
}
}
extern void ma_di_tuan()
{
khoi_tao();
Try(2,y+1,x+1);
if(state) WriteResult();
setcolor(WHITE);
outtextxy(MAXX/2,MAXY/2,"THE END");
getch();
}