کد:
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <iomanip.h>
const n=20;
const m=20;
char x[m][n]={{'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
{'.','.','.','.','#','#','#','.','.','#','.','.','.','.','.','#','#','#','#','#'},
{'#','#','#','.','#','.','#','#','.','#','.','#','#','#','.','#','#','#','.','#'},
{'#','#','#','.','#','.','#','.','.','#','.','.','.','#','.','.','.','#','.','#'},
{'#','#','#','.','.','.','#','.','#','#','#','#','.','#','#','#','.','#','.','#'},
{'.','.','.','#','#','.','#','.','#','#','#','#','.','.','.','#','.','.','.','#'},
{'#','#','.','.','.','.','#','.','.','#','.','.','#','#','.','#','#','.','#','#'},
{'#','#','.','#','#','#','#','#','.','.','.','.','#','.','.','#','#','.','#','#'},
{'#','.','.','#','#','#','.','.','.','#','#','.','#','.','#','#','#','.','#','.'},
{'#','.','#','#','#','#','.','#','#','.','#','.','#','.','.','#','.','.','#','#'},
{'#','.','.','.','#','.','.','#','#','.','#','.','#','#','.','#','.','#','#','#'},
{'#','#','#','.','#','.','#','#','#','#','#','.','#','.','.','#','.','#','#','#'},
{'#','.','.','.','#','.','#','#','#','.','.','.','#','.','#','.','.','#','#','#'},
{'#','.','#','#','#','.','#','#','#','.','#','#','#','.','#','.','#','#','#','#'},
{'#','.','#','#','#','.','#','#','#','.','#','#','.','.','#','.','#','#','#','#'},
{'.','.','#','#','#','.','.','.','#','.','#','#','.','#','#','.','#','#','#','#'},
{'#','.','.','.','.','#','#','.','#','.','#','#','.','#','#','.','.','.','.','#'},
{'#','#','#','#','.','#','#','.','#','.','.','#','.','#','#','#','#','#','.','#'},
{'#','#','#','#','.','.','.','.','#','#','.','.','.','#','#','#','#','#','.','#'},
{'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','.','#'}};
class maze
{
//written by : navid
public:
maze(){finish=0;}
void draw(void);
void insert(void);
int search(void);
void slow(void);
void move(int, int, int);
int end(int, int);
private:
int stp;
int gapi[10],gapj[10];
int finish;
};
//*********************************************
void maze::insert()
{
char ch;
int i,j;
cout<<"press any key to start...";
getch();
stp=1;
draw();
int k=search();
if(k<=1)
{
cout<<"\n\n not find way...";
getch();
exit(0);
}
cout<<"\n\nthere is "<<k<<" ways existing , press key...";
getch();
clrscr();
cout<<"if you have enter the start data press (y).\nelse press any key:";
ch=getch();
if(ch=='y')
{
cout<<"\n\nenter the coordinate:\n\n";
cout<<"enter i:";
cin>>i;
cout<<"enter j:";
cin>>j;
}
else
{
i=gapi[0];
j=gapj[0];
}
cout<<"\n\nif you have slow move press (y). \nelse press any key:";
ch=getch();
if(ch=='y')
stp=300;
else
stp=200;
move(1,i,j);
move(2,i,j);
move(3,i,j);
move(4,i,j);
if(finish)
cout<<"\n \n find the way...";
else
cout<<" \n\n not find any way...";
}
//***********************************
int maze::search(void)
{
int k=0;
for(int i=0;i<m;i++)
if(x[i][0]=='.')
{
gapi[k]=i; gapj[k]=0;
k++;
}
for(int z=0;i<m;i++)
if(x[z][n-1]=='.')
{
gapi[k]=z; gapj[k]=n-1;
k++;
}
for(int j=0; j<n;j++)
if(x[0][j]=='.')
{
gapi[k]=0; gapj[k]=j;
k++;
}
for(int b=0;j<n;j++)
if(x[m-1][b]=='.')
{
gapi[k]=m-1; gapj[k]=b;
k++;
}
return (k);
}
//*****************************************
void maze::draw (void)
{
clrscr();
for(int p=0;p<m;p++)
{
for(int q=0;q<n;q++)
cout<<x[p][q]<<setw(3);
cout<<endl;
}
slow();
}
//*****************************
int maze::end(int i,int j)
{
int k=finish;
if(i==0 || i==m-1 || j==n-1)
if(i!=gapi[0] && j!=gapj[0])
{
finish=1;
k=1;
}
return (k);
}
//******************************
void maze::slow(void)
{
for(int p=0;p<stp;p++)
for(int q=0;q<stp;q++)
for(int r=0;r<stp;r++);
}
//*********************************
void maze::move(int dir, int i, int j)
{
if(end(i,j))
return;
if(dir==1 && j+1<n && x[i][j+1]=='.')
{
x[i][j]='o';
x[i][j+1]='M';
draw();
move(1,i,j+1);
move(2,i,j+1);
move(3,i,j+1);
move(4,i,j+1);
move(0,i,j+1);
}
else if(dir==2 && i+1<m && x[i+1][j]=='.')
{
x[i][j]='o';
x[i+1][j]='M';
draw();
move(1,i+1,j);
move(2,i+1,j);
move(3,i+1,j);
move(4,i+1,j);
move(0,i+1,j);
}
else if(dir==3 && j-1>=0 && x[i][j-1]=='.')
{
x[i][j]='o';
x[i][j-1]='M';
draw();
move(1,i,j-1);
move(2,i,j-1);
move(3,i,j-1);
move(4,i,j-1);
move(0,i,j-1);
}
else if(dir==4 && i-1>=0 && x[i-1][j]=='.')
{
x[i][j]='o';
x[i-1][j]='M';
draw();
move(1,i-1,j);
move(2,i-1,j);
move(3,i-1,j);
move(4,i-1,j);
move(0,i-1,j);
}
else if(dir==0)
{
x[i][j]='.';
draw();
}
return;
}
//**********************************
int main()
{
maze m;
m.insert();
getch();
return 0;
}
منبع:
علاقه مندی ها (Bookmarks)