Смекни!
smekni.com

Поиск в лабиринте (стр. 2 из 2)

fread(&ch, sizeof(ch), 1, f);

//Chitat' startovuyu pozitciu:X

fread(&ch,sizeof(ch), 1, f);

start.x = atoi(&ch);

fread(&ch, sizeof(ch), 1, f);

//Chitat' startovuyu pozitciu:Y

fread(&ch,sizeof(ch), 1, f);

start.y = atoi(&ch);

fread(&ch, sizeof(ch), 1, f);

//Chitat' final'nuyu pozitciu:X

fread(&ch,sizeof(ch), 1, f);

finish.x = atoi(&ch);

fread(&ch, sizeof(ch), 1, f);

//Chitat' final'nuyu pozitciu:Y

fread(&ch,sizeof(ch), 1, f);

finish.y = atoi(&ch);

fread(&ch, sizeof(ch), 1, f);

count_a=n;

memset(a, 0, sizeof(a));

for(i=1;i<=count_a;i++)

{

for(j=1;j<=count_a;j++)

{

fread(&ch, sizeof(ch), 1, f);

a[i][j]=atoi(&ch);

ch=0;

}

fread(&ch, sizeof(ch), 1, f);

}

fclose(f);

/*

for(i=1;i<=count_a;i++)

{

for(j=1;j<=count_a;j++)

printf("%d", a[i][j]);

printf("&bsol;n");

}

*/

}

int tLabirint::GetCommon(int i, int j, smezh &s)

{

//struct

tCoords check[5];

int k, count;

count=0;

check[1].x=j;

check[1].y=i-1;

check[2].x=j+1;

check[2].y=i;

check[3].x=j;

check[3].y=i+1;

check[4].x=j-1;

check[4].y=i;

for(k=1;k<=4;k++)

{

if((check[k].x>0) && (check[k].y>0) && (check[k].x<=count_a) && (check[k].y<=count_a))

{

if(a[check[k].y][check[k].x]==1)

{

count++;

s[count].x=check[k].x;

s[count].y=check[k].y;

}

}

}

return count;

}

void tLabirint::DFS_Visit(int y, int x)

{

int i;

int cnt;

smezh sm;

if(flag==1)

{

exit;

}

/**/

color[y][x]=1;

delay(500);

count_p++;

path[count_p].x=x;

path[count_p].y=y;

setcolor(BLUE);

//defaultmouseoff;

gui->Circle(sx+x*edge-edge / 2, sy+y*edge-edge / 2, 3);

//defaultmouseon;

//printf("X-%d;Y-%d&bsol;n", x, y);

//getchar();

if((finish.x==x) && (finish.y==y))

flag=1;

/**/

cnt=GetCommon(y, x, sm);

for(i=1;i<=cnt;i++)

{

if(color[sm[i].y][sm[i].x]==0 && flag==0)

DFS_Visit(sm[i].y, sm[i].x);

}

color[y][x]=2;

}

void tLabirint::DFS()

{

flag=0;

DFS_Visit(start.y, start.x);

}

void tLabirint::DrawLabirint()

{

int i, j;

edge=15;

cx=getmaxx() / 2;

cy=getmaxy() / 2;

sx=cx-((count_a / 2)*edge-(edge / 2));

sy=cy-((count_a / 2)*edge-(edge / 2));

fx=sx+count_a*edge;

fy=sy+count_a*edge;

setcolor(RED);

gui->Rectangle(sx, sy, fx, fy);

for(i=1;i<=count_a;i++)

gui->Line(sx+i*edge, sy, sx+i*edge, fy);

for(i=1;i<=count_a;i++)

gui->Line(sx, sy+i*edge, fx, sy+i*edge);

for(i=1;i<=count_a;i++)

{

for(j=1;j<=count_a;j++)

{

if(a[i][j]==1)

gui->Fill(sx+(j*edge)-edge / 2, sy+(i*edge)-edge / 2, RED);

}

}

}

void tLabirint::GetCoords()

{

/*

start.x=1;

start.y=1;

finish.x=5;

finish.y=4;

*/

FILE *f;

char ch;

int i,j,n;

f = fopen("input.txt", "rt");

fread(&ch,sizeof(ch), 1, f);

n = atoi(&ch);

fread(&ch, sizeof(ch), 1, f);

//Chitat' startovuyu pozitciu:X

fread(&ch,sizeof(ch), 1, f);

start.x = atoi(&ch);

fread(&ch, sizeof(ch), 1, f);

//Chitat' startovuyu pozitciu:Y

fread(&ch,sizeof(ch), 1, f);

start.y = atoi(&ch);

fread(&ch, sizeof(ch), 1, f);

//Chitat' final'nuyu pozitciu:X

fread(&ch,sizeof(ch), 1, f);

finish.x = atoi(&ch);

fread(&ch, sizeof(ch), 1, f);

//Chitat' final'nuyu pozitciu:Y

fread(&ch,sizeof(ch), 1, f);

finish.y = atoi(&ch);

fread(&ch, sizeof(ch), 1, f);

}

void tLabirint::Find()

{

GetCoords();

DFS();

if(flag==0)

outtextxy(20, 440, "No way!");

else

outtextxy(20, 440, "Found way!");

}

void main()

{

tLabirint *lab;

clrscr();

lab = new tLabirint();

lab->ReadMatrix();

lab->DrawLabirint();

lab->Find();

/**/

getch();

delete lab;

}