#include
using namespace std;
class point
{public:
int q;
int w;
int getq(){return q;}
int getw(){return w;}
};
class maze
{
int map[10][10];
point record[100];
int startx;
int starty;
int exitx;
int exity;
public:
maze(int a[10][10],point r[100],int mx,int my,int nx,int ny);
int lujin(int x,int y,int count);
void show_maze(int b[10][10]);
};
maze::maze(int a[10][10],point r[100],int mx,int my,int nx,int ny)
{
startx=mx;
starty=my;
exitx=nx;
exity=ny;
for(int i=0;i {for(int j=0;j {
map[i][j]=a[i][j];
switch(map[i][j])
{
case 0:cout case 1:cout case 9:cout case -9:cout }
}
cout }
for(int i=0;i record[i]=r[i];
}
int maze::lujin(int x,int y,int count)
{
int newx,newy;
while(map[x-1][y]+map[x+1][y]+map[x][y-1]+map[x][y+1]==1)
{
if(x==exitx&&y==exity)
{
record[count].q=0;
record[count].w=0;
return 1;
}
map[x][y]=0;
newx=(x-1)*map[x-1][y]+(x+1)*map[x+1][y]+x*map[x][y-1]+x*map[x][y+1];
newy=(y-1)*map[x][y-1]+(y+1)*map[x][y+1]+y*map[x-1][y]+y*map[x+1][y];
record[count].q=newx-x;
record[count].w=newy-y;
count++;
x=newx;
y=newy;
}
map[x][y]=0;
if(map[x-1][y]+map[x+1][y]+map[x][y-1]+map[x][y+1]>1)
{
if(map[x-1][y]==1)
{
record[count].q=-1;
record[count].w=0;
if(lujin(x-1,y,count+1)==1)
return 1;
}
if(map[x+1][y]==1)
{
record[count].q=1;
record[count].w=0;
if(lujin(x+1,y,count+1)==1)
return 1;
}
if(map[x][y-1]==1)
{
record[count].q=0;
record[count].w=-1;
if(lujin(x,y-1,count+1)==1)
return 1;
}
if(map[x][y+1]==1)
{
record[count].q=0;
record[count].w=1;
if(lujin(x,y+1,count+1)==1)
return 1;
}
}
return 0;
}
void maze::show_maze(int b[10][10])
{
cout<<"\n逃跑路径为:\n";
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
map[i][j]=b[i][j];
for(int i=0;!(record[i].getq()==0&&record[i].getw()==0);i++)
{
startx+=record[i].getq();
starty+=record[i].getw();
map[startx][starty]++;
}
map[6][2]=9;
map[8][7]=-9;
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
switch(map[i][j])
{
case 0:cout<<"█";break;
case 1:cout<<" ";break;
case 2:cout<<"人";break;
case 9:cout<<"起";break;
case -9:cout<<"终";break;
}
}
cout<<"\n";
}
}
#include"my.h"
int mrr[10][10]={{0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,0,1,0,0,1,0},
{0,0,0,1,1,1,1,0,0,0},
{0,1,1,1,0,1,0,0,1,0},
{0,0,1,0,0,1,1,0,0,0},
{0,0,1,0,0,0,1,0,0,0},
{0,0,9,0,1,1,1,1,1,0},
{0,0,0,1,1,0,1,0,1,0},
{0,0,0,1,1,0,1,-9,0,0},
{0,0,0,0,0,0,0,0,0,0}};
point r[100];
void main()
{
maze m(mrr,r,6,2,8,7);
if(m.lujin(6,2,0)==3)
{
cout<<"迷宫构造有误!!!!!!!!!";
}
else m.show_maze(mrr);
system("pause");
}
http://blog.sina.com.cn/s/blog_63f880820102w6xm.html
简单描述下你这个迷宫的思路,遇到什么问题啊。。
迷宫规则?
My own code:
#include
#include
#include
#include
int main()
{
char a[100][100]={ "############",
"#O# #",
"# # # # # #",
"# # # #",
"########## #",
"# # ## #",
"# ### #",
"# ########",
"# # * ",
"############"
};
int x=1,y=1;
for(int i=0;i<=9;i++)
puts(a[i]);
while(true)
{
char ch;
bool win=false;
ch=getch();
if(ch=='s')
{
if(a[y+1][x]=='*')
a[y][x]=' ',a[++y][x]='@',win=true;
else if(y==8&&a[y+1][x]!='#')
a[y][x]=' ',a[0][x]='O',y=0;
else if(a[y+1][x]!='#')
a[y][x]=' ',a[++y][x]='O';
}
if(ch=='w')
{
if(a[y-1][x]=='*')
a[y][x]=' ',a[--y][x]='@',win=true;
else if(y==1&&a[y-1][x]!='#')
a[y][x]=' ',a[9][x]='O',y=9;
if(a[y-1][x]!='#')
a[y][x]=' ',a[--y][x]='O';
}
if(ch=='a')
{
if(a[y][x-1]=='*')
a[y][x]=' ',a[y][--x]='@',win=true;
else if(a[y][x-1]!='#')
a[y][x]=' ',a[y][--x]='O';
}
if(ch=='d')
{
if(a[y][x+1]=='*')
a[y][x]=' ',a[y][++x]='@',win=true;
else
if(a[y][x+1]!='#')
a[y][x]=' ',a[y][++x]='O';
}
system("cls");
for(int i=0;i<=9;i++)
puts(a[i]);
if(win)
{
for(int i=1;i<=INT_MAX;i+=5)
printf("YOU WIN!\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
return 0;
}
}
return 0;
}