能给个数据结构自动生成迷宫图的算法吗?
发布网友
发布时间:2022-05-16 16:21
我来回答
共4个回答
热心网友
时间:2023-10-30 21:11
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int M,N;
typedef struct _Position
{
int x;
int y;
}Position;
void Print( int** const );
void Line( int** const p, Position, Position );
int main()
{
srand( (unsigned)time(NULL) );
Position a,b;
int i,j;
printf("请输入迷宫行列:");
scanf("%d %d",&M,&N);
while(getchar()!='\n');
int** const p = (int**) malloc( M*sizeof(int*) );
for( i=0; i<M; i++ )
{
p[i] = (int*) malloc( N*sizeof(int) );
for( j=0; j<N; j++ )
{
p[i][j]=1;
}
}
p[1][1]=p[M-2][N-2]=0;
a.x=M-2;
a.y=N-2;
b.x=1;
b.y=1;
Line( p, a, b );
while(1)
{
a.x=rand()%(M-1)+1;
a.y=rand()%(N-1)+1;
b.x=rand()%(M-1)+1;
b.y=rand()%(N-1)+1;
if( a.x < b.x )
{
i=a.x;
a.x=b.x;
b.x=a.x;
}
Line( p, a, b);
if( rand()%6 ==0 ) break;
}
Print( p );
getchar();
return 0;
}
void Print( int** const dat )
{
int i,j;
for( i=0; i<M; i++ )
{
for( j=0; j<N; j++ )
{
printf("%d",dat[i][j]);
}
printf("\n");
}
printf("\n");
}
void Line( int** const p, Position Fr, Position To )
{
srand( (unsigned)time(NULL) );
int a,b,x,y,i,j;
a=Fr.x;
b=Fr.y;
i=Fr.x-1;
while(i>=To.x)
{
if(i==To.x)
{
j=To.y;
}
else
{
j=rand()%(N-1)+1;
}
p[i][j]=0;
x=i;
y=j;
while(j!=b)
{
p[i][j]=0;
if(p[i+1][j]==0)
{
break;
}
if(j>b)
{
j--;
}
else
{
j++;
}
if(i!=a && (rand()%2)==0)
{
p[i][j]=0;
i++;
}
}
a=x;
b=y;
i=x-1;
}
}
热心网友
时间:2023-10-30 21:11
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <memory.h>
/* define the size of maze */
#define MAX_COL 6
#define MAX_ROW 6
#define TRUE 1
#define FALSE 0
#define IS_USABLE(a, b) (a >= 0 && a < MAX_ROW) && (b >= 0 && b < MAX_COL) && maze[a][b] && (!my_maze[a][b])
static int maze[MAX_ROW][MAX_COL];
static int target_maze[MAX_ROW][MAX_COL];
static void init_maze();
static int move_to(int i, int j, int (*maze)[MAX_COL], int count);
static void print_maze(int (*maze)[MAX_COL]);
static void init_maze()
{
int i, j;
srand((unsigned) time(NULL));
for (i = 0; i < MAX_ROW; i++)
for(j = 0; j < MAX_COL; j++) {
maze[i][j] = (int) (rand() % 2);
}
maze[1][0] = 1; /* start point */
maze[MAX_ROW - 1][MAX_COL - 2] = 1; /* end point */
}
static int move_to(int _i,int _j, int (*in_maze)[MAX_COL], int count) {
int my_maze[MAX_ROW][MAX_COL], i, j;
if (!in_maze) {
for (i = 0; i < MAX_ROW; i++)
for(j = 0; j < MAX_COL; j++) {
my_maze[i][j] = 0;
}
} else {
for (i = 0; i < MAX_ROW; i++)
for(j = 0; j < MAX_COL; j++) {
my_maze[i][j] = in_maze[i][j];
}
}
my_maze[_i][_j] = count;
/* reach the end point */
if (_i == MAX_ROW - 1 && _j == MAX_COL - 2) {
for (i = 0; i < MAX_ROW; i++)
for(j = 0; j < MAX_COL; j++) {
target_maze[i][j] = my_maze[i][j];
}
return TRUE;
}
if (IS_USABLE(_i - 1, _j)) {
if (move_to(_i - 1, _j, my_maze, count + 1))
return TRUE;
}
if (IS_USABLE(_i + 1, _j)) {
if (move_to(_i + 1, _j, my_maze, count + 1))
return TRUE;
}
if (IS_USABLE(_i, _j - 1)) {
if (move_to(_i, _j - 1, my_maze, count + 1))
return TRUE;
}
if (IS_USABLE(_i, _j + 1)) {
if (move_to(_i, _j + 1, my_maze, count + 1))
return TRUE;
}
return FALSE;
}
static void print_maze(int (*maze)[MAX_COL]) {
int i, j;
for (i = 0; i < MAX_ROW; i++) {
for(j = 0; j < MAX_COL; j++) {
if (maze[i][j] != 0)
printf("%d ", maze[i][j]);
else
printf(" ");
}
printf("\n");
}
}
int main()
{
while(1) {
init_maze();
printf("Out put the maze :\n");
print_maze(maze);
if (move_to(1, 0, NULL, 1)) {
printf("Out put the path :\n");
print_maze(target_maze);
break;
} else {
printf("No way!\n");
}
}
}
VC60下正常运行
热心网友
时间:2023-10-30 21:12
我空间里有个迷宫的程序,可以自动生成迷宫,你可以看看。
热心网友
时间:2023-10-30 21:13
只给你算法你知道怎么调用吗?
// =======================================
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
char cw[][4]={" ","┃","━","┗","┃","┃","┏","┣",
"━","┛","━","┻","┓","┫","┳","╋"
};
char m[50][50];
char* getw(int x, int y)
{
return cw[(m[x][y-1]?0:1)|(m[x+1][y]?0:2)|(m[x][y+1]?0:4)|(m[x-1][y]?0:8)];
}
int sr(int x,int y)
{
static int d[4][2]={0,1,1,0,0,-1,-1,0};
int zx=x*2,zy=y*2,nx,tn=rand()%2? 1:3,i;
m[zx][zy]=1;
for (i=0,nx=rand()%4;i<4;i++,nx=(nx+tn)%4)
if (m[zx+2*d[nx][0]][zy+2*d[nx][1]]==0)
m[zx+d[nx][0]][zy+d[nx][1]]=1,sr(x+d[nx][0],y+d[nx][1]);
return 0;
}
void Make_Maze(int x,int y)
{
int z1,z2;
for (z1=0,z2=2*y+2;z1<=2*x+2;z1++)m[z1][0]=1,m[z1][z2]=1;
for (z1=0,z2=2*x+2;z1<=2*y+2;z1++)m[0][z1]=1,m[z2][z1]=1;
m[1][2]=1;m[2*x+1][2*y]=1;
srand((unsigned)time(NULL));
sr(rand()%x+1,rand()%y+1);
}
int main(void)
{
int x=10,y=8,z1,z2;Make_Maze(x,y);
for (z2=1;z2<=y*2+1;z2++)
{
for (z1=1;z1<=x*2+1;z1++)printf(m[z1][z2]?" ":getw(z1,z2));
if (z2<=y*2)putchar(10);
}
return 0;
}