Instance programming: Maze Pathfinder III

Source: Internet
Author: User
Tags int size rand

Consider a tree from the maze entrance to the shortest path near the point. The shortest path near the exit can be found by means of breadth traversal. The algorithm idea of this program originates from the Dijkstra algorithm of finding the shortest path near the other points in the graph.

/* Maze Pathfinder III (Shortest path) * *

/* Dijkstramaze. C * * *

* 2003-8-26 * *

#include <stdlib.h>

#include <time.h>

#include <math.h>

#include <stdio.h>

#include <graphics.h>

#define N 22

#define M 22

int bg[m][n];

int aa[m][n];

struct pace{

int pre;

int x;

int y;

int ri;

int RJ;

}road[m*n];

struct pace bestroad[m*n];

void Makebg (Int,int);

void Drawbg (Int[][],int,int,int,int,int);

void Drawman (Int,int,int);

void rect (int,int,int,int);

void Main () {*/* Main () starts/*

int step=20;

int len=10;

int size=20;

int x=0,y=0;

int i=0,j=0;

int Gdriver=detect,gmode;

Char ch;

int Direc;

int routelen=0;

int dj[]={-1,1,0,0};

int di[]={0,0,-1,1};

int begin;

int end;

int k;

int t;

int num;

int suc;

int cnt;

int x0;

int y0;

int le;

int tmp;

MAKEBG (M,n);

/* Registerbgidriver (Egavga_driver);

Initgraph (&gdriver,&gmode, "c:\turboc2");

Initgraph (&gdriver,&gmode, "C:\TC20\BGI");

Cleardevice ();

Setwritemode (Xor_put);

Settextstyle (1,0,3);

SetColor (GREEN);

Outtextxy (100,180, "DIJKSTRA MAZE");

SetColor (BLUE);

Setfillstyle (Line_fill,blue);

/*DRAWBG (bg,m,n,size,0,0);

DRAWBG (aa,m,n,size,0,0);

SetColor (white);

X+=len;y+=len;

Drawman (X,y,len);

X0=x;y0=y;

/* Computer control *

i=j=0;

Aa[0][0]=1;

begin=0;

End=0;

road[0].ri=0;

road[0].rj=0;

road[0].x=x0;

Road[0].y=y0;

Road[0].pre=-1;

le=1;

suc=0;

while (!SUC) {

cnt=0;

le++;

for (k=begin;k<=end;k++) {

for (t=0;t<4;t++) {

X=road[k].x+dj[t]*step;

Y=road[k].y+di[t]*step;

I=ROAD[K].RI+DI[T];

J=ROAD[K].RJ+DJ[T];

if (i<m&&i>=0&&j<n&&j>=0&&aa[i][j]==0) {

cnt++;

Aa[i][j]=le;

Road[end+cnt].pre=k;

Road[end+cnt].x=x;

Road[end+cnt].y=y;

Road[end+cnt].ri=i;

Road[end+cnt].rj=j;

if (i==n-1&&j==m-1) {

Suc=1;

Break

}

}

}

if (suc) break;

}

begin=end+1;

end=end+cnt;

}

/* Output */

i=end;j=0;

while (I!=-1) {

bestroad[j].x=road[i].x;

BESTROAD[J].Y=ROAD[I].Y;

Bestroad[j].ri=road[i].ri;

BESTROAD[J].RJ=ROAD[I].RJ;

I=road[i].pre;

j + +;

}

for (i=0;i<j/2;i++) {

tmp=bestroad[i].x;

bestroad[i].x=bestroad[j-1-i].x;

bestroad[j-1-i].x=tmp;

TMP=BESTROAD[I].Y;

BESTROAD[I].Y=BESTROAD[J-1-I].Y;

bestroad[j-1-i].y=tmp;

}

Getch ();

Drawman (X0,y0,len);

for (i=0;i<j;i++) {

Drawman (Bestroad[i].x,bestroad[i].y,len);

Delay (80000);

Drawman (Bestroad[i].x,bestroad[i].y,len);

}

i--;

Drawman (Bestroad[i].y,bestroad[i].x,len);

Getch ();

Closegraph ();

}

/* Main () End/*

* * Draw Villain * *

void Drawman (int x,int y,int len) {

int R=LEN/4;

Rect (X-R,Y-LEN,X+R,Y-LEN+2*R);

Line (x,y-len+2*r,x,y);

Line (x-len,y,x+len,y);

Line (X,y,x-len,y+len);

Line (X,y,x+len,y+len);

}

* * Draw Maze Map * *

void drawbg (int bg[][n],int a,int b,int size,int x,int y) {

int startx=x;

int i,j;

for (i=0;i<a;i++) {

for (j=0;j<b;j++) {

if (bg[i][j]==-1)

Rect (x,y,x+size-1,y+size-1);

X+=size;

}

X=startx;

Y+=size;

}

Rectangle (0,0,size*b,size*a);

Line (0,0,size,0), line (0,0,0,size);

Line (size*b,size* (A-1), size*b,size*a);

Line (size* (b-1), size*a,size*b,size*a);

}

/* Draw a solid rectangle * *

void rect (int x0,int y0,int x1,int y1) {

int i,j;

for (i=x0;i<=x1;i++)

Line (I,Y0,I,Y1);

}

/* random generation to represent the maze map of the array * *

void Makebg (int a,int b) {

int i,j;

int ran;

int Direc;

/* Initialize MAZE map * *

for (i=0;i<a;i++)

for (j=0;j<b;j++)

Bg[i][j]=1;

/* Randomly generated maze path * *

Randomize ();

i=j=0;direc=2;

while (1) {

bg[i][j]=0;

if (i>=m-1&&j>=n-1) break;

ran= (int) rand () *4;

if (ran<1) {

if (direc!=1&&i<a-1) {

i++;

direc=3;

}

}

else if (ran<2) {

if (direc!=2&&j>0) {

j--;

direc=0;

}

}

else if (ran<3) {

if (direc!=3&&i>0) {

i--;

Direc=1;

}

}

else {

if (direc!=0&&j<b-1) {

j + +;

direc=2;

}

}

}

* * Randomly generated maze rest part

for (i=0;i<a;i++)

for (j=0;j<b;j++)

if (bg[i][j]==1) {

ran= (int) rand () *10;

if (ran<5) bg[i][j]=0;

}

for (i=0;i<a;i++)

for (j=0;j<b;j++) {

if (bg[i][j]==1) aa[i][j]=-1;

else aa[i][j]=0;

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.