大牛来看看这段生成迷宫的代码哪错了 ( 并查集思想 )
int hash(int x,int y, int *col){
return ( (x-1) *(* col) + y );
}
void id(int val, int &a,int &b,int *M,int *N)
{
a = val/(*M)+1;
b = val%(*M);
}
int FindSet(int x,int *father)
{
if( x != father[x])
{
father[x] = FindSet(father[x],father);
}
return father[x];
}
void Union(int a,int b,int *father)
{
father[a] = b;
}
int autoMaps(int maps[][Maxsize],int *M,int *N,Pos start,Pos end)
{
printf("请输入地图高度和宽度:");
scanf("%d%d",M,N);
printf("请输入开始位置坐标:");
scanf("%d%d",&start->x[0],&start->y[0]);
printf("请输入结束位置坐标:");
scanf("%d%d",&end->x[0],&end->y[0]);
int direction[4][2] = {0,-1,0,1,-1,0,1,0};
int father[81]={0}; //并查集的父节点记录数组
for(int i=1;i<=((*M)*(*N)+1);++i )
father[i] = i;
srand((unsigned)time(0));
int a = (start->x[0]-1)*(*N)+start->y[0];//入口
int b = (end->x[0]-1)*(*N)+end->y[0];//出口
maps[start->x[0]][start->y[0]] = maps[end->x[0]][end->y[0]] = 0;
while(FindSet(a,father) != FindSet(b,father))
//如果入口和出口不联通
{
int tmp=rand()%((*M)*(*N))+1;
int dire = rand()%4;
int tx,ty,ttx,tty;
id(tmp,tx,ty,M,N);
ttx = tx + direction[dire][0];
tty = ty + direction[dire][1];
if( (maps[tx][ty]||maps[ttx][tty]) && ttx!=0&&ttx!=(*N)+2 &&tty!=0&&tty!=(*M)+2 &&tx!=0&&tx!=(*N)+2 &&ty!=0&&ty!=(*M)+2)
{
maps[tx][ty] = 0;
maps[ttx][tty] = 0;
Union(hash(tx,ty,N),hash(ttx,tty,N),father);
}
}
for(int m = 0;m<(*N);++m)
{
for(int n =0;n<(*M);++n)
{
printf("%d",maps[m][n]);
}
printf("\n");
}
return 0;
}